Java Do While Loop

In this tutorial you will learn about the Java Do While Loop and its application with practical example.

Java Do While Loop

The do…while loop executes block of statements inside loop body first and then test the condition for next iteration and executes next only if condition is true. The do…while loop is much similar to Java while loop with one major difference, in do…while loop block of statements inside loop body executes at least once.

Java do while Loop Flow Diagram

swift-repeat-while-loop-flowchart-diagram

Syntax:-

Here, loop executes block of statements inside loop body first and then test the condition provided along with while keyword for next iteration and executes next only if condition is true. Condition is a Boolean expression that results in either True or False, if it results in True then statements inside loop body are executed and condition is evaluated again. This process is repeated until the condition is evaluated to False. If the condition results in False then loop is terminated and control is transferred to next statement.

Example:-

Here, we have initialized ctr and maxCtr to 1 and 5 respectively, in the next statement we have loop body inside the pair of curly brackets {} after do keyword, statements inside loop body are executed first then control is passed to the condition ctr <= maxCtr provided along with while keyword for next iteration. If the test condition returns True, then control is passed again to loop body for the next iteration. If the condition results in False then loop is terminated and control is transferred to next statement. Inside the loop body we have incremented the ctr value by one for each if the iteration. When we run the above java program, we see the following output –

Recommended:-  Java while Loop

Output:-

java_do_while_loop_example

In this tutorial we have learn about the Java Do While Loop and its application with practical example. I hope you will like this tutorial.