A for loop is a control structure that repeats a block of code a set number of times. It has three parts: initialization, condition, and update.
Click to reveal answer
beginner
Write the basic syntax of a for loop in C++.
for (initialization; condition; update) { // code to repeat }
Click to reveal answer
beginner
What happens if the condition in a for loop is false at the start?
The loop body does not run at all, and the program continues after the loop.
Click to reveal answer
beginner
How can you use a for loop to print numbers from 1 to 5?
for (int i = 1; i <= 5; i++) { std::cout << i << std::endl; }
Click to reveal answer
beginner
What is the role of the update expression in a for loop?
The update expression changes the loop variable each time the loop runs, usually to move toward the loop ending condition.
Click to reveal answer
Which part of the for loop runs first?
AInitialization
BCondition check
CUpdate expression
DLoop body
✗ Incorrect
The initialization runs first to set up the loop variable before checking the condition.
What happens if the condition in a for loop is always true?
AThe loop runs once
BThe loop runs forever unless broken
CThe loop never runs
DThe program crashes
✗ Incorrect
If the condition never becomes false, the loop repeats endlessly until a break or exit.
Which symbol is used to separate the parts of a for loop?
A;
B,
C:
D.
✗ Incorrect
Semicolons separate initialization, condition, and update in a for loop.
How do you increase the loop variable by 1 each time?
Ai = i - 1
Bi--
Ci++
Di = 0
✗ Incorrect
The i++ expression adds 1 to i each time the loop runs.
What will this loop print? for(int i=0; i<3; i++) { std::cout << i; }
A321
B123
C000
D012
✗ Incorrect
The loop prints 0, then 1, then 2, all together as 012.
Explain how a for loop works in C++ and describe its three main parts.
Think about how the loop starts, checks if it should continue, runs code, and then changes the variable.
You got /5 concepts.
Write a for loop that counts down from 5 to 1 and prints each number.
Start with i = 5, stop when i is less than 1, and decrease i each time.
You got /5 concepts.
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
Step 1: Understand the purpose of a for loop
A for loop is designed to repeat code multiple times based on a condition.
Step 2: Identify the correct description
Among the options, only repeating code a fixed number of times matches the for loop's behavior.
Final Answer:
Repeats a block of code a fixed number of times -> Option D
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
Step 1: Check initialization syntax in for loop
The initialization part must declare and initialize the loop variable, e.g., int i = 0.
Step 2: Compare options for correctness
Only int i = 0 correctly declares and initializes the variable.
Final Answer:
int i = 0 -> Option A
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
Step 1: Identify loop iterations
The loop runs with i = 1, 2, 3 because condition is i <= 3.
Step 2: Calculate output for each iteration
Outputs are i*2: 2, 4, 6 printed with spaces.
Final Answer:
2 4 6 -> Option B
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
Step 1: Analyze the loop condition
The condition is i > 5 but i starts at 0, so it is false immediately.
Step 2: Understand loop execution
Since condition is false at start, loop body never executes.
Final Answer:
The condition 'i > 5' is incorrect, loop never runs -> Option C
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
Step 1: Understand the goal
We want to print even numbers 2,4,6,8,10 inclusive.
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.
Final Answer:
for (int i = 2; i <= 10; i += 2) { std::cout << i << ' '; } -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.