The do-while loop

Reading Time: 2 minutes

The do-while loop first executes the loop statements before evaluating the expression. It means that the statements in a do-while loop will always run at least once, even the condition evaluates to false.

 The syntax

When the program runs, code inside do block is executed once without testing the condition. After that, condition is tested.

There are 2 cases

  • Test condition is true.
    Code inside do block will be executed. Then, test condition is tested again. Process is repeated till test condition is true.
  • Test condition is false.
    If it is so, do while loop is terminated.
This flowchart shows the process of the do-while loop. As you can see the loop first will execute the statement. Thenafter it will check the given condition.

If the condition is true, then the statement will be executed again. The loop will be repeated until the condition is true. When it is false, then the loop will be finished.

Examples

 The output
Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1
 The output
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Questions

I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

 


 

Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

Leave a comment

stay informed!

Subscribe to receive exclusive content and notifications