For Loop in C

Advertisements

While Loop in C Do While in C

For Loop in C Programming

When you need to execute a block of code several number of times then you need to use looping concept in C language. In C Programming Language for loop is a statement which allows code to be repeatedly executed. It contains 3 parts.
for loop in C language is used to iterate the statements or a part of the program several times.

  • Initialization
  • Condition
  • Increment or Decrements

Syntax

for ( initialization; condition; increment )
{
 statement(s);
}
For Loop steps
  • Initialization: This step is execute first and this is execute only once when we are entering into the loop first time. This step is allow to declare and initialize any loop control variables.
  • Condition: This is next step after initialization step, if it is true, the body of the loop is executed, if it is false then the body of the loop does not execute and flow of control goes outside of the for loop.
  • Increment or Decrements: After completion of Initialization and Condition steps loop body code is executed and then Increment or Decrements steps is execute. This statement allows to update any loop control variables.

Note: In for loop everything is optional but mandatory to place 2 semicolons (; ;)

Example

for()	    // Error
for( ; ; )  // valid

Flow Diagram for Loop in C

for loop in c

Control flow of for loop

control flow of for loop in c
  • First initialize the variable, It execute only once when we are entering into the loop first time.
  • In second step check condition
  • In third step control goes inside loop body and execute.
  • At last increase the value of variable
  • Same process is repeat until condition not false.
for loop in c

See for loop in c control flow in Detail

Example of For Loop in C

#include<stdio.h>
#include<conio.h>

void main()
{
int i;
clrscr();
for(i=1;i<5;i++)
{
printf("\n%d",i);
}
getch();
}

Output

1
2
3
4

Important Points

  • In for loop if condition part is not given then it will repeats infinite times, because condition part will replace it non-zero. So it is always true like.
    for( ; 1; )
  • For loop is repeats in anti lock wise direction.
  • In for loop also rechecking process will be occurred that is before execution of the statement block, condition part will evaluated.

For Loop Code in C

while(0)  // no repetition
for( ; 0; )  // it will repeats 1 time

Note: Always execution process of for loop is faster than while loop.

Initialize More Than One Variable

Initialization step is execute first and this is execute only once. Here we can initialize more than one variable in Initialization step.

For Loop Program in C

#include<stdio.h> 
#include<conio.h>

void main()  
{  
    int a,b,c;  
    clrscr();
    for(a=0,b=12,c=25;a<2;a++)  
    {  
        printf("%d ",a+b+c);  
    }  
getch();
}  

Output

37  38

Condition Statement in For Loop

In for loop we use more than One conditon and more than One initilization.

Example

#include<stdio.h> 
#include<conio.h>

void main()  
{  
    int i,j,k;  
    clrscr();
    for(i=0,j=0,k=0;i<2,k<4,j<12;i++)  
    {  
        printf("%d %d %d\n",i,j,k);  
        j+=3;  
        k+=4;  
    }  
    
 getch();
}  

Output

0  0  0
1  3  4
2  6  8
3  9  12

For Loop Without Conditional Statement

We can use for loop without any conditional statement.

For Loop Program in C

#include<stdio.h> 
#include<conio.h>

void main()  
{  
   
    int i;  
    clrscr();
    for(i=0;;i++)  
    {  
        printf("%d",i);  
    }    
    
 getch();
}  

Output

infinite loop

Increase or Decrease more than one Variable in For Loop

We can Increase or Decrease more than one Variable in For Loop

For Loop Example in C

#include<stdio.h> 
#include<conio.h>

void main()  
{  
    int i=0,j=2; 
    clrscr();
    for(i = 0;i<6;i++,j=j+2)  
    {  
        printf("%d %d\n",i,j);  
    } 
    
 getch();
}  

Output

0  2
1  4
2  6
3  8
4  10
5  12

While Loop in C Do While in C

Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements