Recall & Review
beginner
What is a for loop in C++?
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) {<br> // code to repeat<br>}
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++) {<br> std::cout << i << std::endl;<br>}
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?
✗ 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?
✗ 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?
✗ Incorrect
Semicolons separate initialization, condition, and update in a for loop.
How do you increase the loop variable by 1 each time?
✗ 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; }
✗ 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.