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" 10,000 times. Typingcout 10,000 times is impossible. Loops solve this by allowing a block of code to run repeatedly as long as a specified condition remains true. They are the backbone of automation in programming.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Use the
forloop for iteration.
-
Use the
whileloop for unknown iteration counts.
-
Use the
do-whileloop for guaranteed first execution.
-
Manipulate loop flow using
breakandcontinue.
- Understand nested loops.
3. The for Loop
Used when you know exactly how many times you want the loop to run.
Syntax: for (initialization; condition; increment/decrement)
cpp
How it works:
-
1.
int i = 1: Executes ONCE at the start.
-
2.
i <= 5: Checked before every loop. If true, run the code. If false, stop.
-
3.
i++: Executes after the code block runs, then goes back to step 2.
4. The while Loop
Used when you don't know how many times the loop will run, but you know the condition that should keep it running.
cpp
5. The do-while Loop
Similar to the while loop, but it guarantees that the code block will execute at least once, because the condition is checked at the *bottom* of the loop.
cpp
6. The break and continue Statements
-
break: Immediately terminates the loop completely.
-
continue: Skips the current iteration and jumps to the next one.
cpp
7. Nested Loops
A loop inside a loop. The inner loop runs completely for every single iteration of the outer loop.
cpp
8. Mini Project: Pattern Generator
cpp
*Output for 4 rows:*
text
9. Memory-Level Explanation
Loops do not duplicate the code in memory. The compiled machine code contains a single block of instructions with a "Jump" command at the end that points the CPU back to the top of the block until the condition register is zero.10. Common Mistakes
-
Infinite Loops: Forgetting
i++in a while loop means the condition never becomes false, crashing/freezing the program.
-
Off-by-One Errors: Using
< 5instead of<= 5when you actually want it to run 5 times starting from 1.
-
Semicolon after for/while:
for (int i=0; i<5; i++); { cout << i; }The semicolon terminates the loop instantly, and the block below it runs only once.
11. Exercises
-
1.
Write a
whileloop that counts backwards from 10 to 1, then prints "Liftoff!".
-
2.
Write a
forloop that prints all even numbers between 1 and 20.
-
3.
Write a
do-whileloop that creates a simple menu system (Press 1 to Play, 2 to Quit).
12. MCQ Quiz with Answers
Question 1
Which loop is best when you know exactly how many iterations you need?
Question 2
Which loop is guaranteed to run at least once?
Question 3
What does the break statement do in a loop?
Question 4
What does the continue statement do?
Question 5
What causes an infinite loop?
for loop?
a) Yes b) No
Answer: a) Yes (e.g., for (int i = 0; ...))
Question 7
What is the output of for(int i=0; i<3; i++) cout << i;?
Question 8
In a nested loop, how many times does the inner loop execute?
Question 9
Which loop evaluates its condition at the bottom?
Question 10
Is for(;;) a valid C++ statement?
13. Interview Questions
-
Q: Explain the difference between
whileanddo-whileloops.
-
Q: Write code to calculate the factorial of a number using a
forloop.
- Q: What is a "Range-based for loop" in Modern C++?
14. Summary
Loops execute code repeatedly. Thefor loop is ideal for known iteration counts. The while loop handles conditional repetition. The do-while loop ensures at least one execution. break and continue give fine-grained control over the loop's execution flow.