What is While Loop in Java?

Video Tutorial
FREE
While loops thumbnail
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

A while loop in Java is a control flow statement that executes code repeatedly based on a given Boolean expression. It executes the statements repeatedly until the Boolean expression is true, execution is terminated when the condition becomes false.

Java's while loop is a repeating if statement and is used when a specific statement needs to be executed repeatedly.

When the number of iterations isn't fixed, Java's while loop is recommended. Here, first the boolean expression, and depending on its result the loop is executed hence while loop in java is an entry-controlled loop.

While Loop Syntax in Java

Parts of While Loops in Java

Syntax of the while loop shows that its execution depends on two major parts as mentioned below.

  1. Test Condition
  2. Update Condition

The loop variable used in these two conditions is initialized outside of the while loop block, before starting its execution. Let's understand these two parts of the while loop.

Test Condition

  • This is the first condition that is checked when the flow of execution falls in the while loop.
  • Test condition is a boolean expression that is checked whenever the loop is executed.
  • If the test_condition is true then only statements in the body of the while loop are executed.
  • If the condition is false then the flow of execution will come out from the while loop i.e. it exits from the while loop.
  • Example:

Update Condition

  • Update condition is executed after execution of the loop body.
  • This condition increments/decrements the loop variable in the Test Condition by some value.
  • After updating the loop variable using this condition, the execution of loops starts again.
  • Execution of the while loop in java is terminated when the loop variable updated using the update condition does not meet the test condition.
  • Update condition is used so that after a given set of the interval the loop will break else it will lead to an infinite loop.
  • Example:

Flowchart of While Loop in Java

Let's understand the execution of the while loop in java using a flowchart:

While loop flowchart

Working of Java While Loop

According to the above flowchart, the working of the while loop is as follows:

  1. Execution control of code falls into the while loop.
  2. The flow jumps to the Test condition of the while loop which is a boolean expression.
  3. Test condition is checked.
    • If the test condition is True, then the body of the while loop is executed.
    • If the test condition is False, then the execution of the loop is terminated.
  4. When the Test condition is true, the body of the while loop is executed.
  5. Update condition is executed.
  6. Execution control flows back to Step 2.
  7. When the Test condition is false, the execution of the while loop is terminated.

While loop in Java Examples

Example 1

Let's understand the while loop using a simple example.

Problem Statement: Print "Hello World!" 10 times using while loop in java.

Program:

Output:

In this example, while loop is used to print Hello World!. The test_condition (i<=10) is checked while executing the loop. The variable i is initialized to 1 and the test condition says it can be less than or equal to 10.

Every time Hello World! is printed and variable i is incremented by 1. This loop is executed continuously until update condition gives result i = 11. When the test condition is checked for this condition it yields the result False and the while loop is terminated. In this way, the above program prints the statement Hello World! 10 times along with the iterations.

Example 2

Let's see one more program using while loop in java.

Problem Statement: Write a program to calculate the sum of positive integers from 1 to 100.

Program:

Output:

Here, we have calculated the sum of the first 100 positive integers i.e 1 to 100 using the while loop in java. First variable i*** is initialized to 1. The test condition (i<=100) checks whether i is less than or equal to 100.

Until the condition is true, the number in variable i is added in variable sum using statement sum = sum + i;. And i is incremented by 1 using update condition i++. The final output stored in sum is printed after the flow of execution comes out of the while loop.

Infinite While Loop in Java

As explained earlier, while loop stops execution when the boolean test_condition is false. While loop executes infinitely when the boolean condition is always true. To terminate the infinite loop, we need to enter Ctrl + C command. Let's see this using a program.

Using an infinite while loop is never recommended, this may occur when the update statement is not correctly defined and the loop condition never becomes false hence it doesn't break.

Program:

Output:

As we know, while loop executes continuously until the test condition is true. Here, the test condition is always true hence the loop executes infinitely. We need to terminate the condition using the command Ctrl + C on the command prompt.

Conclusion

  • While loop in java is a control structure used to execute code repeatedly until the boolean test condition is true.
  • Update condition is used to increment the loop variable. Execution of the while loop is terminated when the test condition is false.
  • While loop executes infinitely when the test condition is always true.

See more