Skip to content

For loops in C programming

by yashan on October 19th, 2011

In C programming the for loop statement  is another entry controlled loop that provides a more concise loop control structure. The general form of for loop is as follow -

for ( initialization ; test-condition ; increment/decrement )
{
        body of the loop
}

The execution of the for loop statement is as follows -

  1.  Initialization of the control variables is done first, using assignment statements such as i = 1 and count = 0. The variable i and count are known as loop control variables.
  2. The value of the control variable is tested using the test condition. The test condition is a relational expression, such as i < 10 that determines when the loop will exit. If the condition is true, the body of the loop is executed otherwise the loop is terminated and the execution continues with the statement that immediately follows the loop.
  3. When the body of the loop is executed, the control is transferred back to the for statement for evaluating the last statement in the loop. Now, the control variable is incremented or decremented using an assignment statement such as i = i+1 or i++ and the new value of the control variable is again  tested to see whether it satisfies the loop condition. If the condition is satisfied, the body of the loop is again executed. This process continues till the value of the control variable fails to satisfy the test-condition.
For loop is also the most famous and widely used loop in the C programming because this loop is easy to understand than any other loop. It increases the readability for the programmer. Also the most important that it reduces the length of the program while other loops makes the programs lengthy.

Some Additional Features of For Loop

The for loop in c programming has several capabilities that are not found in other loop constructs. For  example, more than one variable can be initialized at a time in the for statement. The statements -

p = 1;
for(n=0 ; n<17; ++n)

can be written as -

for (p=1, n=0 ; n<17; ++n)

Note that the initialization section has two parts p = 1 and n = 1 separated by comma. Like the initialization section , the test condition may also use any compound relation and the increment section may also have more than one part.

Another unique aspect of for loop in C programming is that one or more sections can be omitted, if necessary. Consider the following statements:

m = 5;
for ( ; m!=100 ; )
{
      printf(" %d ", m);
      m = m+5;
}

Both the initialization and increment sections are omitted in the for statement. The initialization has been done before the for statement and the control variable is incremented inside the loop. In such cases, the sections are left ‘blank’. However, the semicolons separating the sections must remain. If the test condition is not present the for loop sets up an infinite loop. Such loops can be broken by using break and goto statements in the loop.

We can set up time delay loops using the null statements as follows -

for( j = 1000 ; j>0 ; j=j-1)
;

This loop is executed 1000 times without producing any output; it simply causes a time delay. Notice that the body of the loop contains only a semicolon, known as null statement.

From → C Tutorials

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS