while loop: It repeats a statement until the controlling expression becomes false.
Note: Controlling expression can be of any Boolean type.
Note: While loop body can be empty, that can be seen in the example given below.


while (controlling-expression)
// body of the loop, statements to be executed

The Do/While Loop in Java

The do/while loop is a modification of the while loop in java. The loop will perform the piece of code once before verifying if the condition goes right or evaluates to true, then it will replicate the loop as long as the condition goes right and evaluates to true.


do {
// body of the loop, it will be executed at least once
} while (condition-expression);

Infinite While Loop in Java

This loop indicates that it will become an infinite while loop if you pass true in the while loop.
In this, you need to press ctrl+c to exit from the code.

There are two parts of the While loop, and these are mentioned below:

  • Test Expression: In Test expression, you have to test the given condition. When the condition goes right and evaluates to true, run the loop's body and move to update the expression unless you will exit from the while loop.
    For example: i <= 10
  • Update Expression: After executing the loop body, update expression increments/decrements the loop variable by any value.
    For example: i++;

Example :

Com.knowledge2life public class Main { public static void main(String[] args) { int i = 0; while (i < 5) { System.out.println("Knowledge2life "); i++; } } }