fbpx

When to Use Do-While Loops in C++?

When to Use Do-While Loops in C++


INTRODUCTION

Loops are a fundamental concept in programming, allowing developers to automate repetitive tasks efficiently. In C++, loops enable the execution of a block of code multiple times based on specific conditions, making them indispensable for tasks like iterating over data structures, implementing algorithms, or performing repetitive computations.

C++ provides various types of loops, each suited for different scenarios:

  • For Loop: Ideal when the number of iterations is predetermined.
  • While Loop: Used when the number of iterations depends on a dynamic condition.
  • Do-While Loop: Guarantees execution of the loop body at least once, even if the condition is initially false.
  • Nested Loops: Allows loops within loops for handling complex iteration patterns.

1. For Loop: (when the no of iterations are known)

Syntax:

for (initialization; condition; increment/decrement) {

    // code block

}

Example:

 

#include <iostream>

 

int main() {

    for (int p = 0; p<5 ; p++) {

        cout<< i;

    }

    return 0;

}

Output:

 

i = 0

i = 1

i = 2

i = 3

i = 4

Explanation:

  • initialization: int i = 0; — Initializes the loop variable.
  • condition: i < 5; — Loop continues as long as this condition is true.
  • increment/decrement: i++ — Increments the loop variable after each iteration.

2. While Loop

The while loop is used when the number of iterations is not known, and the loop continues as long as a specified condition is true.

Syntax:

 

while (condition) {

    // code block

}

Example:

 

#include <iostream>

 

int main() {

    int i = 0;

    while (i < 5) {

        cout<< i;

        i++;

    }

    return 0;

}

Output:

 

i = 0

i = 1

i = 2

i = 3

i = 4

Explanation:

  • The while loop continues as long as i < 5 is true. After each iteration, i is incremented.

3. Do-While Loop

The do-while loop is similar to the while loop, but it guarantees that the loop body is executed at least once, even if the condition is false.

Syntax:

 

do {

    // code block

} while (condition);

Example:

 

#include <iostream>

 

int main() {

    int i = 0;

    do {

        cout<< i;

        i++;

    } while (i < 5);

    return 0;

}

Output:

 

i = 0

i = 1

i = 2

i = 3

i = 4

Explanation:

  • The loop executes at least once, regardless of the condition. After the first iteration, the condition i < 5 is checked to determine whether the loop continues.

4. Nested Loops

In C, you can use loops inside other loops. These are called nested loops.

Example:

 

#include <iostream>

 

int main() {

    for (int i = 1; i <= 3; i++) {

        for (int j = 1; j <= 3; j++) {

            cout <<i<< j;

        }

    }

    return 0;

}

Output:

 

i = 1, j = 1

i = 1, j = 2

i = 1, j = 3

i = 2, j = 1

i = 2, j = 2

i = 2, j = 3

i = 3, j = 1

i = 3, j = 2

i = 3, j = 3

Explanation:

  • The outer loop (for i) runs 3 times, and for each iteration, the inner loop (for j) also runs 3 times.

5. Breaking Out of Loops

You can use the break statement to exit a loop prematurely, regardless of whether the loop’s condition is true.

Example:

 

#include <iostream>

 

int main() {

    for (int i = 0; i < 10; i++) {

        if (i == 5) {

            break;  // Exits the loop when i equals 5

        }

        Cout<< i;

    }

    return 0;

}

Output:

 

i = 0

i = 1

i = 2

i = 3

i = 4

Explanation:

  • The loop runs until i reaches 5, at which point the break statement is executed, terminating the loop.

6. Skipping Iteration (Continue)

You can use the continue statement to skip the current iteration of the loop and proceed with the next iteration.

Example:

 

#include <iostream>

 

int main() {

    for (int i = 0; i < 5; i++) {

        if (i == 3) {

            continue;  // Skips the rest of the loop when i equals 3

        }

        Cout<< i;

    }

    return 0;

}

Output:

 

i = 0

i = 1

i = 2

i = 4