What is do-while loop?

The do-while loop iterates a section of the C++ program several times. In the do-while loop, test expression is added at the bottom of the loop. The loop body comes before the test expression. That's why the loop body must execute for once, even when test expression evaluates to false in the first test.

In this C++ tutorial, you will learn:

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:

do{    
//code 
}while(condition);  

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?

C++ Do while flow chart

Flow Chart Explanation:

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

Example 1

#include <iostream>
using namespace std;
int main() {
	// Local variable 
	int x = 1;
	do {
		cout << "X is: " << x << endl;
		x = x + 1;
	} while (x < 5);
	return 0;
}

Output:

Here is a screenshot of the code:

Code Explanation:

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

Example 2

#include <iostream>
using namespace std;
int main()
{
	int num, sum = 0;
	do {

		cout << "Enter a number: ";

		cin >> num;

		sum += num;

	} while (num != 0);
	cout << "Sum is " << sum;

	return 0;
}

Output:

Here is a screenshot of the code:

Code Explanation:

  1. Including the iostream header file in our code. It will allow us to read from and write to the console.
  2. Including the std namespace so as to use its classes and functions without calling it.
  3. Calling the main() function inside which the logic of the program should be added.
  4. The { marks start of body of the main() function.
  5. Declaring two integer variables, num, and sum. The variable sum has been initialized to 0.
  6. Creating a do-while loop. The { marks start of loop body.
  7. Printing the text "Enter a number:" on the console.
  8. Reading user input from the console and storing the value in variable num. The cin (console input) is a function that reads user input.
  9. Adding the value of num to value of sum and storing result in variable sum.
  10. 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.
  11. Printing value of variable sum on the console alongside other text.
  12. The main() function should return a value if the program runs fine.
  13. 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

do{
statement(s)
do{
statement(s)
}while(condition);
statement(s)
}while(condition);

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

#include <iostream>  
using namespace std;
int main() {
	int a = 1;
	do {
		int b = 1;
		do {
			cout << a << "\n";
			b++;
		} while (b <= 3);
		a++;
	} while (a <= 3);
}

Output:

Here is a screenshot of the code:

Code Explanation:

  1. Including the iostream header file in our code. It will allow us to read from and write to the console.
  2. Including the std namespace so as to use its classes and functions without calling it.
  3. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function.
  4. Declaring an integer variable a and assigning it a value of 1.
  5. Creating the outer do-while loop. The { marks start of outer loop body.
  6. Declaring an integer variable b and assigning it a value of 1.
  7. Creating the inner do-while loop. The { marks start of inner loop body.
  8. To print the value of variable a on the console. The "\n" is a new line character that moves mouse cursor to next line.
  9. Increasing value of b by 1 after every iteration.
  10. 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.
  11. Increasing value of a by 1 after every iteration.
  12. 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.
  13. 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.

 

YOU MIGHT LIKE: