You are currently viewing Java while loop and do-while loop

Java while loop and do-while loop

In the previous tutorial, we have learned For Each Loop and in this tutorial, we will learn about the while and do-while loop with an example.

What is while Loop?

The while loop in Java is a control flow statement that allows code to be repeated based on a Boolean condition. The while loop is similar to a looping if statement. If the condition evaluates to true, the body of the loop will be executed, and we will proceed to the update expression.

The while loop is a type of if statement that repeats itself. The while loop is recommended if the number of iterations is not fixed.

Syntax:

while (testExpression) {
// statements
}

The various parts of the while loop are as follows:

  • Test Expression: We must test the condition in this expression. If the condition evaluates to true, the body of the loop will be executed, and we will proceed to the update expression. Otherwise, the while loop will be terminated.

Example: i = 1

  • Updated Expression: This expression increments or decrements the loop variable by some value after the loop body is executed.

Example: i++;

Working of while loop:

  • Control is trapped in a while loop.
  • The flow then jumps to Condition, which is tested.
  • If the condition is true, the flow is directed to the Body.
  • If the condition returns false, the flow exits the loop.

Example: Print integer value from 1 to 10.

class Sample {
  public static void main(String[] args) {

    // declare variables
    int i = 1, n = 10;

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

Output:

1
2
3
4
5
6
7
8
9
10

Example: Infinitive While Loop.

public class Sample {    
public static void main(String[] args) {   
// By passing true to the condition, we can start an infinite while loop.    
while(true){    
        System.out.println("Developers Dome");    
    }    
}    
}    

Output:

Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
… infinite…

What is do while Loop?

The Java do-while loop is used to repeatedly iterate a section of a programme until the specified condition is met. A do-while loop is recommended if the number of iterations is not fixed and the loop must be executed at least once. An exit control loop is a Java do-while loop.

  • An exit control loop is a Java do-while loop. In contrast to while and for loops, the do-while loop checks the condition at the end of the loop body. Because the condition is checked after the loop body, the Java do-while loop is executed at least once. 
  1. Condition: It is an expression. If the condition is met, the loop body is executed, and control is transferred to the update expression. When the condition becomes false, the loop automatically breaks.

Example: i = 100

  1. Update expression: This expression increments or decrements the loop variable every time the loop body is executed.

Example: i++;

Syntax:

do {    
//…statement   
}while (condition);   

Example: Print integer values from 1 to 10

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

Output:

1
2
3
4
5
6
7
8
9
10

Example: Infinitive do-while Loop

public class Sample {  
public static void main(String[] args) {  
    do{  
        System.out.println("Developers Dome");
    }while(true);  
   }  
}  

Output:

Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome
…infinite

You May like:

Java For Loop with Example | Java Tutorial

We hope that this article will assist you in understanding all about Java while loop and do while. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, we will be glad to assist you.

This Post Has 2 Comments

Leave a Reply