Loading...

Loops in C++: for, while, do-while

Learn loops in C++ using for, while, and do-while statements to repeat tasks and control program execution efficiently.

In programming languages, loops allow a certain operation to be repeated multiple times. In C++, the most commonly used loop constructs are for, while, and do-while. Loops are used for repetitive tasks such as iterating over arrays, performing calculations, or reading input from the user.


1. for Loop

The for loop is the most commonly used construct when the number of iterations is known. It consists of three parts: initialization, condition, and increment/decrement.


for (initialization; condition; increment) {
    // Statements to repeat
}

Example:


#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        cout << "Number: " << i << endl;
    }
    return 0;
}

Output:


Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

2. while Loop

The while loop runs as long as the condition is true. The condition is checked at the start of each iteration, so if it is false at the beginning, the loop may not run at all.


int number = 1;

while (number <= 5) {
    cout << "Number: " << number << endl;
    number++;
}

This example produces the same result as the for loop but has a more flexible structure. It is generally used when the number of repetitions is not known in advance (for example, reading user input).


3. do-while Loop

In a do-while loop, the condition is checked at the end, so the loop body runs at least once.


int number = 1;

do {
    cout << "Number: " << number << endl;
    number++;
} while (number <= 5);

The output again prints the numbers from 1 to 5. But since the condition is checked at the end, the first iteration runs regardless of the condition.


4. Infinite Loops

If a loop’s condition never becomes false, the loop will run forever. Infinite loops are usually terminated with break or return.


while (true) {
    cout << "This message is printed continuously." << endl;
    break; // Stops the infinite loop
}

Note: Infinite loops should be used carefully; otherwise, the program may never stop.


5. break and continue Keywords

Two special statements are used to change loop control:


for (int i = 1; i <= 10; i++) {
    if (i == 5)
        continue; // Skips 5
    if (i == 8)
        break;    // Stops the loop at 8
    cout << i << " ";
}

Output:


1 2 3 4 6 7

6. Nested Loops

A loop can be defined inside another loop. This structure is especially useful when working with tables, matrices, or multi-dimensional data.


#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 4; j++) {
            cout << "(" << i << "," << j << ") ";
        }
        cout << endl;
    }
    return 0;
}

Output:


(1,1) (1,2) (1,3) (1,4)
(2,1) (2,2) (2,3) (2,4)
(3,1) (3,2) (3,3) (3,4)

Here the outer loop (i) represents the rows, and the inner loop (j) represents the columns. This structure is very useful when working with multi-dimensional arrays.


7. Sample Application: Multiplication Table

One of the most common uses of nested for loops is a multiplication table.


#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= 5; j++) {
            cout.width(4);
            cout << i * j;
        }
        cout << endl;
    }
    return 0;
}

Output:


   1   2   3   4   5
   2   4   6   8  10
   3   6   9  12  15
   4   8  12  16  20
   5  10  15  20  25

8. Using while and do-while with User Input

Loops can also be used to repeatedly ask the user for input until a valid value is entered.


#include <iostream>
using namespace std;

int main() {
    int number;
    do {
        cout << "Enter a positive number: ";
        cin >> number;
    } while (number <= 0);

    cout << "You entered: " << number << endl;
    return 0;
}

If the user enters a negative number, the loop repeats; when a positive number is entered, the loop ends.


9. TL;DR

  • for → use when the number of iterations is known.
  • while → runs as long as the condition is true.
  • do-while → runs at least once, then checks the condition.
  • break → terminates the loop, continue → skips to the next iteration.
  • Nested loops are used for tables or matrix operations.
  • All examples can be run with Visual Studio 2022 or GCC 11+.

Related Articles