Bird
Raised Fist0
C++programming~10 mins

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

Choose your learning style10 modes available
Concept Flow - While loop
Initialize variable
↓
Check condition
↓
Execute loop body
↓
Update variable
↩Back to Check condition
The while loop checks a condition before each iteration. If true, it runs the loop body and updates variables, then checks again. If false, it exits.
Execution Sample
C++
#include <iostream>
using namespace std;

int main() {
    int i = 0;
    while (i < 3) {
        cout << i << " ";
        i++;
    }
    return 0;
}
This code prints numbers 0, 1, 2 by looping while i is less than 3.
Execution Table
Stepi valueCondition (i < 3)ActionOutput
10truePrint 0, i = i + 10
21truePrint 1, i = i + 11
32truePrint 2, i = i + 12
43falseExit loop
💡 i reaches 3, condition 3 < 3 is false, loop stops
Variable Tracker
VariableStartAfter 1After 2After 3Final
i01233
Key Moments - 2 Insights
Why does the loop stop when i equals 3?
Because the condition i < 3 becomes false at step 4 (see execution_table row 4), so the loop exits.
What happens if we forget to update i inside the loop?
The condition i < 3 would always be true, causing an infinite loop because i never changes (no progress).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i at step 2?
A0
B2
C1
D3
💡 Hint
Check the 'i value' column in execution_table row 2.
At which step does the condition become false and the loop stops?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look at the 'Condition' column in execution_table to find when it is false.
If we change the condition to i < 2, how many times will the loop run?
A2 times
B1 time
C3 times
D0 times
💡 Hint
Check variable_tracker and think when i reaches 2, condition i < 2 becomes false.
Concept Snapshot
while (condition) {
    // code to repeat
    // update variables
}

- Checks condition before each loop
- Runs body only if condition true
- Updates variables inside to avoid infinite loop
- Stops when condition is false
Full Transcript
A while loop starts by checking a condition. If the condition is true, it runs the code inside the loop body. Then it updates variables, usually to move toward stopping the loop. It repeats this check and run cycle until the condition becomes false, then it exits. For example, starting with i = 0, while i is less than 3, it prints i and increases i by 1. When i reaches 3, the condition fails and the loop stops. Forgetting to update i causes an infinite loop because the condition never becomes false.

Practice

(1/5)
1.

What does a while loop do in C++?

easy
A. Executes code only once
B. Repeats code a fixed number of times
C. Stops the program immediately
D. Repeats code while a condition is true

Solution

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

    A while loop runs the code inside it repeatedly as long as the condition is true.
  2. Step 2: Compare options with definition

    Only Repeats code while a condition is true correctly states this behavior.
  3. Final Answer:

    Repeats code while a condition is true -> Option D
  4. Quick Check:

    while loop = repeat while true [OK]
Hint: While loops run as long as condition is true [OK]
Common Mistakes:
  • Thinking while loops run a fixed number of times
  • Confusing while with if statement
  • Assuming while loops always run once
2.

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

???
{
    // code
}
easy
A. while condition
B. while {condition}
C. while (condition)
D. while: (condition)

Solution

  1. Step 1: Recall correct while loop syntax

    The condition must be inside parentheses after the keyword while.
  2. Step 2: Check each option

    while (condition) uses while (condition), which is correct syntax in C++.
  3. Final Answer:

    while (condition) -> Option C
  4. Quick Check:

    while loop syntax = while (condition) [OK]
Hint: Condition in while must be inside parentheses [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Using curly braces instead of parentheses
  • Adding colon after while
3.

What is the output of this C++ code?

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

Solution

  1. Step 1: Analyze loop condition and increments

    Variable i starts at 1 and increases by 1 each loop until i > 3.
  2. Step 2: Trace output values

    i = 1 prints "1 ", then 2 prints "2 ", then 3 prints "3 ". Loop stops after i becomes 4.
  3. Final Answer:

    1 2 3 -> Option B
  4. Quick Check:

    Prints 1 to 3 with spaces [OK]
Hint: Count from start to condition limit inclusive [OK]
Common Mistakes:
  • Printing extra number after loop ends
  • Starting count from 0 instead of 1
  • Printing numbers in reverse
4.

Find the error in this code snippet:

int count = 5;
while (count > 0)
    std::cout << count << " ";
medium
A. Infinite loop because count is never changed
B. Variable count is not declared
C. Syntax error in while condition
D. Missing semicolon after cout statement

Solution

  1. Step 1: Check loop body and variable changes

    The loop prints count but does not change it inside the loop.
  2. Step 2: Understand loop behavior

    Since count stays 5, condition is always true, causing an infinite loop.
  3. Final Answer:

    Infinite loop because count is never changed -> Option A
  4. Quick Check:

    Loop variable must change to avoid infinite loop [OK]
Hint: Update loop variable inside while to avoid infinite loop [OK]
Common Mistakes:
  • Forgetting to change loop variable
  • Assuming loop ends automatically
  • Misreading syntax as error
5.

Write a while loop in C++ that prints all even numbers from 2 to 10 inclusive.

Which code snippet correctly does this?

???
hard
A. int n = 2; while (n <= 10) { std::cout << n << " "; n += 2; }
B. int n = 1; while (n < 10) { if (n % 2 == 0) std::cout << n << " "; n++; }
C. int n = 2; while (n < 10) { std::cout << n << " "; n += 2; }
D. int n = 0; while (n <= 10) { std::cout << n << " "; n += 2; }

Solution

  1. Step 1: Understand the requirement

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

    int n = 2; while (n <= 10) { std::cout << n << " "; n += 2; } starts at 2, increments by 2, and stops at 10 inclusive, printing correct numbers.
  3. Final Answer:

    2 4 6 8 10 -> Option A
  4. Quick Check:

    Start at 2, add 2 each time, stop at 10 [OK]
Hint: Start at 2, add 2, stop at 10 inclusive [OK]
Common Mistakes:
  • Stopping before 10 (using < instead of <=)
  • Starting at 1 and missing even numbers
  • Including 0 which is not in range
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