For Loop in C


The for loop


The for allows us to specify three things about a loop in a single line:

(a) Setting a loop counter to an initial value.
(b) Testing the loop counter to determine whether its value has reached the number of
repetitions desired.
(c)Increasing the value of loop counter each time the program segment within the loop has
been executed.

First of all the value is initialized, then in the loop the condition is checked and after processing
the body of the loop, it will increment or decrement the loop according to requirement.

In this loop structure, more than one variable can be initilized.

Syntax:

for(initialisation; test-condition; incre/decre)
{
statements;
}

In above syntax, the given three expressions are seperated by ';' (Semicolon)

Features :

  • More concise
  • Easy to use
  • Highly flexible
  • More than one variable can be initilized.
  • More than one increments can be applied.
  • More than two conditions can be used.

Simple Example :


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

void main()
{
int a;
clrscr();
for(i=0; i<5; i++)
{
printf("\nHi"); // 5 times
}
getch();
}

Output :

Hi
Hi
Hi
Hi
Hi


Description :

First the variable i is initialized to value 0 and then the condition is checked which is true since 0<5, then the control goes inside the loop and it print 'Hi', and then the value of i is incremented to 2. This process continues till the value of i becomes greater than 5 that is 5 times 'Hi' is displayed.

Note :

It is important to note that the initialization, testing and incrementation part of a for loop can be replaced by any valid expression. Thus the following for loops are perfectly ok.

for ( i = 10 ; i ; i -- ) printf ( "%d", i ) ;

for ( i < 4 ; j = 5 ; j = 0 ) printf ( "%d", i ) ;

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

for ( scanf ( "%d", &i ) ; i <= 10 ; i++ )
printf ( "%d", i ) ;


Nested for loops :


The way if statements can be nested, similarly for can also be nested. To understand how nested loops work, look at the program given below:

/* Demonstration of nested loops */

main( )
{
int r, c, sum ;
for ( r = 1 ; r <= 3 ; r++ ) /* outer loop */
{
for ( c = 1 ; c <= 2 ; c++ ) /* inner loop */
{
sum = r + c ;
printf ( "r = %d c = %d sum = %d\n", r, c, sum ) ;
}
}
}
When you run this program you will get the following output:
r = 1 c = 1 sum = 2
r = 1 c = 2 sum = 3
r = 2 c = 1 sum = 3
r = 2 c = 2 sum = 4
r = 3 c = 1 sum = 4
r = 3 c = 2 sum = 5

Here, for each value of r the inner loop is cycled through twice, with the variable c taking values from 1 to 2. The inner loop terminates when the value of c exceeds 2, and the outer loop
terminates when the value of r exceeds 3.

Multiple Initialisations in the for Loop

The initialisation expression of the for loop can contain more than one statement separated by a comma. For example,

for ( i = 1, j = 2 ; j <= 10 ; j++ )

Multiple statements can also be used in the incrementation expression of for loop; i.e., you can increment (or decrement) two or more variables at the same time. However, only one expression is allowed in the test expression. This expression may contain several conditions linked together using logical operators.

Use of multiple statements in the initialisation expression also demonstrates why semicolons are used to separate the three expressions in the for loop. If commas had been used, they could
not also have been used to separate multiple statements in the initialisation expression, without confusing the compiler.

Note :

We will discuss more for loops programs in our C programs section

     Prev                                                   NEXT

0 comments:

Post a Comment