

In the case of do-while loop, first statements inside do block are executed and then the test condition is verified.Ģ. In the case of while loop, first test condition is verified and then executes the statements inside the while block. The difference between while loop and do-while loop in Java is as follows:ġ.

("Sum of numbers: " +sum) ĭifference between while loop and do-while loop Continue reading data until the input is 0. Create an object of Scanner class to take input. In this example program, do-while loop is in nested form and produces the following output. Let’s another example program based on do while statement where we will display the multiplication table. As the value of x is greater than 6, the condition will be false and the loop will be terminated.Ģ. In this way, this process continues until the value of x is less than or equal to 6.į. Since 3 is also less than 10, the flow once again goes back and displays x value.Į. Then the x value once again incremented and becomes 3. Since this condition is true, the flow of execution will go back and the value of x, i.e. The conditional boolean expression x<=10 is tested.Ĭ. Now, x++ statement will increment the value of x by 1, hence the value of x becomes 2. In this example, the value of x is initially set to 1. } while(x <= 6) // This statement will execute as long as x <= 6.Ī. Let’s take a simple example program where we will display numbers from 1 to 6. Example Program based on do while loop in Javaġ. In this example, the body of loop will execute until one of the two relations is true. We do not need to use curly braces if there is only one statement inside the do…while loop.Ĭonsider the following example. The test condition must be boolean expression. Since the specified condition testes at the bottom of the loop, the do…while loop provides an exit-controlled loop. The control of execution comes out of the do-while loop and goes to the next statement that appears immediately after the while statement. When the specified condition is false, the loop ends. This process continues until the test condition is true. If the evaluation is true, the loop body executes again. The execution flowchart for the do-while loop in Java has shown in the below figure.įrom the do-while loop flowchart, it is clear that the loop body executes first, and then the loop conditional expression evaluates to determine whether to continue or terminate the loop. The general syntax for using do-while loop in Java is as: Initialization įlowchart Diagram of Do While Loop Statement The for loop syntax is considered to be somewhat more complex than that of other loop types in Java due to its use of three expressions.The do-while loop always executes its body at least once before the test evaluates because its test conditional expression is at the bottom of the loop.

The Java f or loop is often a better choice than a while or do while loop when you know exactly how many times you want to loop through a block of code. You can learn more about while and do while loops in our guide: Java While and Do While Loops. In today’s follow-up, we will conclude our examination of supported loop types in Java as we explore the f or and f or-each loops. In the Java while and do while loops programming tutorial, we learned about two of the most basic and oldest looping constructs.
