Recall & Review
beginner Click to reveal answer
What is the basic structure of a for loop in C?
A for loop in C has three parts inside parentheses: initialization, condition, and update. It looks like:
for (initialization; condition; update) {
// code to repeat
}beginner Click to reveal answer
What happens if the condition in a for loop is false at the start?
The code inside the for loop does not run at all. The loop ends immediately.
beginner Click to reveal answer
How can you use a for loop to print numbers from 1 to 5?
You can write:
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
} This prints numbers 1, 2, 3, 4, 5 each on a new line.intermediate Click to reveal answer
Can the update part of a for loop be empty?
Yes, the update part can be empty, but you must update the loop variable inside the loop body to avoid an infinite loop.
intermediate Click to reveal answer
What is an infinite loop and how can it happen with a for loop?
An infinite loop happens when the loop's condition never becomes false. For example, if you forget to update the loop variable, the loop runs forever.
Which part of the for loop runs first?
✗ Incorrect
The initialization runs first, setting up the loop variable before checking the condition.
What happens if the condition in a for loop is false initially?
✗ Incorrect
If the condition is false at the start, the loop body does not run at all.
Which of these is a correct for loop header to count from 0 to 9?
✗ Incorrect
Option A correctly counts from 0 up to 9 by increasing i each time.
What must you do if you leave the update part empty in a for loop?
✗ Incorrect
If the update is empty, you must update the loop variable inside the loop body to avoid infinite loops.
What is the output of this code?
for (int i = 1; i <= 3; i++) { printf("%d", i); }
for (int i = 1; i <= 3; i++) { printf("%d", i); }
✗ Incorrect
The loop prints 1, then 2, then 3 without spaces, so output is '123'.
Explain the three parts of a for loop and their roles.
Think about what happens first, what keeps the loop running, and how the loop variable changes.
You got /4 concepts.
Describe how a for loop can cause an infinite loop and how to prevent it.
Focus on the condition and updating the loop variable.
You got /4 concepts.
