C do-while Loop Complete Tutorial

  • Post author:
  • Post category:C

In this section, we will learn what the do-while statement is and how to use it in C.

Note: Before reading this section, we’re expecting that you’re familiar with the while loop in C Programming.

What is do-while loop in C?

Just like the `while` loop, the do-while loop is used to run a block of instructions for multiple times or basically, for as long as a condition is met.

In the `while` loop section, we mentioned that first the condition of the loop will be checked and if that condition was true, only in that case the body of the loop will start to run!

But there are times we want to start the loop FIRST and then check whether the condition is true and repeat the loop again or just simply stop it after the first time.

In such cases, even if the condition wasn’t true, the body of the loop runs at least for once.

For example, let’s say there’s a login page and we want users to be able to see the login page for the first time and be able to enter their username and password.

If then the username or password wasn’t true, we will reload the page with an error message and ask the user to enter the right username and password.

So for the first time no-matter if the username and password are correct, the page should become visible to the user without any error message.

This is where we can use `do while` loop.

C do-while loop syntax

Here’s the structure of the `do while` loop:

do {
  // code block to be executed
}
while (condition);

`do`: to create a do-while loop, we first start with the keyword `do`. Now the pair of curly braces `{}` that comes after this keyword defines the body of the `do-while` loop where we can put instructions of the loop to be executed.

`while`: the `while` keyword and the pair of parentheses that come after the closing curly brace `}` of the do-while loop, is the place where we put the condition of this type of loop.

Notes:

  • Be aware that the CPU first executes the body of the target do-while loop first (the instructions within the pair of curly braces `{}`) and after that it will move to the condition of this type of loop to see if it results true or not.
  • If the condition within the `while` was true, the body of the `do` statement will run again. This process will continue until the condition becomes false.
  • At some point, the condition of the `while` should become false and, for the majority of cases, the logic of turning the condition of the loop to that it results false should be declared within the body of `do` statement itself.

Example: creating do-while loop in C

#include <stdio.h>

int main() {


    int password;

    do{
        printf("Please enter your password: \n");
        scanf("%d",&password);

    }while(password != 123456);
    
    printf("Welcome home 🙂 ");


    return 0;
}

How does do-while loop work?

In the example above, as long as you enter a wrong password (any number other than super strengthened password: 123456) you’ll see the same message.

But the moment we enter the correct password (123456) the loop will break and the next line, which is calling the `printf` function, will execute and the message `Welcome home ` will appear on the screen.

Difference between do while and while in C

The main difference between the while loop and do-while loop is that:

  • In a while loop, first the condition of the loop will be checked and then the body of the loop will run (only if the result of the condition is true). So basically, there’s a chance that the body might never run! (If the condition is false in the first place).
  • On the other hand, using the `do-while` loop, first the body of the loop will run and then the condition of the loop will be checked. This means at least the body of the loop will run once.

Leave a Reply