do while loop in Java

do while loop is also said as exit check loop since it checks the condition after executing the body of the do loop.

General Syntax

do {

//statements or code to be executed

}

while(condition or expression);

Explanation

At first, the statements inside the body of do gets executed, then checks the condition in while statement.

If condition returns true,the statements inside body of the do gets executed again.

If condition returns false the loop stops execution.


Example

public class doWhileTest {

public static void main(String args[]) {

int i=1;

do{

System.out.println(i);

i++;

}while(i<1);

}

}

Try In Editor

Output

1

In the above example, variable i is initialized with 1. Statements in the body of do is executed first and then checks the condition given inside while.

If condition returns true, statements inside do gets executed again and checks the condition again. If false terminates the loop execution.

This will be repeated till the condition becomes false.