Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops

What Is a do‑while Loop?

The do‑while loop is a control‑flow construct that executes a block of code at least once and repeats it as long as a specified condition remains true. Unlike a standard while loop, the condition is evaluated after the loop body, guaranteeing one execution regardless of the initial value of the condition.

When to Use a do‑while Loop?

Use a do‑while loop when you need:

Syntax

do {
    // code
} while (condition);

The loop body precedes the while condition, so the compiler executes the body first and then checks the condition to decide whether to iterate again.

How It Works

C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops

Flow‑chart steps:

  1. Start the loop.
  2. Execute the loop body.
  3. Evaluate the condition.
  4. If true, repeat from step 2.
  5. If false, exit the loop and continue with subsequent statements.

Example 1: Counting with a do‑while Loop

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

Output:

C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops

The loop prints values 1 through 4 because the condition x < 5 becomes false after x reaches 5.

Example 2: Summing Numbers Until Zero Is Entered

#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:

C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops

In this example, the loop continues until the user enters 0, ensuring that at least one number is processed.

Nested do‑while Loops

Placing a do‑while loop inside another creates a nested structure, useful for multi‑dimensional iterations such as printing a grid or performing compound calculations.

Syntax

do {
    // outer body
    do {
        // inner body
    } while (inner_condition);
} while (outer_condition);

How It Works

Execution proceeds as follows:

  1. Initialize outer variables.
  2. Enter the outer loop body.
  3. Enter the inner loop body.
  4. After each inner iteration, evaluate inner_condition to decide whether to continue the inner loop.
  5. Once the inner loop ends, update outer variables and evaluate outer_condition to decide whether to repeat the outer 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);
    return 0;
}

Output:

C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops

Key Takeaways

C Language

  1. Master C++ For Loops: A Step-by-Step Guide
  2. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  3. Mastering C++ Pointers: Concepts, Examples & Practical Applications
  4. Mastering C++ Char Data Types: Declaration, ASCII, and Conversion Techniques
  5. C++ Operator Overloading – A Practical Guide with Code Examples
  6. C++ Functions Explained with Practical Code Examples
  7. C# Hashtable Explained: Key-Value Storage, Operations, and Practical Examples
  8. While vs. Do‑While Loops: Clear Comparison with Practical Examples
  9. Python Loop Control: break, continue, and pass Statements Explained with Practical Examples
  10. Understanding C++ Loop Types: For, While, Do-While Explained