A loop is used to execute a block of code repeatedly until a specified condition is met.
C++ provides three types of loops:
for loop
while loop
do–while loop
Used when the number of iterations is known.
Used when the number of iterations is not known in advance.
Executes the loop body at least once, even if the condition is false.
| Feature | for | while | do–while |
|---|---|---|---|
| Condition check | Before loop | Before loop | After loop |
| Guaranteed execution | No | No | Yes |
| Best used when | Iterations known | Condition-based | At least one execution |
A loop inside another loop.
| Statement | Purpose |
|---|---|
break | Terminates the loop |
continue | Skips current iteration |
Loops reduce code repetition
Choose the loop based on the problem
do–while runs at least once
Nested loops handle multi-level repetition
Loops are essential in C++ for performing repetitive tasks efficiently and logically.
Take quizzes related to this topic and see where you stand!
Start Quiz Now