22
DecUnderstanding for loop in C
for Loop in C: An Overview
The for loop in C provides functionality. It is nothing but a repetition control structure that allows us to efficiently write a loop that needs to be executed a specific number of times. It contains the initialization, condition, and updating statements as part of its syntax. The for loop is used to traverse arrays, vectors, and other data structures.
In this C Tutorial, we will explore more about for Loop which will include for loop in C, what is for loop in C, for loop in C programming example, and the syntax of for loop in C. First, we will see the Type of loops in C.
Read More - Top 50 C Interview Questions and Answers
Types of Loop in C
Let’s get into the three types of loops used in C programming.- For Loop
- While Loop
- Do While Loop
But right now, we are going to focus only on Loop, So let's discuss it.
What is a for Loop?
- A for loop is a control structure that enables a set of instructions to get executed for a specified number of iterations.
- It is an entry-controlled loop.
- In the for loop, the initialization statement is executed only once.
- After that, the test expression is evaluated.
- If the test expression is evaluated as false, the for loop is terminated.
- However, if the test expression is evaluated as true. The statements inside the body of the for loop are executed, and the update expression is updated.
Flowchart
Syntax
for(initialization; test condition; update expression){
//code to be executed
}
- Here, the
initialization
statement is executed first and only once. - The
test condition
is checked, iffalse
the loop terminates - If the test condition is
true
, the body of the loop executes - The
update expression
gets updated - Again the
test condition
is evaluated - The process repeats until the
test condition
becomesfalse
.
Example: For loop in C Compiler
// Program to print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++)
{
printf("%d\n", i+1);
}
return 0;
}
Output
1
2
3
4
5
6
7
8
9
10
Explanation
The above code prints the numbers from 1 to 10 using a for
loop in C.
- We know it will take 10 iterations to print 10 numbers so, we have used the
for
loop. -
i
is initialized to 0. - The condition
i<10
will be checked. It istrue
, thereforei+1
i.e. 1 gets printed. - Then
i
increments to 1 again the condition,i<10
is evaluated. - The process will repeat until
i
become 10.
Infinite for loop:
- When the loop never stops and keeps running forever, it is called an infinite loop.
- In short, in the for loop, if we don't specify the test condition (check_condition), it's assumed to be true by default, As a result, the condition never becomes false.
- And the loop will keep running forever until we do force-stop the program.
- To avoid all this Don't forget to specify a condition.
- There are two possibilities in the infinite for loop When the given condition is not mentioned and when the given condition is always true. Let's see one by one.
1. When the condition is not mentioned:
Example
#include<stdio.h>
int main()
{
for(int i = 0; ; i++) // When condition is not mentioned
{
printf("%d ",i);
}
return 0;
}
Explanation:
2. When the condition is always true:
Example
#include<stdio.h>
int main()
{
for(int i = 10; i > 0 ; i++) //When test condition is always TRUE
{
printf("%d ",i);
}
return 0;
}
Explanation:
Conclusion:
FAQs
While loop
Nested loop
For loop.
Do-While loop