Recall & Review
beginner
What is a
while loop in C?A
while loop repeatedly executes a block of code as long as a given condition is true.Click to reveal answer
beginner
How does the
while loop check its condition?Before each iteration, the
while loop checks the condition. If it is true, the loop runs the code inside; if false, it stops.Click to reveal answer
beginner
What happens if the
while loop condition is false at the start?The loop body does not run even once if the condition is false initially.
Click to reveal answer
beginner
Write a simple
while loop that prints numbers 1 to 3.int i = 1;
while (i <= 3) {
printf("%d\n", i);
i++;
}
Click to reveal answer
beginner
Why is it important to update the loop variable inside a
while loop?Updating the loop variable prevents an infinite loop by eventually making the condition false.
Click to reveal answer
What does a
while loop do in C?✗ Incorrect
A
while loop repeats the code block as long as the condition remains true.When is the condition in a
while loop checked?✗ Incorrect
The condition is checked before running the loop body each time.
What happens if the loop condition is false at the start?
✗ Incorrect
If the condition is false initially, the loop body is skipped.
Which of these can cause an infinite
while loop?✗ Incorrect
If the loop variable is not updated, the condition may never become false, causing an infinite loop.
What is the output of this code?
int i = 1;
while (i <= 2) {
printf("%d ", i);
i++;
}
✗ Incorrect
The loop prints 1 and 2, then stops when i becomes 3.
Explain how a
while loop works in C and why updating the loop variable is important.Think about what happens if the condition never changes.
You got /3 concepts.
Write a simple
while loop in C that counts down from 5 to 1 and prints each number.Start with int i = 5 and decrease i each time.
You got /4 concepts.