C Language

For Loop

In C Language by Baqir AliLeave a Comment

Situations in which the number of passes through the loop is known in advance are often best dealt with using a for statement.

The general form of a for statement is

 for (expr1; expr2; expr3)
          statement; 

where expr1 is used to initialize some parameter (called a control variable, or index) that controls the looping action, expr2 represents a condition that must be true for the loop to continue execution, and expr3 is used to modify the value of the control variable initially assigned by expr1

The for statement can also incorporate a compound statement

 for (expr1; expr2; expr3)
    {
         statement 1 ;
         statement 2 ;
         ...
         statement n ;
   } 

An example without loops

 Sum the first eight odd numbers:
   int sum = 0;
   sum = sum + 1;
   sum = sum + 2;
   sum = sum + 2;
   sum = sum + 2;
   sum = sum + 2;
   sum = sum + 2;
   sum = sum + 2;
   sum = sum + 2; 

An example using loops

   int i, sum = 0;
   for (i=1; i<=15; i = i + 2)
        sum = sum + i; 
For Loop in C
For Loop

For statement – omissions

Any of the three parts can be omitted, although the semicolons must remain. If expr1 can be omitted if the control variable is initialized elsewhere:

i = 0;    
for (     ;   i <= n  ;   i++) 
{    
    …        
} 

If expr3 can be omitted if the control variable is altered by statements in the body of the for loop

for (i = 1; i <= n; ) 
{
   …  
   i = i + 1;
} 

If the logical expression, expr2 is not present, it is taken as permanently true, so

for (  ;  ;  ) 
{
   …
} 
is an “infinite” loop. 

expr3: altering the value of the control variable. This expression acts like a standalone C statement at the end of the body of the for loop. Therefore, the expressions

 i = i +1
 i += 1
 ++i
 i++ 
 are all equivalent in incrementing the value of i by 1 

Increment or Decrement Expression

The value of expr3 may increment or decrement the variable and If expr3 is negative, the loop actually counts downwards.

For example:

for (i = n ;  i >= 0 ;  i--)
   statement; 

Expression with Arithmetic Expression

The initialization, loop-continuation condition and control-variable modification can contain arithmetic expressions.

For example:

 for (i = n; i <= n * j; i++)
   statement; 

Examples using the for statement

  • Vary the control variable from 1 to 100 in increments of 1.
    • for (i = 1 ;  i <= 100 ; i++)
  • Vary the control variable from 100 to 1 in increments of –1 (decrements of 1).
    • for (i = 100; i >= 1; i–)
  • Vary the control variable for 3 to 33 in increments of 3.
    • for (i = 3; i <= 33; i+=3)
  • Vary the control variable from 20 to 2 in steps of -2.
    • for (i = 20  ;  i >= 2 ;  i-=2 )

Overstuffed for loops

The comma operator can be used to combine statements in a for statement. For example, the statement:

for(two = 2, three = 3; two < 50; two +=2, three += 3)

is perfectly legal. This statement causes the variable two to increment by 2 and the variable three to increment by 3, all in one loop.

Get The Coding Examples From C Language Code Example Page

Leave a Comment