What is the main purpose of a loop in C programming?
A loop repeats a block of code multiple times until a condition is no longer true.
Click to reveal answer
beginner
Describe the flow of a for loop in C.
A for loop starts by initializing a variable, then checks a condition before each iteration, executes the loop body if true, and updates the variable after each iteration.
Click to reveal answer
beginner
What happens if the loop condition is false at the start of a while loop?
The loop body does not execute even once, and the program continues after the loop.
Click to reveal answer
intermediate
How does a do-while loop differ in execution flow from a while loop?
A do-while loop executes the loop body first, then checks the condition, so it always runs at least once.
Click to reveal answer
intermediate
What is the role of the break statement inside a loop?
The break statement immediately stops the loop and transfers control to the code after the loop.
Click to reveal answer
What part of a for loop runs only once at the beginning?
AInitialization
BCondition check
CLoop body
DIncrement/Update
✗ Incorrect
The initialization runs once before the loop starts.
When does the condition in a while loop get checked?
AOnly once at the start
BAfter each iteration
CBefore each iteration
DNever
✗ Incorrect
The while loop checks the condition before running the loop body each time.
Which loop guarantees the body runs at least once?
A<code>while</code> loop
B<code>do-while</code> loop
C<code>for</code> loop
DNone of these
✗ Incorrect
The do-while loop runs the body first, then checks the condition.
What happens if the loop condition is false at the start of a for loop?
ALoop body runs once
BProgram crashes
CLoop runs infinitely
DLoop body does not run
✗ Incorrect
If the condition is false initially, the loop body does not execute.
What does the break statement do inside a loop?
AEnds the loop immediately
BPauses the loop
CRestarts the loop
DSkips current iteration
✗ Incorrect
The break statement stops the loop right away.
Explain the execution flow of a for loop in C.
Think about what happens first, then what repeats.
You got /5 concepts.
Describe the difference in execution flow between a while loop and a do-while loop.
One checks condition before running, the other after.
You got /3 concepts.
Practice
(1/5)
1. Which loop in C always executes its body at least once, regardless of the condition?
easy
A. do-while loop
B. for loop
C. while loop
D. if statement
Solution
Step 1: Understand loop types
The do-while loop checks its condition after running the loop body once.
Step 2: Compare with other loops
The for and while loops check the condition before running the body, so they may not run at all.
Final Answer:
do-while loop -> Option A
Quick Check:
do-while runs body once before condition check [OK]
Hint: do-while runs body first, then checks condition [OK]
Common Mistakes:
Thinking for or while loops always run once
Confusing if statement with loops
Assuming do-while checks condition first
2. Which of the following is the correct syntax for a for loop in C that counts from 0 to 4?
easy
A. for (i < 5; i = 0; i++) {}
B. for i = 0 to 4 {}
C. for (int i = 0; i < 5; i++) {}
D. for (int i = 0; i <= 5; i++) {}
Solution
Step 1: Check for loop syntax
The correct syntax is for (initialization; condition; increment). for (int i = 0; i < 5; i++) {} matches this.
Step 2: Validate loop range
for (int i = 0; i < 5; i++) {} counts i from 0 up to but not including 5, so 0 to 4.
Final Answer:
for (int i = 0; i < 5; i++) {} -> Option C
Quick Check:
Standard for loop syntax with i from 0 to 4 [OK]
Hint: for loops use (init; condition; increment) syntax [OK]
Common Mistakes:
Using 'for i = 0 to 4' which is not C syntax
Swapping condition and initialization
Using <= 5 to count 0 to 4 (includes 5)
3. What is the output of this C code?
int i = 0;
while (i < 3) {
printf("%d ", i);
i++;
}
medium
A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. 3 2 1 0
Solution
Step 1: Analyze loop condition and variable
Variable i starts at 0 and increments by 1 each loop while i < 3.
Step 2: Trace output values
Loop runs for i = 0, 1, 2 printing each value followed by a space.
Final Answer:
0 1 2 -> Option A
Quick Check:
while loop prints 0,1,2 then stops [OK]
Hint: Check loop condition carefully to count printed values [OK]
Common Mistakes:
Including 3 in output by mistake
Starting count from 1 instead of 0
Printing after increment instead of before
4. Identify the error in this loop code:
int i = 0;
while (i < 5) {
printf("%d ", i);
}
medium
A. Incorrect printf format specifier
B. Missing increment of i inside the loop
C. Condition should be i <= 5
D. Variable i should be declared inside the loop
Solution
Step 1: Check loop body for changes to i
The variable i is never incremented inside the loop, so i stays 0 forever.
Step 2: Understand loop consequence
Without increment, the condition i < 5 is always true, causing an infinite loop.
Final Answer:
Missing increment of i inside the loop -> Option B
Quick Check:
Infinite loop due to no i++ [OK]
Hint: Always update loop variable to avoid infinite loops [OK]
Common Mistakes:
Thinking condition is wrong instead of missing increment
Assuming printf format is incorrect
Declaring i inside loop is optional, not error
5. Consider this nested loop:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
What is the output?
hard
A. 1
22
333
B. 1
2
3
C. 123
123
123
D. 1
12
123
Solution
Step 1: Understand outer loop iterations
The outer loop runs i from 1 to 3, inclusive, so 3 times.
Step 2: Trace inner loop for each i
For each i, inner loop runs j from 1 to i, printing j values without spaces.
Step 3: Combine outputs
Lines printed are: i=1: "1" i=2: "12" i=3: "123" Each followed by newline.
Final Answer:
1
12
123
-> Option D
Quick Check:
Nested loops print increasing sequences per line [OK]
Hint: Inner loop runs up to outer loop counter each time [OK]
Common Mistakes:
Confusing inner loop range with outer loop
Printing j values incorrectly
Missing newline after each inner loop
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.