Skip to content
techsnow

Control Flow

Loops

for loop

for (int i = 1; i <= 5; i++) {
    cout << i << " ";
}

Output

1 2 3 4 5

while and do-while

int n = 3;
while (n > 0) {
    cout << n-- << " ";
}

Output

3 2 1

Nested loops: a pattern

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= i; j++) cout << "* ";
    cout << endl;
}

Output

*
* *
* * *