7.4 – for loop – points to remember

by subbu on October 7, 2013

In the previous session we have discoursed the syntax and functionality of for loop. We have translated some of the programs from while loop to for loop for easy understanding. Now in this session we are going to discourse more important issues step by step with suitable examples. Learning for loop wont completes without addressing the following issues.

Optional in conditional statement of for loop:

We can leave any section in the conditional statement but, using two semicolons are mandatory.
It is so important to remember that missing condition makes a loop as infinite loop because for loop treats no condition as true.

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

Output:
1        2        3        4        5

Here incrementing of i is done in the printf() statement rather in the increment/decrement section

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

Output:
Sum of natural numbers from 1 to 5: 15

Here both sum and i are initialized before the loop rather in the initialization section

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

Output:
1        2        3…….

Here it is treated as infinite loop without condition, keep on prints the integers

#include<stdio.h>
int main()
{
for(;;)
   printf("Hello World");
return 0;
}

Output:
Hello World …………

The above loop is an infinite loop because for loop treats no condition as true.

Null statements in C language:

A control flow statement like if, while or for terminated with a semicolon without having control on any statement(s) is called null statement
A for statement terminated with a semicolon iterates within the conditional statement as long as the condition is true.

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

Output:
6

The above loop iterates within the conditional statement as long as i is less than or equal to 5, breaks when i is 6. Only 6 is printed as the output.

Multiple initialization and increment/decrement statements in for loop:

We can write multiple initialization and increment/decrement statements within the conditional statement using comma separation.

Specification: print the sum of natural number from 1 to 10

sum=0;
i=1
while(i<=10)
{
   sum=sum+i;
   i++;
}

Here the loop has two initialization statements, both these can be placed in the initialization section of for loop with comma separation. Equivalent for loop is

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

The same loop can be written using arithmetic assigning operator as

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

The same loop can be written using null statement as

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

Program:

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

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

Specification: print the following output

1       5
2       4
3       3
4       2
5       1

Logic:
Here we have to take two driver variables i and j. i must start from 1 and j must start from 5.
i must be incremented by 1 and j must be decremented by 1 for every iteration.
Loop must run as long as i is less than or equal to 5 or j is greater than or equal to 1. Here checking one condition is enough

i=1;
j=5;
while(i<=5)  /* can also write as while(j>=1) */
{
   printf("\n%d\t%d",i,j);
   i++;
   j--;
}

The same loop can be written with for loop as

for(i=1,j=5;i<=5;i++,j--)
   printf("\n%d\t%d",i,j);

The same loop can be written with null statement as

for(i=1,j=5;i<=5;printf("\n%d\t%d",i,j),i++,j--);

Program:

/* printing the output in parallel */
#include<stdio.h>
int main()
{
int i,j;
for(i=1,j=5;i<=5;printf("\n%d\t%d",i,j),i++,j--);
return 0;
}

Output:
1        5
2        4
3        3
4        2
5        1

Specification: Accept numbers until 0 and print the sum of them

sum=0;
printf("Enter numbers, give 0 to stop:\n");
scanf("%d",&n);
do{
   sum=sum+n;
   scanf("%d",&n);
}while(n!=0);

The same loop can be written using for loop as

for(sum=0,scanf("%d",&n);n!=0;sum+=n,scanf("%d",&n));

Here the first scanf() read a number for the first time, the second scanf() read numbers for evry iteration

Program:

/* Sum of accepted numbers */
#include<stdio.h>
int main()
{
int sum,n;
printf("Enter numbers, give 0 to stop..\n");
for(sum=0,scanf("%d",&n);n!=0;sum+=n,scanf("%d",&n));
  printf("Sum of numbers %d",sum);
return 0;
}

Execution:
Enter the numbers, give 0 to stop..
12
34
56
77
0
Sum of numbers 179

Previous post:

Next post: