Bird
Raised Fist0
C++programming~10 mins

For loop in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - For loop
Initialize i=0
↓
Check: i < 5?
No→EXIT
Yes↓
Execute body
↓
Update: i = i+1
↩Back to Check
The for loop starts by setting i to 0, then checks if i is less than 5. If yes, it runs the loop body, then increases i by 1 and repeats. If no, it stops.
Execution Sample
C++
for (int i = 0; i < 5; i++) {
    std::cout << i << std::endl;
}
Prints numbers from 0 to 4, one per line.
Execution Table
IterationiCondition (i < 5)ActionOutput
10truePrint 0, i++0
21truePrint 1, i++1
32truePrint 2, i++2
43truePrint 3, i++3
54truePrint 4, i++4
65falseExit loop
💡 i reaches 5, condition 5 < 5 is false, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
i0123455
Key Moments - 3 Insights
Why does the loop stop when i is 5 even though we print numbers starting from 0?
The loop condition checks if i < 5. When i becomes 5, this condition is false, so the loop stops before printing 5. See execution_table row 6.
When exactly does i increase in the loop?
i increases after the loop body runs, before the next condition check. This is shown in execution_table where 'i++' happens after printing.
What happens if we change i++ to i += 2?
i would increase by 2 each time, so the loop runs fewer times. The variable_tracker would show i values jumping by 2 instead of 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i during the 3rd iteration?
A1
B3
C2
D0
💡 Hint
Check the 'i' column in the execution_table row labeled '3'
At which iteration does the loop condition become false?
A5
B6
C4
D1
💡 Hint
Look at the 'Condition (i < 5)' column in the execution_table where it is false
If we change i++ to i += 2, how would the variable_tracker change after the first iteration?
Ai would be 2 instead of 1
Bi would be 0 instead of 1
Ci would be 3 instead of 1
Di would not change
💡 Hint
Refer to variable_tracker and think how i increments by 2 instead of 1
Concept Snapshot
for loop syntax: for (init; condition; update) { body }
Runs body while condition is true.
Init runs once at start.
Update runs after each body execution.
Stops when condition is false.
Full Transcript
A for loop in C++ starts by initializing a variable, here i=0. Then it checks if i is less than 5. If yes, it runs the loop body which prints i. After that, i increases by 1. This repeats until i reaches 5, when the condition becomes false and the loop stops. The variable i changes from 0 up to 5, but 5 is not printed because the loop stops before printing it.

Practice

(1/5)
1.

What does a for loop in C++ do?

easy
A. Defines a new function
B. Runs code only once
C. Stops the program immediately
D. Repeats a block of code a fixed number of times

Solution

  1. Step 1: Understand the purpose of a for loop

    A for loop is designed to repeat code multiple times based on a condition.
  2. Step 2: Identify the correct description

    Among the options, only repeating code a fixed number of times matches the for loop's behavior.
  3. Final Answer:

    Repeats a block of code a fixed number of times -> Option D
  4. Quick Check:

    For loop = repeat fixed times [OK]
Hint: For loops repeat code a set number of times [OK]
Common Mistakes:
  • Thinking for loops run only once
  • Confusing for loops with if statements
  • Believing for loops stop the program
2.

Which of the following is the correct syntax for a for loop in C++?

for (___; i < 5; i++) {
    // code
}
easy
A. int i = 0
B. i = 0
C. int i
D. int i == 0

Solution

  1. Step 1: Check initialization syntax in for loop

    The initialization part must declare and initialize the loop variable, e.g., int i = 0.
  2. Step 2: Compare options for correctness

    Only int i = 0 correctly declares and initializes the variable.
  3. Final Answer:

    int i = 0 -> Option A
  4. Quick Check:

    Initialization needs declaration and assignment [OK]
Hint: Initialization needs declaration and assignment [OK]
Common Mistakes:
  • Using 'int i' without initialization
  • Using 'int i == 0' which is a comparison
  • Omitting the type declaration
3.

What is the output of this code?

for (int i = 1; i <= 3; i++) {
    std::cout << i * 2 << " ";
}
medium
A. 1 2 3
B. 2 4 6
C. 0 2 4
D. 2 3 4

Solution

  1. Step 1: Identify loop iterations

    The loop runs with i = 1, 2, 3 because condition is i <= 3.
  2. Step 2: Calculate output for each iteration

    Outputs are i*2: 2, 4, 6 printed with spaces.
  3. Final Answer:

    2 4 6 -> Option B
  4. Quick Check:

    Multiply i by 2 each loop [OK]
Hint: Multiply loop variable by 2 each iteration [OK]
Common Mistakes:
  • Confusing loop range (off by one)
  • Printing i instead of i*2
  • Missing spaces in output
4.

Find the error in this for loop:

for (int i = 0; i > 5; i++) {
    std::cout << i << " ";
}
medium
A. Variable i is not declared
B. Missing semicolon after i++
C. The condition 'i > 5' is incorrect, loop never runs
D. The loop will run infinitely

Solution

  1. Step 1: Analyze the loop condition

    The condition is i > 5 but i starts at 0, so it is false immediately.
  2. Step 2: Understand loop execution

    Since condition is false at start, loop body never executes.
  3. Final Answer:

    The condition 'i > 5' is incorrect, loop never runs -> Option C
  4. Quick Check:

    Condition false at start means no loop runs [OK]
Hint: Check if loop condition is true initially [OK]
Common Mistakes:
  • Assuming loop runs despite false condition
  • Thinking missing semicolon causes error here
  • Confusing variable declaration errors
5.

How can you use a for loop to print all even numbers from 2 to 10 inclusive?

Choose the correct code snippet:
hard
A. for (int i = 2; i <= 10; i += 2) { std::cout << i << ' '; }
B. for (int i = 1; i < 10; i++) { if (i % 2 == 0) std::cout << i << ' '; }
C. for (int i = 2; i < 10; i++) { std::cout << i * 2 << ' '; }
D. for (int i = 0; i <= 10; i += 3) { std::cout << i << ' '; }

Solution

  1. Step 1: Understand the goal

    We want to print even numbers 2,4,6,8,10 inclusive.
  2. Step 2: Check each option

    for (int i = 2; i <= 10; i += 2) { std::cout << i << ' '; } increments by 2 starting at 2, printing 2,4,6,8,10 correctly.
    for (int i = 1; i < 10; i++) { if (i % 2 == 0) std::cout << i << ' '; } prints 2,4,6,8 but misses 10.
    for (int i = 2; i < 10; i++) { std::cout << i * 2 << ' '; } prints 4,6,8,10,12,14,16,18 (wrong range).
    for (int i = 0; i <= 10; i += 3) { std::cout << i << ' '; } increments by 3, not even numbers.
  3. Final Answer:

    for (int i = 2; i <= 10; i += 2) { std::cout << i << ' '; } -> Option A
  4. Quick Check:

    Increment by 2 from 2 to 10 [OK]
Hint: Increment loop variable by 2 starting at 2 [OK]
Common Mistakes:
  • Using wrong increment step
  • Forgetting to include 10
  • Using condition inside loop unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard