Do While Loop in C Programming Language

The do while loop in C, works similar to the while loop, but with one big difference:

  • The do while loop:
    • runs at least one de code, independently of the condition we have defined.
  • The while loop:
    • only runs the code if the condition is true.

Do While loop syntax:

do{
  <instruction1>
  <instruction2>
  ...
  <instruction4>
}while(condition);

Syntax Explained:

  1. First we run the code;
  2. And then we check the condition:
    1. if the condition is true continue doing the loop;
    2. else if is false, stop and go out of do while loop.

The do while loop is normally used to create menus.

Example

int main(int argc, char** argv) {
    
    //we define the variable that will store the user choice input
    int option;
    
    
    //the more external loop will print the menu, every time the user,
    //choice is different of 0 (EXIT)
    do{
        //this is the internal loop, it is responsible for print the menu,
        //When you work with menu, normally the users input an integer
        //because is easier work with integers than with characters
        do{
            //we set all menu options with 
            printf("1 - Add New User\n");
            printf("2 - Change User\n");
            printf("3 - Delete User\n");
            printf("0 - Exit\n");
            //we store the user input choice in variable 'option'
            scanf("%d", &option);
         
        //the condition is simple if the user input is lower than 0 and
        //upper 3(number of options, without exit option), we will continue 
        //print the menu
        }while(option<0 || option>3);
        
        //with this switch statement we set what we want to if user choose 1 or
        // 2 or 3 or 0, in this case just print a message with the user choice
        switch(option){
            case 1:
                printf("You Select Add New User\n");
                break;
            case 2:
                printf("You Select Change User\n");
                break;
            case 3:
                printf("You Select Delete User\n");
                break;
            default:
                printf("You Select Exit\n");
        }
    //this is the condition responsible for display the menu while
    // the user don't choose '0 - Exit'
    }while(option!=0);

    return (EXIT_SUCCESS);
}

Code Explained

This is just a brief explanation of what the code does, all the detailed information is commented in the code example. This program creates a simple menu for users, this menu have 4 options:

  • 1 – Add New User;
  • 2 – Change User;
  • 3 – Delete User;
  • 0 – Exit.

This options are showed to the users. When we work with menus normally, we ask to users input an integer to choose the menu option, only because is more easier and kick to set the conditions and use in the switch statement to. In this switch statement  we define what we are going to do, after the user choose an option.

 

If you have any doubts, or want more examples of do while loop, please comments bellow!


Leave a Reply

Your email address will not be published. Required fields are marked *