1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Introduction to do while Loops in C

Discussion in 'C' started by lionaneesh, Jul 8, 2011.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India

    Do while loops



    Do while loops are special kind of loops that are used to execute statements repeatedly in some programming languages including C. The do while loop is quite similar to the while loop but the main special thing about Do While loops are that they evaluate conditions at the bottom of the loop after executing the loop body.

    It follows the following format:-

    Code:
    do
    {
        Code to execute;
    }
    while(condition);
    
    The Working

    1. First the program blindly executes the ‘Code’ specified between the braces (‘{’ and ‘}’)
    2. Then after executing it checks if the condition evaluates to true.
    3. If it is true the loop body is executed
    4. This process of repeatedly execution continues until the condition evaluates to false.
    That’s much of a theory there now let’s get started and Construct some do while loops

    Constructing do while loops



    Let’s start by converting some English statements to pseudo-code statements using do while loops.

    1. Mother says to the Robot, “You can mop the floor any number of times if you wish but at least 1 time is compulsory.”

    Code:
    do
    {
        mopTheFloor();
    }
    while(want == true);
    
    2 . Teacher says to the Student, “You can do any number of questions as you want but you have to do at least 20 of them.”
    Code:
    do
    {
        do20Questions();
    }
    while(want == true);
    
    Now that we have a basic understanding of how to construct do while loops now let’s move on to how we can use these statements to create simple real world C programs.

    Using do while loops



    Now let’s create a simple C program which would basically printout “hello world” at least one time and will ask if the user wants to display it more times.
    Code:
    #include<stdio.h>
    
    int main()
    {
        char option;
    
        do
        {
            printf("Hello World");
            printf("\nDo you want to Continue (Y/N) : ");
            scanf("%c",&option);
        }
        while(option != 'N');
        return(0);
    } 
    
    Explanation:-

    1. First we declare a ‘char’ variable named option. ‘option’ is used to input the answer of the user.
    2. In the loop body , We simply printout hello world and ask the user if he/she wants to continue.
    3. Then we input their answer using scanf() statement.
    4. Then finally we check we check if the user has input a ‘N’ (i.e NO) , and if it is false the loop continues.

    Output:-
    Code:
    Hello World
    Do you want to Continue (Y/N) : Y
    Hello World
    Do you want to Continue (Y/N) : Y 
    Hello World
    Do you want to Continue (Y/N) : N
    
    That’s all for this tutorial stay tuned for more.
     
  2. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    very informative sir!!!
     
  3. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    My Pleasure.
     

Share This Page