Java While Loop

The while loop in Java is a control flow statement that allows you to repeat a block of code as long as a boolean condition is true. It’s a looping statement that allows you to execute a block of code repeatedly until a particular condition is met. The while loop is useful when you don’t know how many times you need to loop in advance, as it allows you to loop until a certain condition is met. The while loop is also useful when you want to perform an action an unknown number of times, as it allows you to repeat the action as long as a certain condition is true.

While Loop

The while loop in Java is a control flow statement that allows you to repeat a block of code as long as a boolean condition is true. It’s a looping statement that allows you to execute a block of code repeatedly until a particular condition is met.

Here is the syntax of 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 is executed. If the condition is false, the loop is terminated and control is transferred to the next statement following the loop.

It’s important to include a way to change the value of the condition within the loop, otherwise the loop will become an infinite loop and will never terminate. This can cause your program to crash or freeze.

Here is an example of a while loop in Java:

int i = 1;

while (i <= 10) {
  System.out.println(i);
  i++;
}

In this example, the condition is i <= 10, which means that the loop will continue to execute as long as i is less than or equal to 10. The code block inside the loop consists of a single statement that prints the value of i to the console. The value of i is incremented by 1 at the end of each iteration, using the i++ operator. This ensures that the condition will eventually become false, terminating the loop.

The output of this while loop will be the numbers 1 through 10, each on a separate line.

While Loop Examples

counts down from 10 to 1:

int i = 10;

while (i >= 1) {
  System.out.println(i);
  i--;
}

In this example, the boolean expression is i >= 1, which means that the loop will continue to execute as long as i is greater than or equal to 1. The code block inside the loop consists of a single statement that prints the value of i to the console. The value of i is decremented by 1 at the end of each iteration, using the i-- operator. This ensures that the boolean expression will eventually become false, terminating the loop.

The output of this while loop will be the numbers 10 through 1, each on a separate line.

calculates the factorial of a number:

int num = 5;
int factorial = 1;

while (num > 1) {
  factorial *= num;
  num--;
}

System.out.println(factorial); // 120

In this example, the while loop calculates the factorial of num, which is 5. The boolean expression is num > 1, which means that the loop will continue to execute as long as num is greater than 1. The code block inside the loop consists of a single statement that multiplies the value of factorial by num and then decrements num by 1. This ensures that the boolean expression will eventually become false, terminating the loop.

The output of this while loop will be the value 120, which is the factorial of 5.

Similar Posts

Leave a Reply

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