Differentiation of Java While Loop

Loops

Loops can execute a block of code as long as a specified condition is reached. Loops are handy because they save time, reduce errors, and they make code more readable. The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is recommended to use the while loop. The while loop can be thought of as a repeating if statement. While loop in Java comes into use when we need to repeatedly execute a block of statements

Java While Loop

The while loop loops through a block of code as long as a specified condition is true:

Syntax

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

In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:

Example

int i = 0;
while (i < 5) {
  System.out.println(i);
  i++;
}

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax

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

The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:

Example

int i = 0;
do {
  System.out.println(i);
  i++;
}
while (i < 5);

The different parts of do-while loop:

1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed and control goes to update expression. When the condition becomes false, we exit the while loop.

Example:

i <=100

2. Update expression: Every time the loop body is executed, this expression increments or decrements loop variable.

Example:

i++;

Flowchart of Java While Loop

Here, the important thing about while loop is that, sometimes it may not even execute. If the condition to be tested results into false, the loop body is skipped and first statement after the while loop will be executed.

flowchart of java while loop

Example:

In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately need to initialize and increment the variable used in the condition (here, i). Otherwise, the loop will execute infinitely.

public class WhileExample {  
public static void main(String[] args) {  
    int i=1;  
    while(i<=10){  
        System.out.println(i);  
    i++;  
    }  
}  
}  

Output:

1
2
3
4
5
6
7
8
9
10

Java Infinitive While Loop

If you pass true in the while loop, it will be infinitive while loop.

Syntax:

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

Example:

public class WhileExample2 {    
public static void main(String[] args) {   
 // setting the infinite while loop by passing true to the condition  
    while(true){    
        System.out.println("infinitive while loop");    
    }    
}    
}    

Output:

infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
ctrl+c
Differentiation of Java While Loop
Show Buttons
Hide Buttons