The do-while loop in C is a control flow statement that allows code to be executed repeatedly based on a specified condition. The key difference between the do-while loop and other loops (like for and while) is that the do-while loop guarantees that the loop body is executed at least once, even if the condition is false at the first check. This is because the condition is evaluated after the loop body is executed.

Syntax of the do-while Loop

do { // Code to execute } while (condition);

Components of the do-while Loop

  1. Loop Body: This block of code is executed once before checking the condition. It contains the statements that you want to repeat.

  2. Condition: This is a boolean expression evaluated after the execution of the loop body. If the condition is true (non-zero), the loop continues to execute. If it is false (zero), the loop terminates.

Example of a do-while Loop

Here’s a simple example that demonstrates how to use a do-while loop:

#include <stdio.h> int main() { int count = 0; // A do-while loop that prints numbers from 0 to 4 do { printf("Count: %d\n", count); count++; // Increment the count variable } while (count < 5); // Check the condition after the loop body return 0; }

Explanation of the Example

  1. Initialization: The variable count is initialized to 0.
  2. Loop Body: The loop prints the current value of count and increments it by 1.
  3. Condition Check: After the loop body is executed, the condition count < 5 is checked. If true, the loop runs again.

The output of this code will be:

Count: 0 Count: 1 Count: 2 Count: 3 Count: 4

Infinite do-while Loop

A do-while loop can also create an infinite loop if the condition always evaluates to true. For example:

#include <stdio.h> int main() { // This will run indefinitely until interrupted do { printf("This loop runs forever.\n"); } while (1); // Condition always true return 0; }

Breaking Out of a do-while Loop

You can use the break statement to exit a do-while loop prematurely:

#include <stdio.h> int main() { int count = 0; do { if (count == 5) { break; // Exit the loop when count is 5 } printf("Count: %d\n", count); count++; } while (count < 10); // Condition checked after the loop body return 0; }

In this case, the loop will print:

Count: 0 Count: 1 Count: 2 Count: 3 Count: 4

And will stop when count equals 5.

Continuing to the Next Iteration

You can use the continue statement to skip the current iteration and proceed to the next one:

#include <stdio.h> int main() { int count = 0; do { count++; // Increment count before checking the condition if (count == 3) { continue; // Skip the iteration when count is 3 } printf("Count: %d\n", count); } while (count < 5); return 0; }

The output will be:

Count: 1 Count: 2 Count: 4 Count: 5

In this example, the number 3 is skipped.

Summary

  • The do-while loop is used when you want to ensure that the loop body is executed at least once.
  • The condition is checked after the execution of the loop body, which distinguishes it from the while loop.
  • Like other loops, the do-while loop can be controlled using break to exit and continue to skip to the next iteration.

The do-while loop is a useful construct in C programming for situations where you need at least one execution of the loop body, regardless of the initial condition.