C Programming for loop



In the previous chapter we have learnt if-else statements in C and different operators and expressions. In this tutorial we will be learning more about C Programming for loop.

C Programming for loop

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("\nC Programming for loop");
}

In the above program we have printed the string C Programming for loop 5 times.

Syntax

for(initial expression; test expression; update expression) 
{ 
body of loop; 
}
Part of Loop Explanation
initial expression It is initial expression used to initialize subscript/loop variable
test expression Expression decides whether to go inside the loop or not
update expression Updating the loop variable for next iteration of loop

Flowchart of For Loop

We will get better idea after looking at above for loop flowchart.

For Loop Statement in C Programming
Explanation of For Loop :

  1. Firstly the for loop executes the initialize statement in which the subscript variable will be initialized with the initial value.
  2. 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
  3. 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
  4. After updating subscript variable control again goes to execute condition statement.

For Loop : Dry Run

Consider the following image to learn more about c programming for loop –
For Loop Statement in C Programming - Explanation
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

Summary

  1. For single line of code – Opening and closing braces are not needed in c programming for loop.
  2. There can exist for loop without body.
  3. Initialization , Incrementation and Condition steps are on same Line.
  4. Like While loop , C programming for loop is entry controlled loop. i.e conditions are checked if found true then and then only code is executed

We will be learning different types of c programming for loops in next tutorial