Loop Control statements

Loop:

                    The process of repeatedly executing a block of statement up to specified number of times is called a loop

                    C supports three types of looping statements.
                    They are :
                                    1) while loop
                                    2) do-while loop
                                    3) for loop

1. while loop: (Entry control loop statement)

                    It is a conditional controlled statement in C language.

Syntax:

                    while(test condition)
                    {
                            Statements;
                    }

                    In this loop first the condition will be evaluated. If it is true, then the statement block will be executed. After the execution of statement, the test condition will be evaluated once again, if it is true then the statement block will be executed once again. This process of repeated execution continued until the test condition becomes false.

2. do-while loop: (exit control loop statement)

                    It is an alternative form of while loop. The only difference between while and do-while is the minimum number of execution of while is zero and the minimum number of execution of do-while is one.

Syntax:

                    do
                    {
                            statements;
                    }
                    while(test condition);

                    In this loop first the statement will be executed, after the execution of statements, the test condition will be evaluated. If it is true, then the statement block will be executed once again, this process of repeated execution continuous until the test condition becomes false.

3. for loop:

                    It is the most commonly used loop statement in C language. It is consisting of three expressions.

Syntax:

                    for(exp1; exp2; exp3)
                    {
                            Statements;
                    }

                    In this loop first expression is used to initialize the index, second expression is used to test whether the loop is to be continued or not (test condition) and the third expression is used to change the index for further iteration.

Nested loops:

                    Using a loop statement, within another loop is called as nested loop.



Next Topic        :  

No comments:

Post a Comment