C for loop
Contents
Understanding Looping Statement :
Whenever we need to execute certain action multiple times, we need to wrap programming statement in the loop body. Take an example we are computing the sum of 2 numbers 5 times then we need to write code in the loop body. Consider the following trailer -
for(i=0; i<5; i++) { printf("\nEnter two Numbers : "); scanf("%d %d",num1,num2); ans = num1 + num2; printf("\nAddition of 2 Numbers is %d"); }
Syntax of the For Loop :
for(initial expression; test expression; update expression) { body of loop; }
We will get better idea after looking at above for loop flowchart.
Flowchart of For Loop :

Explanation of For Loop :
- Firstly the for loop executes the initialize statement in which the subscript variable will be initialized with the initial value.
- After the initialize statement the condition part of the loop will be executed if the condition becomes true then body of the loop will be executed otherwise the loop will be terminated
- If the loop condition becomes true then body of the loop will be executed. After the execution of the for loop body the control goes to the third part of the loop statement i.e Expression Updation
- After updating subscript variable control again goes to execute condition statement.
For Loop : Dry Run
Consider the following image then -

Sequence of the execution will be as below -
| Seq. No | Statement Flow | Explanation |
|---|---|---|
| 01 | Flow No 1 will be executed | i = 0 |
| 02 | Flow No 2 will be executed | Condition Checking |
| 03 | Flow No 3 will be executed | True Condition |
| 04 | Flow No 4 will be executed | - |
| 05 | Flow No 5 will be executed | i = 1 |
| 06 | Flow No 3 will be executed | True Condition |
| 07 | Flow No 4 will be executed | - |
| 08 | Flow No 5 will be executed | i = 2 |
| 09 | Flow No 3 will be executed | True Condition |
| 10 | Flow No 4 will be executed | - |
| 11 | Flow No 5 will be executed | i = 3 |
| 12 | Flow No 3 will be executed | True Condition |
| 13 | Flow No 4 will be executed | - |
| 14 | Flow No 5 will be executed | i = 4 |
| 15 | Flow No 3 will be executed | True Condition |
| 16 | Flow No 4 will be executed | - |
| 17 | Flow No 5 will be executed | i = 5 |
| 18 | Flow No 3 will be executed | False Condition |
Note :
- For Single Line of Code - Opening and Closing braces are not needed.
- There can Exist For Loop without body.
- Initialization , Incrementation and Condition steps are on same Line.
- Like While loop , For Loop is Entry Controlled Loop.[i.e conditions are checked if found true then and then only code is executed ]
