In this lesson of our Java course, we will learn about Java while loop. In Java, loops are used to execute a block of code repeatedly until a certain condition is met. One of the loop types available in Java is the while loop.
Java while loops are a type of control flow statement that repeatedly executes a block of code as long as a given condition is true. There are 2 variations of the Java while loop. The main difference between a do-while loop and a while loop is that a do-while loop guarantees that the code block inside the loop will be executed at least once.
The while loop in Java is a type of control flow statement that repeatedly executes a block of code as long as a given condition is true. The basic syntax of the while loop is as follows:
while (condition) {
// code block to be executed
}
Let’s take a look at the following flow chart to understand how Java while loop works:
public class JavaWhileLoopExample {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
}
}
In this example, the while loop will execute the code block that prints the value of i
to the console and increments i
by 1
, as long as the condition i < 5
is true. The loop will exit when i is no longer less than 5
.
The do-while loop in Java is similar to the while loop, but it guarantees that the code block inside the loop will be executed at least once. The basic syntax of the do-while loop is as follows:
do {
// code block to be executed
} while (condition);
A flowchart of a do-while loop would look like this:
public class JavaWhileLoopExample {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
}
}
In this example, the do-while loop will execute the code block that prints the value of i
to the console and increments i
by 1
, at least once.
The do-while loops are useful when you want to execute a block of code at least once before checking the condition, for example when you want to read user input until they enter the correct value
public class JavaWhileLoopExample {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int value;
do {
System.out.print("Enter a value between 1 and 10: ");
value = input.nextInt();
} while (value < 1 || value > 10);
}
}
This example uses a do-while loop to read user input and check that the value entered is between 1 and 10. The code block inside the loop prompts the user to enter a value and reads the input using the nextInt()
method of the Scanner class. The condition value < 1 || value > 10
is evaluated after the code block has executed, and as long as the condition is true, the loop will continue to execute.
The while loops are a useful tool for controlling the flow of a program and repeatedly executing a block of code as long as a given condition is true. The main difference between a do-while loop and a while loop is that a do-while loop guarantees that the code block inside the loop will be executed at least once.
As always, the source code for this lesson is available on our GitHub repository.