Java Do While Loop

 

The do-while is an iterative control in Java programming language. This loop executes one or more statements while the given condition is true. In this loop, the condition comes after the body of loop. The loop is important in a situation where a statement must be at executed at least once. The body of “do while” loop is executed once because the condition comes after the body of the loop.

SYNTAX:

The following is the syntax of “do while” loop is Java:

do

{

Statements;

}

while (condition);

 

Flowchart of “do while” loop in Java:

flowchart-of-do-while-loop-in-java

Working of Do-while loop in Java:

First of all, the body of loop is executed. After executing the statements in the loop body, the condition is evaluated. If it is true, the control again enters the body of loop and executes all statements in the body again. This process continues as long as the condition remains true. The loop terminates when the condition becomes false. This loop is executed at least once even if the condition is false in the beginning.

Example of “do while” loop in Java:

Consider the following example in which the “do while” loop is used to print a string of characters:

CODE:

public class Example DoWhile {

public static void main (string [] args) {

int n;

n =1;

do

{

system. out. println (“Country”);

n++;

} While (n<=5);

}

}

OUTPUT:

Country

Country

Country

Country

Country

In the above example we used the do while statement to print the string of characters. First a variable is declared and is initialized at one, then comes the loop, the keyword “do” is used for the “do while” loop. Note that we did not use condition there and the control enters the body of the loop. The statements in the loop are executed and then we checked the condition after the body of the loop. If the condition is true the control again enters the body of the loop. This will continue until the condition becomes false.

 

Java infinitive do while loop:

If in the “do while” loop true is passed by the user then the loop becomes infinitive in Java. The following is the syntax of the infinitive “do while” loop in Java programming language.

Syntax:

The following is the syntax to make a do while loop an infinitive loop:

do {

statements;

} while (true);

Example of infinitive do while loop in Java:

CODE:

public class ExampleDoWhile {

public static void main (string [] args) {

do

{

system. out. println (“Infinitive do While loop in Java”);

} While (true);

}

}

OUTPUT:

Infinitive do While loop in Java

Infinitive do While loop in Java

Infinitive do While loop in Java

Infinitive do While loop in Java

Infinitive do While loop in Java

Infinitive do While loop in Java

Infinitive do While loop in Java

Infinitive do While loop in Java

Infinitive do While loop in Java

Infinitive do While loop in Java

Infinitive do While loop in Java

Infinitive do While loop in Java

The loop will not break until the user press ctrl + c. The combination that is ctrl + c is used to exit from the loop or program.