Pages

Wednesday, 9 May 2018

do while loop in C


do while loop in C


Till now we have learnt the most frequently used loops in C programming which are for and while loop. Now lets learn about the third loop control instruction which is do while loop.
do while loop is just like a normal loop control instruction which executes a set of statements until the condition turns false.

do while loop in C
Till now we have learnt that loops checks the condition first before executing the set of statements inside its body. But this loop is a bit different than others. It is compulsory that the statements under the body of do while loop will get executed at least once in a program. This loop control instruction executes its statements first, after that it checks the condition for it.

Why do we use do while loops?
Well that’s a really silly question. You cannot predict anything before programming some big thing. While writing some complex big programs, there is a need of a loop which will execute statements at least once. That’s why do while loop is introduced in C.

Syntax

do
{
do this
and this atleast once!
}while(condition);
Lets make one program to grasp this loop too.

#include<stdio.h>

void main()
{
    do
    {
        printf("OMG its working!");
    }while(7>10);
}