C++ For Loop
Last Updated : 10 Jan 2026
In C++, the for loop is an entry-controlled loop that is mainly utilized to iterate a part of the program multiple times. If the number of iterations is fixed, it is recommended to use for loop than while or do-while loops.
Syntax
The C++ for loop is the same as other languages, such as C and C#. We can initialize variables, check conditions, and increment/decrement values.
- Initialization: It initializes the variables and is executed only once.
- Condition Statement: The condition statement checks the given condition. If the condition is true, it executes the of for loop. If the condition is false, it terminates the for loop.
- Update Expression: After execution of the loop body, this expression increments or decrements the loop variable by 1.
Flowchart of for Loop

As we can see in the flowchart, use the following steps to implement the for loop in C++:
Step 1: First, go to the for loop and initialize the variables and their values.
Step 2: Next, the control flow jumps to the condition statement.
Step 3: Here, the condition is tested:
- If the condition is true, it executes the loop body.
- If the condition is false, it terminates the loop body and moves to the next iteration.
Step 4: After that, it goes to the update expression, where it increases or decreases the value by 1, and then again, it goes to step 3 to check the condition.
Step 5: At the end, it exits from the loop and prints the output.
Simple C++ for Loop Example
Let us take a simple example to print the numbers 1 to 10 using the C++ for loop.
Output:
Factorial Program using C++ for Loop
Let us take another example to find the factorial of a given number using a for loop in C++.
Output:
Enter a positive number: 6 The factorial of 6 is: 720
Multiplication table program using for loop in C++
Let us take an example to print a multiplication table using for loop in C++.
Output:
Enter a Number: 4 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 4 * 10 = 40
C++ Nested for Loop
In C++, if we have a for loop inside another for loop, it is known as a nested for loop. The inner loop is completely executed when the outer loop executes.
It allows us to perform complex iterations by executing a for loop for every iteration of the outer for loop. Nested for loops are beneficial when working with multi-dimensional arrays or patterns.
Syntax:
It has the following syntax:
Flowchart of Nested for loop:

C++ Nested for Loop Example:
Let us take a simple example of a nested for loop in C++.
Output:
1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
C++ Pyramid Program using Nested for Loop:
Let us take an example to print a pyramid pattern using nested for loop in C++.
Output:
Enter the Number of rows: 6 * * * * * * * * * * * * * * * * * * * * *
Explanation:
In this example, we have taken two for loops. Here, the first loop handles the number of rows, and the second loop handles the number of columns and prints the stars.
C++ Inverted Pyramid Program using Nested for Loop:
Let us take an example to print an inverted pyramid pattern using nested for loop in C++.
Output:
Enter the Number of rows: 5 * * * * * * * * * * * * * * *
Explanation:
In this example, we have taken two for loops. Here, the outer loop handles the number of rows, and the inner loop handles the number of stars per row.
C++ foreach Loop
The foreach loop is also known as the range-based for loop. It is mainly utilized to iterate over the elements of a container (such as list, array, vector, etc) in a simple way without performing initialization, increment/decrement, and testing. The 'for' keyword is utilized for the foreach loop in C++.
Syntax:
It has the following syntax:
In this syntax,
- Variable_name: It is a variable name that is provided to the elements stored in a container.
- Container_name: It is a variable name of a container of any type, including list, array, vector, and many others.
C++ Foreach Loop Example:
Let us take an example to illustrate the foreach loop in C++.
Output:
Explanation:
In this example, we have taken a vector library for using dynamic arrays. Next, we declare a vector of integers. After that, we use two for loops in this program. The first loop uses a reference to modify the vector elements, and the second loop prints the modified values.
C++ Infinite for Loop
In C++, if we remove the initialization, condition, and update expression, it will result in an infinite loop.
In other words, if we use a double semicolon in the for loop, it will be executed infinite times.
Syntax:
It has the following syntax:
C++ Infinite For Loop Example:
Let us take an example to illustrate the infinite for loop in C++.
Output:
Hello! Tpointtech Hello! Tpointtech Hello! Tpointtech Hello! Tpointtech
Here, we need to press Ctrl+C to exit from the infinitive program.