Difference Between While and Do While Loop

Posted in

Difference Between While and Do While Loop
sameekshamedewar

Sameeksha Medewar
Last updated on November 21, 2024

    Loops come in handy when you want to execute the same block of code repeatedly. They help you optimize and organize your program . Among the different types of loops, the two most confusing ones are while and do-while loops .

    The while loop evaluates the given condition first and executes a block of code only if the condition holds true. In contrast, the do-while loop first executes the block of code and then evaluates the given condition. This is the primary difference between a while and a do-while loop.

    In this article, I will help you understand more differences between these two types of loops with examples.

    What is a Loop?

    A loop is a control structure that enables you to execute a set of instructions (code block) repeatedly for a specified number of times. It stops the execution when the specified condition is not met.

    In computer programming , you come across several instances where you need to perform the same task repeatedly but with different inputs. In such scenarios, loops help you automate the task by writing the code block only once and automatically executing it repeatedly with different inputs.

    For instance, consider you are given a task to write a program to print numbers from 1 to 100. It is extremely inefficient to write the print statement 100 times. Right?

    To avoid this repetitive task, you can use a loop to execute a single print statement 100 times and display numbers from 1 to 100.

    What is a While Loop?

    A while loop is an entry-controlled loop, as it evaluates the specified boolean condition before executing the loop body. If the condition holds true, it then enters into the body of the loop and executes the statements. After that, the control of a program returns to the beginning of the while loop to again check the condition and repeat the same process.

    Simply put, a while loop executes the block of code repeatedly unless the given condition is met. Once the condition fails, the while loop does not process its body, and the program control exits from the loop.

    A while loop primarily consists of two components –

    • Control statement: It is a condition evaluated in every iteration of the while loop. As long as the condition remains true, the while loop continues to execute.
    • Loop body: It is a block of code or set of instructions to be executed if the control statement holds true. Simply put, the loop body consists of code for a task that you want to perform repeatedly until the condition is true.

    Note: If the initial condition is not met, the while loop body will not be executed, not even once.

    Syntax:

    if (boolean condition)
    {
    //body of the loop
    }

    How does it work?

    • Initially, the program checks the control condition specified in parentheses.
    • If the condition evaluates to true, the control of a program moves inside the body of the loop to execute the series of statements.
    • Again, the program control moves to the beginning of the while loop to check the condition.
    • The process continues until the condition evaluates to false.
    • When the condition does not hold true, the while loop terminates.

    Example

    #include<iostream>
    using namespace std;
    int main()
    {
      int num = 1;
      while(num<=10)
      {
         cout<<num<<"\n";
         num+= 1;
      }
    return0;
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    Explanation:

    In the above example, we have printed the numbers from 1 to 10.

    • Initially, we have initialized a variable num = 1.
    • We specified the condition (num<=10).
    • In the first iteration, the value of the variable num is 1.
    • When num = 1, the condition (num<=10) holds true. Hence, the program control moves to the body of the loop and executes the statements. It prints the value of the variable num and increases its value by 1.
    • In the second iteration, the value of the variable num becomes 2.
    • When num = 2, the condition (num<=10) is true. Again, the control moves to the body of the loop to execute the code. This time, it prints 2 and increases the value of num by 1.
    • The same process continues until num becomes 10. After num > 10, the condition evaluates to false, and the loop ends.

    What is a Do-While Loop?

    A do-while loop is an exit-controlled loop, as it evaluates the control statement (boolean condition) after executing the loop body.

    In the first iteration of the do-while loop, the loop body is executed without checking any condition. So, the loop body is executed at least once, even if the condition is not met.

    If the specified condition is true, the loop body is executed once more, and it continues until the condition evaluates to false. Otherwise, the do-while loop terminates.

    Syntax:

    do 
    
    {
    //body of the loop
    } while(boolean condition);

    How does it work?

    • Initially, the loop body executes and then checks for the specified boolean condition.
    • If the condition evaluates to true, the loop body is executed again, and the condition is checked one more time.
    • The process continues until the condition becomes false.
    • Once the condition becomes false, the do-while loop terminates.

    Example 1

    #include<iostream>
    usingnamespacestd;
    intmain()
    {
    int num = 1;
    do
    {
    cout<<num<<"\n";
    num+= 1;
    }while(num<=5);
    return0;
    }

    Output:

    1
    2
    3
    4
    5

    Explanation:

    In the above example, we have initialized a variable num to 1.

    • For the first iteration, the loop body executes without checking any condition.
    • It prints the num value, 1, and increases the variable value by 1.
    • Now, the value of num is 2.
    • The boolean condition (num<=5) is checked, which holds true.
    • The loop body is again executed, which prints the num value, 2, and increases it by 1.
    • Further, the boolean condition (num<=5) is evaluated. If this holds true, the loop is executed.
    • The process continues until num becomes 5.

    Example 2

    #include<iostream>
    usingnamespacestd;
    intmain()
    {
    int num = 1;
    do
    {
    cout<<num<<"\n";
    num+= 1;
    }while(num>=5);
    
    cout<<"The do-while loop terminates";
    return0;
    }

    Output:

    1

    Explanation:

    This example is a bit different than the above one and demonstrates that the loop body in a do-while loop executes at least once, even if the condition is not met.

    • In the beginning, we have initialized the variable num = 1.
    • The do-while loop starts. The program then prints the value of num, 1, and increases it by 1.
    • Further, it checks the condition (num>=5). As it does not hold true, the loop terminates and prints the statement outside it.

    Difference Between While and Do While Loop

    Let me now provide a head-to-head comparison of the while loop and do while loop below.

    While Loop

    Do While Loop

    The while loop executes the condition first and then the loop body.

    The do-while loop executes the loop body first and then the specified condition.

    It is an entry-controlled loop.

    It is an exit-controlled loop.

    The while loop follows the top-down approach.

    The do-while loop follows the bottom-up approach.

    It uses the while keyword.

    It uses the do…while keyword.

    If the condition is false for the very first iteration, the loop body is not executed at all.

    Even if the condition is false in the first iteration, the do-while loop body is executed at least once.

    Syntax:

    while (condition)
    
    {
    
    //body of the loop
    
    }

    Syntax:

    do
    
    { 
    
    //body of the loop
    
    } while (condition);

    There is no need for a semicolon at the end of while (condition) .

    The do-while loop uses a semicolon at the end of while (condition).

    If the body of the loop has a single statement, you can eliminate the use of curly braces.

    Even if the loop body has a single statement, you must use curly braces.

    While vs Do While Loop - When Should I Use?

    Use the while loop when you:

    • Want to check the condition before executing the loop body.
    • Iterate over data structures to traverse and process each element sequentially.
    • Wish to validate input data.
    • Implement different algorithms, like binary search, quicksort, etc., and perform repetitive actions until the condition is satisfied.

    Use the do-while loop when you:

    • Write menu-driven programs to present users with the menu at least once.
    • Want to validate user input.
    • Wish to test conditions at the end of the block.
    • Fetch and process data from a database until there are no more records.

    Conclusion

    I hope the above distinctions helped you understand the difference between while loop and do while loop. Though both resemble close to each other, the slight difference makes them different.

    The while loop executes the loop's body only if the condition is true. In the case of a do-while loop, the loop’s body is executed first, and then the condition is checked.

    In the while loop, if the condition does not hold true, the loop body is not executed, not even once. In contrast, the body of the do-while loop is executed at least once, even if the condition is false.

    FAQs


    The major difference between while and do while loop is that the while loop executes a condition first and then the loop body. Conversely, the do while loop executes the loop body first and then evaluates the given condition.

    Based on the approach, the difference between while and do while loop is that the while loop follows the top-down approach, whereas the do while loop follows the bottom-up approach.

    The choice between the while and do while loop depends on whether you want to execute the loop body at least once or not.

    Leave a Comment on this Post

    0 Comments