In this C++ tutorial, you will learn:

What is do-while loop? When to use a do-while loop?
Syntax
How do-while loop works?
Example 1
Example 2
Nested Do While Loop
Syntax
How Nested Do While Loop Works
Example

When to use a do-while loop?

The do-while loop should be used when the number of iterations is not fixed, and the loop must execute for at least once. The C++ compiler executes the loop body first before evaluating the condition. That means the loop must return a result. This is the case even when the test condition evaluates to a false on the first evaluation. Since the loop body has already been executed, it must return the result.

Syntax

The basic syntax of C++ do while loop is as follows: The condition is test expression. It must be true for the loop to execute. The { and } mark the body of do while loop. It comes before the condition. Hence, it is executed before the condition.

How do-while loop works?

Flow Chart Explanation:

Start of do while loop. The body of do while loop. The test expression or condition to be evaluated. If the test expression is true, the C++ compiler executed the body of do while loop. If the test expression is false, the C++ compiler executes the statements after the loop body. Statements that come after the loop body.

Example 1

Output:

Here is a screenshot of the code:

Code Explanation:

Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. A comment. The C++ compiler will skip this. Declaring an integer variable x and assigning it a value of 1. Creating a do-while loop. The { marks start of loop body. To print the value of variable x alongside other text on the console. The endl is a C++ keyword meaning end line. Increasing value of x by 1 after every iteration. End of the loop body. Test expression has been added to the end of the loop body. It tests whether the value of x is less than 5. The main() function should return a value if the program runs fine. End of the body of the main() function.

Example 2

Output:

Here is a screenshot of the code:

Code Explanation:

Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring two integer variables, num, and sum. The variable sum has been initialized to 0. Creating a do-while loop. The { marks start of loop body. Printing the text “Enter a number:” on the console. Reading user input from the console and storing the value in variable num. The cin (console input) is a function that reads user input. Adding the value of num to value of sum and storing result in variable sum. The } marks the end of the loop body. The test expression has been added to the end of the loop body. It tests whether the value entered by the user to make sure it’s not 0. The != is the not equal to the operator. If the user enters a 0, the loop should terminate. Printing value of variable sum on the console alongside other text. The main() function should return a value if the program runs fine. End of the body of the main() function.

Nested Do While Loop

In C++, it is possible for us to create one do-while loop inside another do-whole loop. This results into a nested do-while loop.

Syntax

The first do statement denotes the do part of the outer do-while loop. The second do statement denotes the do part of the inner do-while loop. The first while statement denotes the test condition for the inner loop. The second while statement denotes the test condition for the outer loop.

How Nested Do While Loop Works

Nested do while loop works as follows: Step 1: The initialization is executed first and once. Step 2: The statements (the do) is executed once. Step 3: Test expression is evaluation by flow control. Step 4: If true, inner loop is executed. Step 5: Updating statement(s) are updated. Step 6: Process runs repeatedly until test expression becomes false. Step 7: When test expression becomes false, inner loop is exited and control jumps to outer loop. Step 8: Test condition is evaluated again. Step 9: If true, statement(s) are executed to return false. Step 10: Execution of loop stops and control jumps to statements after loop.

Example

Output:

Here is a screenshot of the code:

Code Explanation:

Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring an integer variable a and assigning it a value of 1. Creating the outer do-while loop. The { marks start of outer loop body. Declaring an integer variable b and assigning it a value of 1. Creating the inner do-while loop. The { marks start of inner loop body. To print the value of variable a on the console. The “\n” is a new line character that moves mouse cursor to next line. Increasing value of b by 1 after every iteration. End of the inner loop body. Test expression has been added to the end of the loop body. It tests whether the value of b is less than or equal to 5. Increasing value of a by 1 after every iteration. End of the outer loop body. Test expression has been added to the end of the loop body. It tests whether the value of a is less than or equal to 3. End of the body of the main() function.

Summary

The do-while loop runs a section of code several times. It should be used when the number of iterations is not fixed. In the do-while loop, the loop body comes before test expression. The loop body executes for at least once, even if the test expression becomes false. This loop should be used when the number of iterations is not fixed. Do while loop can run any number of times, provided the condition is true. The condition is evaluated once after every iteration. The outcome of the evaluation of the condition determines the action to be taken. If the condition is true, the loop executed the body. Execution of loop halts immediately the condition evaluates to a false. Since the do while loop body has already been executed, it must return the result.