C++ Loops: while, for, do-while & Range-based Guide
Module 4: Loops in C++
Loops allow a program to execute the same block of code repeatedly until a specified condition becomes false. They help reduce repetitive code and make programs more efficient.
For example, instead of writing cout << "Hello"; ten times, you can use a loop to print it ten times automatically.
Types of Loops in C++
C++ provides four commonly used loops:
whiledo...whilefor- Range-based
for
1. while Loop
The while loop executes a block of code as long as the condition is true. The condition is checked before each iteration.
Syntax
1while (condition) 2{ 3 // Code to execute 4}
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int i = 1; 8 9 while (i <= 5) 10 { 11 cout << i << " "; 12 i++; 13 } 14 15 return 0; 16}
Output
11 2 3 4 5
2. do...while Loop
The do...while loop executes the code block at least once, then checks the condition.
Syntax
1do 2{ 3 // Code 4} 5while (condition);
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int i = 1; 8 9 do 10 { 11 cout << i << " "; 12 i++; 13 } 14 while (i <= 5); 15 16 return 0; 17}
Output
11 2 3 4 5
Difference Between while and do...while
while | do...while |
|---|---|
| Condition checked first | Condition checked after execution |
| May execute zero times | Executes at least once |
3. for Loop
The for loop is commonly used when the number of iterations is known.
Syntax
1for (initialization; condition; update) 2{ 3 // Code 4}
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 for (int i = 1; i <= 5; i++) 8 { 9 cout << i << " "; 10 } 11 12 return 0; 13}
Output
11 2 3 4 5
4. Range-based for Loop
The range-based for loop is used to iterate through all elements of an array or container.
Syntax
1for (dataType variable : container) 2{ 3 // Code 4}
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 int numbers[] = {10, 20, 30, 40, 50}; 8 9 for (int number : numbers) 10 { 11 cout << number << " "; 12 } 13 14 return 0; 15}
Output
110 20 30 40 50
5. Nested Loops
A nested loop is a loop inside another loop. It is useful for working with tables, matrices, and pattern printing.
Example
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 for (int i = 1; i <= 3; i++) 8 { 9 for (int j = 1; j <= 3; j++) 10 { 11 cout << "* "; 12 } 13 14 cout << endl; 15 } 16 17 return 0; 18}
Output
1* * * 2* * * 3* * *
Pattern Printing
Pattern 1: Square
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 for (int i = 1; i <= 4; i++) 8 { 9 for (int j = 1; j <= 4; j++) 10 { 11 cout << "* "; 12 } 13 14 cout << endl; 15 } 16 17 return 0; 18}
Output
1* * * * 2* * * * 3* * * * 4* * * *
Pattern 2: Right Triangle
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 for (int i = 1; i <= 5; i++) 8 { 9 for (int j = 1; j <= i; j++) 10 { 11 cout << "* "; 12 } 13 14 cout << endl; 15 } 16 17 return 0; 18}
Output
1* 2* * 3* * * 4* * * * 5* * * * *
Pattern 3: Number Triangle
1#include <iostream> 2 3using namespace std; 4 5int main() 6{ 7 for (int i = 1; i <= 5; i++) 8 { 9 for (int j = 1; j <= i; j++) 10 { 11 cout << j << " "; 12 } 13 14 cout << endl; 15 } 16 17 return 0; 18}
Output
11 21 2 31 2 3 41 2 3 4 51 2 3 4 5
Best Practices
- Choose the appropriate loop for your task.
- Ensure the loop condition eventually becomes false.
- Avoid unnecessary nested loops for better performance.
- Use meaningful loop variable names.
- Keep loop bodies simple and readable.
Common Mistakes
Infinite Loop
1while (true) 2{ 3 cout << "Hello"; 4}
This loop never ends unless explicitly terminated with break or another exit condition.
Missing Update Statement
1int i = 1; 2 3while (i <= 5) 4{ 5 cout << i; 6}
This creates an infinite loop because i is never incremented.
Correct version:
1int i = 1; 2 3while (i <= 5) 4{ 5 cout << i; 6 i++; 7}
Practice Exercises
- Print numbers from 1 to 100 using a
whileloop. - Display the multiplication table of a number using a
forloop. - Print all even numbers between 1 and 50.
- Calculate the sum of the first 100 natural numbers.
- Print a pyramid pattern using nested loops.
- Traverse an array using a range-based
forloop.
Interview Questions
- What is a loop in C++?
- What is the difference between
whileanddo...while? - When should you use a
forloop? - What is a range-based
forloop? - What is a nested loop?
- What causes an infinite loop?
- How can you avoid infinite loops?
Summary
In this module, you learned:
- How the
whileloop works. - Why the
do...whileloop always executes at least once. - How to use the
forloop for fixed iterations. - How the range-based
forloop simplifies iterating over arrays and containers. - How nested loops are used for tables and pattern printing.
- How to create simple star and number patterns.
- Common loop-related mistakes and best practices.
In the next module, you'll learn Functions in C++, including function declaration, definition, parameters, return values, recursion, function overloading, inline functions, default arguments, and lambda expressions.