Looping in C++: For, While, and Do-While Loops

Looping in C++

For Loop

  • Entry-controlled loop.

  • Executes a block of code repeatedly for a specified number of times.

  • Preferred over while and do-while loops when the number of iterations is known beforehand.

Syntax:
for (initialization; test condition; update) {
    // body of for loop
}
Parts of the for loop:
  • Initialization: Initializes the loop variable to an initial value.

  • Test Condition: Evaluated before each iteration. If true, the loop body executes. If false, the loop terminates.

  • Update Expression: Increments/decrements the loop variable after each iteration.

Example:
#include <iostream>
using namespace std;

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

This code will print numbers 1 to 4.

Example (Print even numbers between 2 and 20):

Goal: Print even numbers from 2 to 20

While Loop

  • Entry-controlled loop.

  • Repeatedly executes a block of code as long as a given condition is true.

  • Used when the exact number of iterations is unknown beforehand.

  • Loop execution terminates based on the test condition.

Syntax:
while (condition) {
    // Body of the loop
    update expression;
}
Key points:
  • The loop variable needs to be declared beforehand.

  • The loop variable should be updated inside the loop body.

Parts of the While loop:
  • Condition: The loop continues as long as this condition is true.

  • Update Expression: Modifies the loop variable, moving it closer to the termination condition.

  • Body: The block of statements executed as long as the condition is true.

Example:
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i < 5) {
        cout << i << endl;
        i++;
    }
    return 0;
}

This code will print numbers 1 to 4.

Example (User Input):

Goal: Continuously prompt the user to enter numbers until -1 is inputted.

Do-While Loop

  • Exit-controlled loop.

  • Executes a block of code at least once.

  • Continues executing as long as a given condition remains true.

  • Guarantees that the loop body will execute at least once.

Syntax:
do {
    // Body of the loop
    // Update expression
} while (condition);
Parts of the do-while loop:
  • Condition: Checked after the loop body executes. The loop continues if the condition is true, exits if false.

  • Update Expression: Modifies the loop variable to approach the termination condition.

  • Body: The block of statements that executes at least once and then continues as long as the condition is true.

Example:
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    do {
        cout << i << endl;
        i++;
    } while (i < 5);
    return 0;
}

This code will print numbers 1 to 4.

Example (Countdown):

Goal: Display a countdown from 10 to 1.

Code Examples (Illustrative)

Example 1
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i >= 5; i++) {
        cout << i << " ";
    }
}
Example 2
#include <iostream>
using namespace std;

int main() {
    int x = 0;
    for (int i = 5; i >= 1; i--) {
        x += i;
    }
    cout << x;
}
Example 3
#include <iostream>
using namespace std;

int main() {
    for (int i = 2; i <= 10; i += 2) {
        cout << i << " ";
    }
}
Example 4
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 3) {
        cout << "i";
        i++;
    }
}
Example 5
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    do {
        cout << (13) << " ";
        i++;
    } while (i <= 5);
}
Example 6
#include <iostream>
using namespace std;

int main() {
    int y = 0, i = 1;
    do {
        y += i;
        i++;
    } while (i <= 3);
    cout << y;
}