LOOPING CONTROL STATEMENTS

In, Programming, Certain Statements(s) are required to be repeated for fixed number of times or till certain condition is satisfied. There are 3 types of looping structure are provided into c programming as follows.

1)   While Loop.

2)   do…while Loop.

3)   For Loop.

Loop flowchart in C language - Program Code

The loop is controlled by a condition. The condition decides how many times the statements will be executed. If there are more than one sentence in the loop, then those sentences are put in { } symbols. The statements within { } symbols are called body of the loop. We can say that there are two parts of loops: Condition and Body.

Ø TYPES OF LOOPS:

     Depending on where the condition is checked, we can have two types of loop structures:

1)   Entry Control.

2)   Exit Control.

         In entry control loop, the condition is written first and then the body of statements. While in exit control loop, the body of statements is written first and then condition is written. This means that body of statements in exit control loop will be executed at least once.


Ø LOOPING STRUCTURES:

1)   While Loop:

While Loop is helpful, when we do not know in advance, how many times the statements ate to be repeated. The syntax of while loop is:

Syntax of While Loop -

While (condition)                             

   {                                            

              Statement (s);                         

   }

 

The statement within the { } symbols are known as body part of the loop. The { } are not required if there is only one statement in the body part. Here, the condition is checked first and then only the statements are executed. So while loop is Entry controlled loop.

The statements in the body part will be executed till the condition is TRUE. As soon as the condition becomes FALSE, the control will come out of the while loop. If the condition remains TRUE always then, it will lead to infinite loop. So, writing a valid condition is important.

Flowchart of While loop :- 

While loop flowchart in C language - Program Code

Example :- 

i=1;

while ( I <= n )  <- Condition

{

      printf ( “%d\n”, i );       <- Body

      i++;

}

 

2) do…while Loop:

do…while is an exit control loop, where the condition is checked later. At least once the body of the loop will be executed. Whenever you want to make sure that loop statements must execute at least one time, you should use do…while loop instead of while loop.

Syntax of do…while Loop –

do

{

  

    Statements (s);         

 

} while (condition);

 

Here the statement (s) are executed till the condition is TRUE. As soon as the condition evaluates to FALSE, the loop terminates and the text statement after do…while loop is executed.

We can use do…while loop wherever we have used while loop. It means that all the program written in while loop can always written using do…while loop.

 

Flowchart of While loop :-

do while loop flowchart in C language - Program Code

Example :- 

i=1;

do  

{

      printf ( “%d\n”, i );       <- Body

      i++;

}while(i <= n);  <- Condition


3) For Loop:

We have seen in previous structures of loop that when we do not know number of iterations required, we can use while or do…while loop. But, in certain cases, we need to execute some steps certain number of times in such situations where we know the number of iterations in advance, then we can use for loop.

Syntax of For Loop -

for ( initialization; condition; increment/decrement )

{

 

        Statement (s);  

 

}

We required one variable which is used to control the loops, which is called variable. The control variable is initialized in initialization expression. This statement executes only once. The condition expression actually checks the value of control variable. If the condition expression is TRUE, then the statement written are executed. These statements also called as body of for loop. If they are more than one, than put in { }, after every execution of statements, the last expression increment/decrement is executed. Then again the condition is checked and body is executed if the condition evaluates to TRUE. Loop terminates when the condition evaluates to FALSE.

Flowchart of While loop :-

for loop flowchart in C language - Program Code

for ( int i=1 ; i <= 10 ; i++ )

{

       printf (“%d\n”, i);

}