for loop in C programming || for program in C Programming Language

for loop in C programming || for program in C Programming Language
Fig: for loop in C


Hello guys, what’s up? Today we are going to discuss about for loop in C. So let’s start—
To iterate a statements or a part of the program several times, there we need to use for loop.  It is frequently used to traverse the data structures like the array and linked list.



for loop in C Syntax:

for(Expression 1; Expression 2; Expression 3){  
//code to be executed
}

Properties of Expression 1

This expression initializes the loop variable. We can initialize multiple variables in it. It is not mandatory, it is optional.

Properties of Expression 2
It is a conditional expression, which checks a specific condition to be satisfied. It can have multiple conditions. Anyway, this loop will iterate until the last condition becomes false. Besides, it can perform expression 1 & expression 3’s task. We can pass zero or any other non-zero value with it. By default zero is false


Properties of Expression 3
This expression is optional, which is used for updating the loop variable. We can update multiple variables at the same time.

Flowchart of for loop

for loop in C programming || for program in C Programming Language

Example 1:

#include<stdio.h>  
int main(){
int i=0;
for(i=1;i<=10;i++){
printf("%d n",i);
}
return 0;
}

After executing the program our output looks like

1
2
3
4
5
6
7
8
9
10


Example 2:

    #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;
}

After executing the program our output looks like

Enter a number: 2
2
4
6
8
10
12
14
16
18
20
Infinitive for loop 

If we want to run for loop infinite, we need to provide two semicolons to validate the syntax.

Syntax:

    #include<stdio.h>  
void main ()
{
for(;;)
{
printf("welcome to javatpoint");
}
}

Above this program, you will run infinite times.

That’s all about for loop in C. Later we will discuss another example of c programming.

Post a Comment