Loop Control
Loop control refers to the mechanism used to control the flow of execution in a loop. Loop statements allow us to execute a statement or a group of statements multiple times, and below is the general form of loop statements in most Java programming:
Flow
Diagram:
Java Loop
The Java programming language provides several types of loops: while loop, do while loop, for loop, and enhanced for loop to handle looping requirements.
While Loop
The while loop in Java is used to execute a block of code repeatedly as long as the specified condition is true. It tests the condition before executing the loop body.
Syntax:
Flow
Diagram:
Source Code:
import java.util.Scanner;
public class WhileLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.print("Enter a number (0 to exit): ");
number = scanner.nextInt();
while (number != 0) { // 0 != 0 //false
System.out.println("You entered: " + number);
System.out.print("Enter another number (0 to exit): ");
number = scanner.nextInt();
}
System.out.println("Loop terminated. Goodbye!");
scanner.close();
}
}Result:
Watch the video:Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

