Java While Loop [with Examples]

Java Tutorials

Summary: In this tutorial, you will learn how to use while loop in Java with the help of examples.

While Loop in Java

A while loop in Java allows you to repeat a block of code as long as a certain condition is true. Here is the syntax for a while loop in Java:

while (condition) {
   // code block to be executed
}

The condition is a boolean expression that is evaluated before each iteration of the loop. If the condition is true, the code block within the loop is executed. If the condition is false, the loop ends and control is transferred to the next statement after the loop.

Here is an example of a while loop in Java that counts down from 10 to 1:

int count = 10;

while (count > 0) {
   System.out.println(count);
   count--;
}

This loop will print the numbers 10 through 1 to the console. On each iteration of the loop, the value of count is decremented by 1 using the -- operator. When count reaches 0, the loop ends.

Example 1: Summing numbers

int sum = 0;
int i = 1;

while (i <= 10) {
   sum += i;
   i++;
}

System.out.println("Sum: " + sum);

This while loop will sum the numbers from 1 to 10 and print the result to the console.

The loop continues as long as i is less than or equal to 10, and on each iteration the value of i is incremented by 1 using the ++ operator.

When i reaches 11, the loop ends and the final value of sum is printed to the console.

Example 2: Reading user input

Scanner input = new Scanner(System.in);
int number;

while (true) {
   System.out.print("Enter a number: ");
   if (input.hasNextInt()) {
      number = input.nextInt();
      break;
   } else {
      System.out.println("Invalid input.");
      input.next();  // discard invalid input
   }
}

This while loop will keep asking the user to enter a number until they provide a valid integer input.

The Scanner class is used to read input from the user, and the hasNextInt() method is used to check if the input is an integer.

If the input is not an integer, the loop continues and asks the user to enter a valid number.

Do While Loop in Java

A do-while loop in Java is similar to a while loop, except that the code block within the loop is always executed at least once. The do-while loop then continues to repeat the block of code as long as a certain condition is true. Here is the syntax for a do-while loop in Java:

do {
   // code block to be executed
} while (condition);

The code block within the loop is executed first, and then the condition is evaluated. If the condition is true, the loop continues and the code block is executed again. If the condition is false, the loop ends and control is transferred to the next statement after the loop.

Here is an example of a do-while loop in Java that counts down from 10 to 1:

int count = 10;

do {
   System.out.println(count);
   count--;
} while (count > 0);

This loop will print the numbers 10 through 1 to the console. On each iteration of the loop, the value of count is decremented by 1 using the -- operator.

The loop continues as long as count is greater than 0. When count reaches 0, the loop ends.

Infinite While Loop

while (true) {
   // code block to be executed
}

This while loop will run indefinitely, because the condition (true) is always true.

You should be careful when using infinite loops, as they can cause your program to hang or crash if you do not include a way to break out of the loop.


Leave a Reply

Your email address will not be published. Required fields are marked *