C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

How For Loop Works in C Programming


Abstraction:
        This tutorial shows you the basic principles of the major loop concept in C Programming



           We learnt already about ‘While Loop’ and ‘Do While Loop’ in previous lessons. Now let us learn another loop; For Loop. For loop is the most popular and widely used loop in programming world. Like the while loop, for loop is also simple and easy to understand.


The general form of for loop is:



 for (initialization; condition; update)   
   {
       Body of the loop;
   }



Initialization:
Initialization of control statement is done first. Here variables are initialized.

Condition:
The value of control variable is tested using the test condition.

Update:
When the body of the loop executed, the control is transferred back to this section, where may has increment or decrement statement.

If you can’t understand above theories, then let us see some examples:

Program to display first 10 numbers:


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


Explanation:
In for loop,

i = 1;
Here ‘i’ is initialized as 1.

i<=10;
Here check the value of i is less than or equal to 10. In first iteration, the value of i is 1 and that is less than 10, so execute the body of the loop.

i++;
After executing the body of the loop, then the control return back to this update section. Here i++ increment the value of the i one by one.And return back to the condition section (i<=10).

Above processes continue and the value of i become 10, after executing the body of the loop, i incremented to 11. Now again check the condition, here i is 11, so the condition does not hold. So control goes out of loop and program gets end.

See the working of loop in picture:


Working of For Loop in C detailed

‘For loop’ works according to the roots where I denoted in this picture. There are two ‘2’. If the condition is true, then go to the body of the loop, if condition does not hold, then go to the out of the loop, here steps 3 and 4 will not work.

We can declare variables in initialization part.


for(int i=1; i<=10; i++)  
{
   printf("%d ",i);
}

Also we can use increment inside the loop body.


for(int i=1; i<=10;)
{
  printf("%d ",i);
  i++;
}

Sample Output:
For loop sample output

Program to print numbers in between 2 ranges:


#include<stdio.h>
#include <conio.h>
int main()
{
  int start,end;
  printf("Enter the starting range:");    
  scanf("%d",&start);
  printf("Enter the ending range:");
  scanf("%d",&end);
  for(int i=start;i<=end; i++)
  {
   printf("%d ",i);
  }
  getch();
}

Here both starting point and ending point were accepted and set them as loop's initial value and ending value respectively.

Sample Output:
Numbers between two ranges in C

Program to display odd numbers below 20:


#include<stdio.h>
#include <conio.h>
int main()
{
  for(int i=1;i<=20; i = i+2)    
  {
    printf("%d ",i);
  }
  getch();
}

Where i initialized as 1 and i increments by 2.


No comments:

Post a Comment