Loops in C/C++ come into use when we need to repeatedly execute a block of statements.
For loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line.
Syntax:
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
The various parts of the For loop are:
- Initialization Expression: In this expression we have to initialize the loop counter to some value.
Example:int i=1;
- Condition: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the for loop.
Example:i <= 10
- Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value.
Example:i++;
How does a For loop executes?
- Control falls into the for loop. Initialization is done
- The flow jumps to Condition
- Condition is tested.
- If Condition yields true, the flow goes into the Body
- If Condition yields false, the flow goes outside the loop
- The statements inside the body of the loop get executed.
- The flow goes to the Updation
- Updation takes place and the flow goes to Step 3 again
- The for loop has ended and the flow has gone outside.
Flow chart for loop (For Control Flow):

Example 1: This program will try to print “Hello World” 5 times. The program will execute in the following manner:
C
// C program to illustrate for loop #include <stdio.h> int main() { int i = 0; // Writing a for loop // to print Hello World 5 times for (i = 1; i <= 5; i++) { printf("Hello World\n"); } return 0; } |
C++
// C++ program to illustrate for loop #include <iostream> using namespace std; int main() { // Writing a for loop // to print Hello World 5 times for (int i = 1; i <= 5; i++) { cout << "Hello World\n"; } return 0; } |
Hello World Hello World Hello World Hello World Hello World
Dry-Running Example 1:
1. Program starts. 2. i is initialized with value 1. 3. Condition is checked. 1 <= 5 yields true. 3.a) "Hello World" gets printed 1st time. 3.b) Updation is done. Now i = 2. 4. Condition is checked. 2 <= 5 yields true. 4.a) "Hello World" gets printed 2nd time. 4.b) Updation is done. Now i = 3. 5. Condition is checked. 3 <= 5 yields true. 5.a) "Hello World" gets printed 3rd time 5.b) Updation is done. Now i = 4. 6. Condition is checked. 4 <= 5 yields true. 6.a) "Hello World" gets printed 4th time 6.b) Updation is done. Now i = 5. 7. Condition is checked. 5 <= 5 yields true. 7.a) "Hello World" gets printed 5th time 7.b) Updation is done. Now i = 6. 8. Condition is checked. 6 <= 5 yields false. 9. Flow goes outside the loop to return 0
Example 2:
C
// C program to illustrate for loop #include <stdio.h> int main() { int i = 0; // Writing a for loop // to print odd numbers upto N for (i = 1; i <= 10; i += 2) { printf("%d\n", i); } return 0; } |
C++
// C++ program to illustrate for loop #include <iostream> using namespace std; int main() { int i = 0; // Writing a for loop // to print odd numbers upto N for (i = 1; i <= 10; i += 2) { cout << i << "\n"; } return 0; } |
1 3 5 7 9
Related Articles:
- Loops in C and C++
- C/C++ while loop with Examples
- C/C++ do while loop with Examples
- Difference between while and do-while loop in C, C++, Java
- Difference between for and while loop in C, C++, Java
Attention reader! Don't stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java For loop with Examples
- Java while loop with Examples
- C/C++ while loop with Examples
- Java do-while loop with Examples
- C/C++ do while loop with Examples
- Reversed Range-based for loop in C++ with Examples
- How will you print numbers from 1 to 100 without using loop?
- A nested loop puzzle
- Print 1 to 100 in C++, without loop and recursion
- Print substring of a given string without using any string function and loop in C
- How to concatenate two integer arrays without using loop in C ?
- What happens if loop till Maximum of Signed and Unsigned in C/C++?
- Print pattern using only one loop | Set 1 (Using setw)
- for_each loop in C++
- Print the pattern by using one loop | Set 2 (Using Continue Statement)
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- Range-based for loop in C++
- How to print a number 100 times without using loop and recursion in C?
- Print a number 100 times without using loop, recursion and macro expansion in C?
- How will you print numbers from 1 to 100 without using loop? | Set-2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


