For Loop in C
For Loop in C
For Loop in C
Most programming languages including C support the for keyword for constructing a
loop. In C, the other loop-related keywords are while and do-while. Unlike the
other two types, the for loop is called an automatic loop, and is usually the first
choice of the programmers.
The for loop is an entry-controlled loop that executes the statements till the given
condition. All the elements (initialization, test condition, and increment) are placed
together to form a for loop inside the parenthesis with the for keyword.
The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as
long as a semicolon appears.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is
false, the body of the loop does not execute and the control jumps to the next
statement just after the "for" loop.
After the body of the "for" loop executes, the control flow jumps back up to the
increment statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after the
condition.
The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again the condition).
After the condition becomes false, the "for" loop terminates.
https://www.tutorialspoint.com/cprogramming/c_for_loop.htm 1/9
6/16/24, 11:57 AM For Loop in C
Developers prefer to use for loops when they know in advance how many number of
iterations are to be performed. It can be thought of as a shorthand for while and
do-while loops that increment and test a loop variable.
The for loop may be employed with different variations. Let us understand how the
for loop works in different situations.
#include <stdio.h>
int main(){
int a;
https://www.tutorialspoint.com/cprogramming/c_for_loop.htm 2/9
6/16/24, 11:57 AM For Loop in C
return 0;
}
Output
a: 1
a: 2
a: 3
a: 4
a: 5
Example
#include <stdio.h>
int main(){
int a = 1;
Output
https://www.tutorialspoint.com/cprogramming/c_for_loop.htm 3/9
6/16/24, 11:57 AM For Loop in C
a: 1
a: 2
a: 3
a: 4
a: 5
Example
#include <stdio.h>
int main(){
int a;
Output
Here too, you will get the same output as in the previous example −
a: 1
a: 2
a: 3
a: 4
a: 5
https://www.tutorialspoint.com/cprogramming/c_for_loop.htm 4/9
6/16/24, 11:57 AM For Loop in C
You can also omit the second clause of the test condition in the parenthesis. In that
case, you will need to terminate the loop with a break statement, otherwise the loop
runs infinitely.
Example
#include <stdio.h>
int main(){
int a;
Output
a: 1
a: 2
a: 3
a: 4
a: 5
Example
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_for_loop.htm 5/9
6/16/24, 11:57 AM For Loop in C
int a, b;
return 0;
}
Output
When you run this code, it will produce the following output −
a: 1 b: 1 a*b: 1
a: 2 b: 2 a*b: 4
a: 3 b: 3 a*b: 9
a: 4 b: 4 a*b: 16
a: 5 b: 5 a*b: 25
Example
#include <stdio.h>
int main(){
int a;
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_for_loop.htm 6/9
6/16/24, 11:57 AM For Loop in C
Output
a: 5
a: 4
a: 3
a: 2
a: 1
Example
#include <stdio.h>
int main(){
int i;
int arr[] = {10, 20, 30, 40, 50};
return 0;
}
Output
When you run this code, it will produce the following output −
a[0]: 10
a[1]: 20
a[2]: 30
a[3]: 40
a[4]: 50
https://www.tutorialspoint.com/cprogramming/c_for_loop.htm 7/9
6/16/24, 11:57 AM For Loop in C
The following program computes the average of all the integers in a given array.
#include <stdio.h>
int main(){
int i;
int arr[] = {10, 20, 30, 40, 50};
int sum = 0;
float avg;
return 0;
}
Output
Average = 30.000000
The following code uses a for loop to calculate the factorial value of a number. Note
that the factorial of a number is the product of all integers between 1 and the given
number. The factorial is mathematically represented by the following formula −
x! = 1 * 2 * . . . * x
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_for_loop.htm 8/9
6/16/24, 11:57 AM For Loop in C
int i, x = 5;
int fact = 1;
return 0;
}
Output
When you run this code, it will produce the following output −
5! = 120
The for loop is ideally suited when the number of repetitions is known. However, the
looping behaviour can be controlled by the break and continue keywords inside the
body of the for loop. Nested for loops are also routinely used in the processing of
two dimensional arrays.
https://www.tutorialspoint.com/cprogramming/c_for_loop.htm 9/9