The For Loop in C Language

The for Loop in C – for statement makes it more convenient to count iterations of a loop and works well where the number of iterations of the loop is known before the loop is entered.

The Syntax of For Loop in C Language

for (initialization; test condition; increment or decrement)
{
Statement(s);
}

The main purpose is to repeat statement while the condition remains true, like the while loop. But in addition, for provides places to specify an initialization instruction and an increment or decrement of the control variable instruction.

So this loop is specially designed to perform a repetitive action with a counter.

The for loop works in the following manner

  1. initialization is executed. Generally, it is an initial value setting for a counter variable. This is executed only once.
  2. the condition is checked, if it is true the loop continues, otherwise the loop finishes and statement is skipped.
  3. Statement(s) is/are executed. As usual, it can be either a single instruction or a block of instructions enclosed in curly brackets { }.
  4. Finally, whatever is specified in the increment or decrement of the control variable field is executed and the loop gets back to step 2.
For Loop in C
For Loop

Program to illustrate For Loop in C Language

Write a program to print first n natural numbers.

/* Program to print first n natural numbers */

#include <stdio.h>

main( )

{

int i,n;

printf(“Enter value of n \n”);

scanf(“%d”,&n);

printf(“\nThe first %d natural numbers are :\n”, n);

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

{

printf(“%d”,i);

}

}

OUTPUT
Enter value of n
6
The first 6 natural numbers are:
1 2 3 4 5 6

The three statements inside the braces of a for loop usually meant for one activity each, however any of them can be left blank also. More than one control variables can be initialized but should be separated by comma.

Various forms of for loop statements can be:

(a) for(;condition;increment/decrement)
body;
A blank first statement will mean no initialization.

(b) for (initialization;condition;)
body;

A blank last statement will mean no running increment/decrement.

(c) for (initialization;;increment/decrement)
body;

A blank second conditional statement means no test condition to control the exit from the loop. So, in the absence of the second statement, it is required to test the condition inside the loop otherwise it results in an infinite loop where the control never exits from the loop.

(d) for (;;increment/decrement)
body;

Initialization is required to be done before the loop and test condition is checked inside the loop.

(e) for (initialization;;)

body;

Test condition and control variable increment/decrement is to be done inside the body of the loop.

(f) for (;condition;)
body;

Initialization is required to be done before the loop and control variable increment/decrement is to be done inside the body of the loop.

(g) for (;;;)
body;

Initialization is required to be done before the loop, test condition and control variable increment/decrement is to be done inside the body of the loop.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: