MY mENU


Wednesday 15 February 2012

Loops in C programming


Loop's in C are three kinds:
 those are 1. For Loop
                 2. While Loop
                   3.do-while Loop

For Loop:
 For loop in C is the most general looping construct. The loop header contains three parts: an initialization, a continuation condition, and step.
Syntax:
               for (initialization, condition, step)
                        {
                        Statement 1;
                        Statement 2; 
                        …………...
                        Statement n;
                       }

               For statement contain three parts separated by two semicolons. The first part is known as initialization. The variable called loop control variable or index variable is initialized in this part.

              The second part is known as the condition. The condition should be a value one. The condition check can be a compound expression made up of relational expression connected by logical AND, OR.

                 The third part is known as step. It should be an arithmetic expression. The initialization need not be contained to a single variable. If more than one variable is used for initialization they are separated by commas. The step can also applied to more than one variable separated by commas.

               When for statement is encountered at first index variable is get initialized. This process is done only once during the execution of for statement. When condition is evaluated, if it is true the body of the loop will be executed. If more than one statement has to be executed it should end with a pair of braces. The condition of for loop is executed each time at the beginning of the loop. After executing the body of the loop the step is executed, again the condition is executed. If the condition become false it exit from the loop and control transferred to the statement followed by the loop.

The following example executes 10 times by counting 0..9.
for (i = 0; i < 10; i++)
        ;
While LOOP:
  The while loop evaluates the test expression before every loop, so it can execute zero times if the condition is initially false. It has the following syntax.

while (expression)
        {
        Statement 1;
        Statement 2; 
        …………...
        Statement n;
       }

               Here the initialization of a loop control variable is generally done before the loop separately. The testing of the condition is done in while by evaluating the expression within the parenthesis. If the evaluation result is true value, the block of statement within calibrates is executed. If the evaluation result is false value the block of statements within the body of loop is skipped and the loop execution get terminated with the control passing to the statement immediately following the while construct. The increment or decrement of the loop control variable is generally done within the body of the loop.

DO-While Loop:
 In this case the loop condition is tested at the end of the body of the loop. Hence the loop is executed at least once.
Syntax of do while loop is
do
     {
     Statement 1;
     Statement 2; 
     …………...
     Statement n;
    }
while(expression);     

                 Here the block of statement following the do is executed without any condition check. After this expression is evaluated and if it is true the block of statement in the body of the loop is executed again. Thus the block of statement is repeatedly executed till the expression is evaluated to false.

               

No comments:

Post a Comment