7.3 – for iteration statement

by subbu on October 5, 2013

So far we went through both the while and do-while iteration statements. Every loop written with these iteration statements have four common parts, those are

  1. Initialization
  2. Condition
  3. Increment/decrement statement
  4. Logic or body of the loop

Initialization statements are used to initialize the driver and result variables, condition is to control the loop, increment/decrement statement to count driver variable and body to implement the logic.

for iteration statement in C language:

In case of for loop, three parts that are initialization, condition and increment/decrement statements are integrates in a single statement separated with two semicolons called conditional statement.
The body is placed just under the for conditional statement.

for iteration statement in C language

  1. Initialization section is the first section to execute in order to initialize the driver and result variables. It will not be executed again in the total life of the loop.
  2. Condition is checked, if the condition is true then proceeds to execute the body otherwise exits from the loop.
  3. The body of the loop is executed and control goes to the increment/decrement section.
  4. Control goes to the condition and proceeds to the next iterations.

Condition, body and increment/decrement statements execute repeatedly as long as the condition is true.

Now let us convert some of the programs from while to for iteration statement to understand the behavior of for loop.

Specification:  Printing 1 to 10 natural numbers

/* using while loop */
#include<stdio.h>
int main()
{
int i=1;
while(i<=10)
{
  printf("\t%d",i);
  i++;
}
return 0;
}

While converting while loop into for loop initialization statement i=1, condition i<=10 and increment/decrement statement i++ are written in for conditional statement separating with two semicolons
The printf(“\t%d”,i); statement is written under the conditional statement

Empty line
printing one to ten natural numbers

/* using for loop */
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
  printf("\t%d",i);
return 0;
}

Example explained:

  • driver variable i is initialized with 1 (i=1)
  • condition i<=10 (1<=10) is true hence proceeds to execute the statement
  • printf() statement prints the value of i (1) and proceeds to increment/decrement statement
  • i++ increments the value of i by 1 and goes to check the condition i<=10 (2<=10), as the condition is true proceeds to next iteration
  • The condition i<=10, printf(“\t%d”,i); and i++ statements execute repeatedly as long as the value of i is less than or equal to 10.

Specification: Printing 10 to 1 natural numbers

Empty line
ten to one natural numbers

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

Example explained:

  • driver variable i is initialized with 10 (i=10)
  • condition i>=1 (1>=1) is true hence proceeds to execute the statement
  • printf() statement prints the value of i (10) and proceeds to increment/decrement statement
  • i- – decrements the value of i by 1 and goes to check the condition i>=1 (2>=1), as the condition is true proceeds to next iteration
  • The condition i>=1, printf(“\t%d”,i); and i– statements execute repeatedly as long as the value of i is greater than or equal to 1

Specification:  Print the sum of natural numbers from 1 to 10
Logic:
We generate 1 to 10 natural numbers using a loop

for(i=1;i<=10;i++)
           i

Here the value of i starts from 1 and increments by 1 for every iteration as long as the value of i is less than or equal to 10.

s=0;
for(i=1;i<=10;i++)
      s=s+i;

We add the value of i to a variable s for every iteration, so that s will have sum of natural numbers from 1 to 10 at the end of loop.
Here s must be initialized with 0 otherwise it may be stored with garbage value, adding anything to garbage results garbage.

for(s=0,i=1;i<=10;i++)
      s=s+i;

both result variable (s) and driver variable (i) can be initialized within the conditional statement using comma operator (,).

/* program to print sum of natural numbers from 1 to 10 */
#include<stdio.h>
int main()
{
int s,i;
for(s=0,i=1;i<=10;i++)
      s=s+i;
printf("Sum of natural numbers from 1 to 10: %d",s);
return 0;
}

Output:
Sum of natural numbers from 1 to 10: 55

Specification:  Print natural numbers from 1 to n
Logic:

for(i=1;i<=10;i++)
   printf("\t%d",i);

It prints natural numbers from 1 to 10

for(i=1;i<=20;i++)
    printf("\t%d",i);

It prints natural numbers from 1 to 20

scanf("%d",&n);
for(i=1;i<=n;i++)
    printf("\t%d",i);

It prints natural numbers from 1 to n, where n may be any number given by the user. If user supply 25 then loop prints 1 to 25 natural numbers, if user gives 50 then loop prints 1 to 50 natural numbers.

/* to print 1 to n natural numbers */
#include<stdio.h>
int main()
{
int n,i;
printf("Enter the limit:");
scanf("%d",&n);
for(i=1;i<=n;i++)
    printf("\t%d",i);
return 0;
}

Execution 1:
Enter the limit: 5
1        2        3        4        5

Execution 2:
Enter the limit: 4
1        2        3        4

Previous post:

Next post: