Loops help repeat actions many times without writing the same code again and again.
Loop execution flow
for (initialization; condition; update) { // code to repeat } while (condition) { // code to repeat } do { // code to repeat } while (condition);
Initialization runs once at the start.
Condition is checked before each loop cycle (except in do-while).
for (int i = 0; i < 5; i++) { printf("%d\n", i); }
int i = 0; while (i < 3) { printf("Hello\n"); i++; }
int i = 0; do { printf("World\n"); i++; } while (i < 2);
This program shows how three types of loops work in C. It counts up with a for loop, counts down with a while loop, and shows do-while runs at least once.
#include <stdio.h> int main() { int count = 1; printf("Counting from 1 to 5:\n"); for (int i = 1; i <= 5; i++) { printf("%d\n", i); } printf("\nUsing while loop to count down from 3:\n"); int j = 3; while (j > 0) { printf("%d\n", j); j--; } printf("\nUsing do-while loop to print once even if condition false:\n"); int k = 0; do { printf("This prints once even if k is %d\n", k); k--; } while (k > 0); return 0; }
Loops run the code inside repeatedly as long as the condition is true.
For loops are great when you know how many times to repeat.
While loops are good when you repeat until something changes.
Do-while loops always run the code at least once.
Loops repeat code to save writing the same steps many times.
For, while, and do-while loops have different ways to control repetition.
Understanding loop flow helps you control how many times code runs.
Practice
Solution
Step 1: Understand loop types
Thedo-whileloop checks its condition after running the loop body once.Step 2: Compare with other loops
Theforandwhileloops check the condition before running the body, so they may not run at all.Final Answer:
do-while loop -> Option AQuick Check:
do-while runs body once before condition check [OK]
- Thinking for or while loops always run once
- Confusing if statement with loops
- Assuming do-while checks condition first
Solution
Step 1: Check for loop syntax
The correct syntax isfor (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 CQuick Check:
Standard for loop syntax with i from 0 to 4 [OK]
- Using 'for i = 0 to 4' which is not C syntax
- Swapping condition and initialization
- Using <= 5 to count 0 to 4 (includes 5)
int i = 0;
while (i < 3) {
printf("%d ", i);
i++;
}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 AQuick Check:
while loop prints 0,1,2 then stops [OK]
- Including 3 in output by mistake
- Starting count from 1 instead of 0
- Printing after increment instead of before
int i = 0;
while (i < 5) {
printf("%d ", i);
}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 BQuick Check:
Infinite loop due to no i++ [OK]
- Thinking condition is wrong instead of missing increment
- Assuming printf format is incorrect
- Declaring i inside loop is optional, not error
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}What is the output?
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 DQuick Check:
Nested loops print increasing sequences per line [OK]
- Confusing inner loop range with outer loop
- Printing j values incorrectly
- Missing newline after each inner loop
Ready to prove it?
Pass the test and this pattern moves unknown
