Loops

What are Loops?

Repeat code multiple times without writing it again.

3 Types:

  1. for loop
  2. while loop
  3. do-while loop

3.1 for Loop

Best when you know HOW MANY times to repeat.

Syntax:

for(initialization; condition; update) {
    // code to repeat
}

Example: Print 1 to 5

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

Output:

1
2
3
4
5


How for Loop Works