For Loop in C Programming: In C Programs, the flow (order) of running all the statements is sequential and if you want to control this flow, then we use conditional statements and loop for that.
We have already read about if, else if …, else conditional statements and in this tutorial we will learn how to use for loop in C programming with examples.
Sometimes we have to repeat some statements in our programs more than once with the same or some changes and for this we use loops.
You can also read: Switch Case Statement In C Programming
Contents
There are three types of loops in C programming:
In this tutorial we will learn to use for loop only and in next tutorials we will learn about while and do while loop.
For Loop in C Programming
What is a for loop and how does a for loop work?
I personally feel that if students understand for loop, then they do not take much time to understand while and do while loop and that is why we are first learning the use of for loop.
The for loop is an entry controlled loop i.e. the condition is checked even before the statements inside the for loop are executed and as long as this condition remains true, the statements inside the for loop keep running.
For how the for loop works, you just have to understand the syntax and its explanation given below.
For Loop in C Programming
for loop syntax execution process:
Initialization:
The for loop starts with variable initialization i.e. the initial value is assigned to the variable in this expression and this step is executed only once.
For Loop in C Programming
for loop Initialization examples:
i = 0;
num = 25;
n = 1;
For Loop in C Programming
Condition:Â
After variable initialization, the second step in the for loop is to have the condition checked. We mostly use relational operators for condition expression.
How many times the for loop will run depends on the condition expression itself. As long as the condition of the for loop remains true, the statements inside the loop will continue to run.
But when the condition of the for loop becomes false then the running of the for loop will stop and the control of the program will go outside the loop.
for loop condition examples:
i<=10;
num >= 5;
i <= n;
x != 5i<=10;
num >= 5;
i <= n;
x != 5
For Loop in C Programming
Update:
When the condition of the for loop becomes true, then the flow of the program goes inside the loop and then all the statements inside the loop are run.
When all the statements of the for loop are run, then the control of the program does not come out of the loop, but it goes on the increment and decrement expression.
We also call this step as variable updation because in this step the variable is updated by incrementing or decrementing it in value.
If you want to increase or decrease only 1 number in the value of the variable in this step, you can use increment and decrement for that.
If you want to update more than 1 number in the value of the variable, then you can use the assignment operator.
For Loop in C Programming
for loop Update examples:
i++;
x--;
n = n + 2;
i = i - 3;
i=i/10;
i=i*5;
i=i%5;
After incrementing or decrementing the variable, the flow of the for loop again goes to the step with the condition and if the condition is true, the statements are run again.
After the statements run, increment or decrement again and so on, the for loop continues until the condition of the for loop becomes false.
For Loop in C Programming
for loop example program:
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
printf("%d ",i);
}
return 0;
}
For Loop in C Programming
Output:
1 2 3 4 5
For Loop in C Programming
Explanation:
Step 1: First of all we initialize the value 1 to the variable i.
Step 2: Now we check this condition whether the value of variable i is less than or equal to 5. Now because the value of i is 1 so our condition (1<=5) will become true.
Step 3: As soon as the condition is true, the flow of the program will go to the body of the loop and with the help of printf function the value of variable i will be printed.
Step 4: When the statement of the body of the for loop is run, the program will flow on updation, where we have incremented the value of the variable i by 1 number.
The value of variable i was 1 earlier which will now increment to 2 and after that again the flow of the program will go to step 2 and check the condition whether the value of variable i is less than or equal to 5.
Now because the value of i is 2 so our condition (2<=5) will again become true and again the flow of our program will go to step 3 and then to step 4 .
This repetition will continue until the condition does not become false at step 2 and this will happen when the value of i is incremented by 5 to 6 and the condition (6<=5) becomes false at step 2 .
infinite loop
As you must be aware that the Hindi meaning of infinite is infinite i.e. never ending.
Now imagine if your loop (for, while, do while) is also such that the condition is never false and its statements keep repeating, we call such a loop infinite loop.
Whenever you use a loop, it is most important to define its condition because if you do not pay much attention while defining the condition, then your loop will become an infinite loop.
I have explained some infinite loop examples below.
for( i=1; i<=5; i-- )
At the beginning of the loop, we have initialized the value of the variable i to be 1, now after that when the condition (1<=5) is checked, it will become true and the control of the program will go inside the loop.
After running a loop statement, when the value of variable i is decremented, then its value will be from 1 to 0 and again the condition (0<=5) will be checked and will become true.
Similarly the value of variable i will always be less than 5 because of decrement (0 to -1, -2, -3, … ) one by one and due to which the condition of loop will never be false.
for( i=10; i>=1; i++ )
In this example, the value of the variable i being incremented one by one (10 to 11, 12, 13, … ) will always be greater than 1 and because of which the loop condition will never be false.
Expression inside for Loop
While using for loop, you can do multiple initialization, condition and updation in loop as per your requirement.
While doing more than one initialization, condition and updation in for loop, some rules are followed, whose list I have given below.
- When more than one variable initialization or updation is done simultaneously, then comma( , ) is used to separate them.
- When more than one condition is defined simultaneously, then use AND ( && ) or OR ( || ) logical operators to separate them.
for( i=1, j=1; i<=5 && j<=5; i++, j++ )
Apart from this, while using for loop, you can skip the initialization, condition or updation expression in the loop as per your requirement.
While skipping any of these three, keep in mind that you do not have to skip its semicolon (;).
For example, if you want to skip the initialization part in the for loop, then you have to put a semicolon (;) before the condition.
for( ; i<=5; i++ )
Similarly, if you want to skip the update part in the for loop, then you have to put a semicolon (;) after the condition.
for( i=1; i<=5; )
Conclusion
I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at pr***********@gm***.com .