Back to C++
2026-01-266 min read

Flowchart of do...while Loop (C++)

Learn Flowchart of do...while Loop (C++) step by step with clear examples and exercises.

Why This Matters

The do...while loop is a fundamental control structure in C++ programming that enables you to execute a code block repeatedly until a specific condition is met, unlike the while loop which may not execute the code block if the initial condition is false. Mastering the do...while loop will empower you to tackle real-world programming problems efficiently and effectively while avoiding common pitfalls.

Prerequisites

To fully comprehend this lesson, you should be familiar with:

  1. Basic C++ syntax (variables, operators, functions)
  2. Control structures (if, if...else, switch)
  3. The while loop
  4. Understanding of variables and data types in C++
  5. Familiarity with input/output operations in C++
  6. Concepts of conditional statements and loops
  7. Basic understanding of algorithms and problem-solving strategies

Core Concept

The do...while loop is a control structure that repeatedly executes a code block as long as a specified condition remains true. Here's the general syntax:

do {
// Code block to be executed
} while (condition);

Unlike the while loop, the do...while loop ensures that the code block is executed at least once before checking the condition. This can be useful when you want to perform an action even if the initial condition is false.

Example: Printing numbers 1 to 5 using a do...while loop

#include <iostream>

int main() {
int number = 1;
do {
std::cout << number << std::endl;
++number;
} while (number <= 5);

return 0;
}

In this example, the code block inside the do...while loop will be executed at least once, printing 1. Then, as long as number is less than or equal to 5, it will print the current value of number and increment it by one.

Advantages and Disadvantages of do...while Loop

Advantages:

  • Ensures execution of the code block at least once before checking the condition, which can be useful when you want to perform an action even if the initial condition is false.
  • Can make your code easier to read in certain situations where it's important to see the loop body before the condition check.

Disadvantages:

  • May execute the code block more times than necessary, as it will always run at least once. This can lead to unnecessary computations and slower performance if not used judiciously.

Worked Example

Let's create a simple program that asks the user for their age and prints "You are eligible to vote" if they are 18 or older, and "You are not eligible to vote" otherwise.

#include <iostream>

int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;

do {
if (age >= 18) {
std::cout << "You are eligible to vote." << std::endl;
break;
} else {
std::cout << "You are not eligible to vote. Try again.\nEnter your age: ";
std::cin >> age;
}
} while (true);

return 0;
}

In this example, we use a do...while loop to continuously ask the user for their age until they enter an eligible vote. The break statement is used to exit the loop once the user is eligible to vote.

Common Mistakes

  1. Forgetting the semicolon at the end of the while condition: This can cause unexpected behavior, as the loop will continue indefinitely without checking the condition.
do {
// Code block
} while (condition); // Correct

do {
// Code block
} while(condition); // Incorrect - missing semicolon
  1. Not initializing the loop variable: If you don't initialize the loop variable before entering the do...while loop, it will have an arbitrary value that may cause unintended behavior.
int number;
do {
std::cout << number << std::endl;
++number;
} while (number < 10); // Incorrect - `number` is not initialized before the loop
  1. Infinite Loop: If the condition for exiting the loop never becomes true, the do...while loop will create an infinite loop. This can be avoided by ensuring that the condition eventually changes to false.

Common Mistakes - Subheadings

  • Missing Semicolon after while condition
  • Uninitialized Loop Variable
  • Infinite Loop

Practice Questions

  1. Write a do...while loop that asks the user for their name and prints "Hello, [name]!" until they enter an empty string.
#include <iostream>
#include <string>

int main() {
std::string name;
do {
std::cout << "Enter your name: ";
std::cin >> name;
if (name.empty()) {
break;
}
std::cout << "Hello, " << name << "!\n";
} while (true);

return 0;
}
  1. Create a program that calculates the factorial of a number using a do...while loop. The program should ask the user to input a positive integer and keep asking until a valid input is provided.
#include <iostream>

int main() {
int n, fact = 1;
bool validInput = false;

do {
std::cout << "Enter a positive integer: ";
std::cin >> n;
if (n >= 0) {
validInput = true;
for (int i = 1; i <= n; ++i) {
fact *= i;
}
std::cout << "Factorial of " << n << " is: " << fact << std::endl;
} else {
std::cout << "Invalid input. Please enter a positive integer.\n";
}
} while (!validInput);

return 0;
}
  1. Write a program that finds the smallest common multiple (SCM) of two numbers using the Euclidean algorithm in a do...while loop.
#include <iostream>

int scm(int a, int b) {
if (b == 0) {
return a;
}
do {
int temp = a % b;
a = b;
b = temp;
} while (temp != 0);
return a * b;
}

int main() {
int num1, num2;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
int scmValue = scm(num1, num2);
std::cout << "Smallest Common Multiple of " << num1 << " and " << num2 << " is: " << scmValue << std::endl;

return 0;
}

FAQ

  1. Why use a do...while loop instead of a while loop? The do...while loop ensures that the code block is executed at least once before checking the condition, which can be useful when you want to perform an action even if the initial condition is false.
  2. How do I exit a do...while loop early? You can use the break statement to exit a do...while loop early. The break statement will cause the loop to terminate immediately.
  3. What happens if I forget the semicolon at the end of the while condition in a do...while loop? If you forget the semicolon, the loop will continue indefinitely without checking the condition, causing an infinite loop. This can lead to program crashes or unexpected behavior.
  4. What is the difference between a do...while loop and a for loop? The main differences are:
  • A for loop has an initialization, condition check, and increment/decrement section, while a do...while loop only has a code block and a condition check.
  • A for loop may not execute the code block if the initial condition is false, whereas a do...while loop always executes the code block at least once before checking the condition.
  1. Can I use a do...while loop to create an infinite loop? Yes, if the condition for exiting the loop never becomes true, the do...while loop will create an infinite loop. This can be avoided by ensuring that the condition eventually changes to false.
  2. What are some common mistakes when using a do...while loop? Common mistakes include forgetting the semicolon at the end of the while condition, not initializing the loop variable, and creating an infinite loop due to a never-changing condition.
  3. When should I use a do...while loop instead of a while loop or for loop? You should use a do...while loop when you want to ensure that the code block is executed at least once before checking the condition, such as in situations where initializing variables inside the loop is necessary or when dealing with user input. However, it's important to consider the trade-offs between readability, performance, and maintainability when choosing a control structure.
Flowchart of do...while Loop (C++) | C++ | XQA Learn