What is Do While Loop in C?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

What is Do While Loop?

A do-while loop is a control flow statement that allows for repeated execution of a block of code based on a condition. Unlike the standard while loop, the do-while loop guarantees that the code block will be executed at least once since the condition is evaluated after the code block has been run. The loop begins with the do keyword, followed by the code block wrapped in curly braces. After this block, the while keyword specifies the condition to be evaluated. If this condition evaluates to true, the code block is executed again. This process continues until the condition evaluates to false. The do-while loop is particularly useful in situations where an operation must be executed at least once before checking the continuation condition, such as reading user input or initializing certain processes.

The syntax of the do while loop in C

Syntax for do-while() loop in C is as follows:

The Flowchart of the do while loop in C

Flow Diagram of Do While loop in C example

Examples of Do While Loop

Let us look at a real-world example of do...while() loop and then we will look at some coding examples of do...while() loop.

Example 1

Let's suppose, you have set an alarm clock to buzz at 05:00AM in the morning (body of the loop). It will go off at 05:00AM in the morning i.e. at least once, and suppose the condition at the end of the loop is that if you have snoozed your alarm or not. If you have snoozed (i.e. true), it will buzz again after a certain time and it will not buzz if you have set the alarm off without snoozing it (i.e. false).

Now, let us look at some of the do...while() loop code examples.

In the first example, we are writing a program for finding the average of first N natural numbers using a do...while() loop in C.

Code:

Input

Output :

Explanation :

  • Two integer variable sum = 0 and i = 0 are declared and initialized in the main() function. These are known as the initialization statements.
  • An integer variable N and a float variable avg are also declared but not initialized.
  • In the above example, we have taken 12 as an input number to store in N using the scanf() statement.
  • Now, the body of do...while() loop starts. Control directly shifts inside the do block and statements start executing.
  • We have an instruction sum += i, which updates the value of sum to the addition of sum and i variables in every iteration.
  • i++; is an update expression i.e. used to fulfill the below condition and exit the loop at a certain point.
  • while( i <= N); checks the condition and repeat the loop if it holds true, and exits if false.
  • In the above example, the loop exits when i = 13 i.e. i is not less than N anymore. The control will shift to the next statements outside the loop.
  • We now calculate the avg using the sum and N values.
    • Formula : Average = Sum of N Numbers / N.
  • Now, the average of N numbers is printed using the avg variable and the printf() statement.

Example 2

In this example, we are going to print a multiplication table of an input number N.

Code:

Input

Output :

Explanation:

  • First, an integer variable i is declared and initialized with 1 in the main() function. It is known as the initialization statement.
  • A variable N is also declared but not initialized. N has been taken as input from the user.
  • Now, the body of do...while() loop starts. Control directly shifts inside the do block and statements start executing.
  • printf("%d * %d = %d\n", N, i, N * i); statement is used to print the multiplication table of N. In the above example, we have taken N to be 5.
  • i will go from 1 to 10 using the update expression i++;. At each iteration/repetition the print statement will be executed with an increased i value.
  • At i = 11, the condition will hold false, so the control will exit from the loop and shift to the next statement after the loop.

How does the do while Loop in C work?

In a do-while loop, we are entering the body of the loop without checking any condition, so in this loop, the code in the body of the loop will execute at least once and then a boolean condition (either true or false) is checked that is passed as an argument in the while() function at the bottom of the loop. If the condition holds true, the control will again shift to the statements inside of the loop body and this process will repeat itself until the condition becomes false. This property of exit-control in do...while() loop makes it different from the other two loops available in the C Language, i.e. for-loop and while-loop that checks the condition at the beginning of the loop and are known as entry controlled loops.

Advantages of Using the Do While Loop

  • Guaranteed Execution: - The loop ensures that its body executes at least once, making it useful when the code block must run prior to the condition check.
  • Simplicity: - The do-while loop can make code easier to understand in scenarios where it's more intuitive for the loop body to execute before checking a condition.
  • Fewer Variables: - In some situations, you can avoid declaring and initializing additional variables outside the loop.
  • Interactive Programs: - It's ideal for situations, such as menu-driven programs, where an action is performed before awaiting user input.

Disadvantages of Using the Do While Loop

  • Potential Infinite Loop: - If not used carefully, it can result in infinite loops since the loop condition is checked after the loop body has executed.
  • Limited Use Cases: - Its applicability is narrower compared to the for and while loops, which check conditions before the loop body executes.
  • Debugging Difficulty: - Errors might be harder to catch because the loop's body will always execute at least once, even if the condition is false from the start.
  • Unexpected Behavior: - For those unfamiliar with the do-while loop, it might lead to unexpected behavior since the loop's condition is at the end.

Conclusion

  • The do-while loop in C offers a unique mechanism for loop control where the loop body is guaranteed to execute at least once, even before the loop condition is evaluated.
  • This loop is particularly useful in scenarios where the programmer wants the loop's body to run before checking a condition, such as in menu-driven programs or when initializing specific operations.
  • While it provides certain advantages like guaranteed execution and simplicity, it also has its set of challenges, including potential for infinite loops and unexpected behavior for those unfamiliar with its structure.
  • Overall, understanding when and how to use the do-while loop efficiently can help in designing clearer and more effective C programs.