-->

7. C-Loop Constructs

C-If-Else-Statement: Previous                                                             Next: C-Nesting of Loops

What Are LOOPS in C programming?

C-Loop Constructs performs a particular set of instructions again and again until a specified condition will not become false. The specified condition helps compiler in termination of loops.
This construct is also called Iteration/Repetitive construct.
In C programming, Loops can be categorized into two ways:
  1.  Determinate
  2.  Inderminate
  3. Pre Tested
  4. Post Tested
  1.  Determinate: - In this, number of steps that are to be performed are known to programmer.
Eg: Determinate loop is for loop.
  1.  Inderminate: - In this, number of steps that are to be performed are not known to the programmer.
       They are further divided into two: -
  1. Pre Tested: - in this, conditions are checked first, and then the body of the loop is executed.
  2. Post Tested: - in this, conditions are checked after the body of the loop is executed.
Indeterminate + Pre Tested = while
Indeterminate + Post Tested = do while
‘C’ supports three looping constructs:
  1. The while loop
  2. The for loop
  3. The do while loop
  1.      While loop: - this loop is suitable in situations where knowledge of the termination condition is not known. while loop evaluated or check the condition first before entering into the loop and if condition is true then only control enters into loop. It is called Entry controlled loop.
Syntax:    while(condition)
{
-----------;
-----------; Body of Loop
-----------;
Increment/decrement;
}

Eg:  
         Write a program to print person`s name 20 times on screen [using while loop]
Solution:  #include<stdio.h>
                                                void main( )
                                                {
                                                                int i = 1;
                                                                while( i <= 20)
                                                                {
                                                                                printf(“Hello Gaurav…!!”);
                                                                                i++;
                                                                }
                                                }
  1. for loop: - this is very common loop and used with array normally. It is also a pre tested + determinate loop and it is also called as Entry Controlled loop.
Syntax:    for(Initialization; Test condition; increment/decrement )
{
-----------;
-----------; Body of Loop
-----------;
}
The for loop contains 3 parts:
  • initial condition, that contains initial values of all the loop counter
  • test condition, it specifies the condition until the loop has to be executed it is also called repetitions desired section
  • increment/decrement, it contains value by which a loop counter is to be increased or decreased.

Eg:           write a program to print person`s name 20 times on screen [using while loop]
Solution:  #include<stdio.h>
void main( )
{
                for( i = 0; i < 20; i++)
                {
                                printf(“Hello MGaurav…!!”);
                }
}
  1. do while loop: - this loop ensure that the program will get executed at least for once whether the condition is false or true. It is an Exit controlled loop.
Syntax:    do
{
-----------;
-----------; Body of Loop
-----------;
Increment/decrement;
} while(condition);
The statement written in loop will execute at least once, if the condition is false and then come out from the loop and if the condition is true then body of the loop is executed until the condition is true.

Eg: 
          write a program to print person`s name 20 times on screen [using while loop]
Solution:  #include<stdio.h>
void main( )
{
                int i = 1;
                do
                {
                                printf(“Hello Gaurav…!!”);
                                i++;
                }while(i <= 20);
}
// The while condition part must be terminated by semicolon( ; ).
   

 What is the Difference between while and do while loop?

while                 do while
1. It is an entry controlled loop          1. It is an exit controlled loop
2. First, condition will check and then enters into the body of loop.          2. First, enters into the body of         loop and then check the condition.
3. If the condition becomes false for first time then loop will not get executed.           3. If the condition becomes           false for first time then also loop          will get executed for at least once.
4. No semicolon is needed after while clause.            4. Semicolon is needed

C-If-Else-Statement: Previous                                                             Next: C-Nesting of Loops