for loop in C Language
Categories: C language
The for loop in C language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.
Syntax of for loop in C
The syntax of for loop in c language is given below:
for(Expression 1; Expression 2; Expression 3){
//code to be executed
}
C for loop Examples
Let's see the simple program of for loop that prints table of 1.
#include<stdio.h>
int main(){
int i=0;
for(i=1;i<=10;i++){
printf("%d \n",i);
}
return 0;
}
C Program: Print table for the given number using C for loop
#include<stdio.h>
int main(){
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++){
printf("%d \n",(number*i));
}
return 0;
}
Output
Enter a number: 2
2
4
6
8
10
12
14
16
18
20
Properties of Expression 1
1. The expression represents the initialization of the loop variable.
2. We can initialize more than one variable in Expression 1.
3. Expression 1 is optional.
4. In C, we can not declare the variables in Expression 1. However, It can be an exception in some compilers.