CHAPTER 08
Beginner
Loops in C
Updated: May 17, 2026
5 min read
# CHAPTER 8
Loops in C
1. Introduction
Imagine needing to print "Hello" 1,000 times. Writingprintf a thousand times is terrible programming. Loops allow you to execute a block of code repeatedly as long as a specified condition is true. Loops are the workhorses of algorithms and data processing.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Use the
whileloop for unknown iteration counts.
-
Use the
do-whileloop for guaranteed execution.
-
Use the
forloop for counted iterations.
-
Control loops using
breakandcontinue.
- Write nested loops.
3. The while Loop
The while loop executes a block of code as long as a condition is true. It checks the condition *before* executing the body.
c
Best for: When you don't know exactly how many times the loop should run (e.g., reading a file until the end).
4. The do-while Loop
Similar to the while loop, but it checks the condition *after* executing the body. This guarantees the loop runs at least once.
c
5. The for Loop
The for loop is best when you know exactly how many times you want to iterate. It combines initialization, condition, and increment into one neat line.
Syntax: for (initialization; condition; increment/decrement)
c
6. Control Statements: break and continue
-
break: Immediately exits the entire loop.
-
continue: Skips the rest of the current iteration and jumps to the next one.
c
7. Nested Loops
A loop inside another loop. Often used for 2D graphics or matrices.
c
*Output:*
8. Mini Project: Pattern Printing App
Let's build a program that prints a right-angled triangle using stars.
c
*Output (if rows = 4):*
9. Memory-Level Explanation
When a loop runs, the CPU repeatedly executes the same block of machine code in memory. The CPU's Instruction Pointer (IP) jumps back to the memory address at the start of the loop as long as the condition evaluates to non-zero.10. Common Mistakes
-
Infinite Loops: Forgetting to update the loop variable (e.g., forgetting
count++).
-
Off-by-one errors: Using
<=instead of<(or vice versa), causing the loop to run one time too many or too few.
-
Semicolon after
for:for(int i=0; i<5; i++); { printf("Hi"); }The semicolon terminates the loop immediately. "Hi" prints only once!
11. Exercises
-
1.
Write a
forloop to print all even numbers from 2 to 20.
-
2.
Write a
whileloop that asks the user to enter a positive number. If they enter a negative number, keep asking.
- 3. Write a program to calculate the factorial of a number using a loop.
12. MCQ Quiz with Answers
Question 1
Which loop is guaranteed to execute at least once?
Question 2
What causes an infinite loop?
Question 3
What does the break statement do?
Question 4
What does the continue statement do?
Question 5
In for(int i=0; i<5; i++), how many times does the loop run?
Question 6
What happens here: while(1) { }?
Question 7
Is for(;;) a valid statement in C?
Question 8
Which loop is best when you know the exact number of iterations?
Question 9
Nested loops mean:
Question 10
What is the output of for(int i=0; i<3; i++); printf("%d", i); ?
13. Interview Questions
-
Q: What is the difference between
whileanddo-whileloops?
- Q: How do you break out of a nested loop completely?
- Q: What is an off-by-one error, and how do you avoid it?
14. Summary
Loops execute code repeatedly. Usewhile for condition-based iteration, do-while when you need at least one execution, and for when counting iterations. break and continue offer fine-grained control over loop execution.