C++ for Loop Examples
π C++ for Loop Examples
Below are clear and practical examples of the C++ for loop, from basic to advanced.
1. Print Numbers from 1 to 5
Output:
2. Print Even Numbers (1 to 10)
Output:
3. Print Numbers in Reverse Order
Output:
4. Sum of Numbers from 1 to 10
Output:
5. Multiplication Table of a Number
6. Loop Through an Array
7. Range-Based for Loop (Modern C++)
Output:
8. Nested for Loop Example
Output:
9. Print Characters of a String
Output:
10. for Loop Without Initialization Inside
Common Mistake β
β Correct:
Key Points
-
Use
forloop when the number of iterations is known -
Initialization, condition, and update are in one line
-
Supports nested loops and range-based loops
-
Clean and efficient for counting-based tasks
