Thursday, November 12, 2009

Loops in C programming language

In many situations we have to repeat the execution of a certain number of statements for a number of times. In such case loop can be applied. The loop continues while a condition set by the programmer is true. When the condition becomes false the loop breaks and the control passes to the statement following the loop. There are three types of loop in 'C'. for loop, while loop and do-while loop.

 A loop consists of two parts: -
1.control statement
2. Body of the loop.
 Again control statement can be of two types
i) Entry control
ii) Exit control

 In entry control the condition of the loop is tested before the loop body is executed. And on each repetition of the loop this checking is done. So it may happen that the loop body is not executed for a single time if the condition is false from the beginning. In case of exit control statement the condition is checked at the end of the loop body. So in this type of control statement the loop body is executed unconditionally for the first time. The easiest among the three is for loop.

For loop is an entry control loop. Here the condition is checked before the execution of the body. The syntax of for loop is:
 for (initialization; condition; reinitialisation) control statement
{ Body of the loop (statement /statements)
}
end of the loop body The sequence of execution followed by a for loop can be stated as:
1.initialization.
2.Conditional expression
3.Body of the loop
4.Reinitialization
5.Steps 2, 3 and 4 will repeat until the condition becomes false.

 Here all the loop control elements are gathered in one place. for is a keyword followed by parentheses that contains three expressions separated by semicolons. When the control hits the for statement, it first executes the initialization expression. In this part a variable is initialized with a value and this variable is known as loop control variable. For a for loop the initialization expression is executed only once and at the start of the loop. Then the control passes to the conditional expression and executes it. If the condition is true, the control enters the body of the loop, executes the entire body and comes back to the reinitialisation expression where it reinitializes the loop control variable. After reinitializing the control again goes to the conditional expression and executes it. If the condition is true the control again enters the loop body and repeats the process until the conditional expression becomes false. When the condition becomes false the control breaks the loop and passes to the statement following the loop body. Here is a simple program that will print the value of a variable from 0 to 9 using for loop.

 #include < stdio.h>
 void main ()
{
 int x; /* 'x' is the loop control variable.*/
 clrscr ();
for (x=0;x < 10;x++)
printf ("Value of x is: %d\n", x);
 printf ("End of loop");
 getch ();
}

  Loop control variable 'x' is initialized here with '0'.Then 0 is checked with 10 to see whether it is less than 10, found true and the loop body is executed .We will get the output as: value of x is :0 Next the control goes to the reinitialisation expression i.e. x++. Here the value of 'x' is incremented to '1' and again the conditional expression is executed with the new value of 'x'. The process of reinitialisation, execution of the conditional expression and for loop body continues until 'x' becomes 10.10<10 is false and in this stage the control breaks the loop and passes to the next statement printf ("End of loop"); So the output of this program will be: value of x is :0 value of x is: 1 value of x is: 2 value of x is: 3 value of x is: 4 value of x is: 5 value of x is: 6 value of x is: 7 value of x is: 8 value of x is: 9 End of loop One point to be noted that never put semicolon after the loop control statement. Loop control statement and the body of the loop make a single statement. If we want to display the values of 'x' from 9 to 0 then change the control statement as shown below. for (x=9;x>=0;x--)

 Any Question ? Put Your comments.

4 comments:

  1. what happens if we put a semicolon after for statement.

    ReplyDelete
  2. In for loop - control statement ( for statement ) and body of the loop is a single statement. If we put semicolon after the control statement it means the loop is of empty body. The loop will execute the empty body for number of times specified in conditional statement and the block following the control statement ( which was the body of loop) will be executed only once.

    ReplyDelete
  3. which is optional attributes in for loop?

    ReplyDelete
  4. Initialisation and re-initialisation statement of loop control variable is optional in for loop. You can initialise the loop control variable anywhere above the loop and it can be re-initialised within loop body.

    If you do not use conditional statement then the loop would be infinite loop.

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner