C# do-while loop


The do-while loop in C# is similar to the while loop but with one key difference: it guarantees that the loop body will be executed at least once, regardless of the condition. This is because the condition is evaluated after the execution of the loop body.

Syntax of do-while Loop:

do { // Code to be executed } while (condition);
  • Loop Body: The block of code inside the do section that will be executed at least once.
  • Condition: A boolean expression that is evaluated after each iteration. If true, the loop runs again; if false, the loop terminates.

Example of a Basic do-while Loop:

int i = 1; do { Console.WriteLine("Iteration: " + i); i++; // Increment i to eventually break the loop } while (i <= 5);

Explanation:

  • Initialization: int i = 1 initializes the loop control variable i to 1.
  • Loop Body: The statement Console.WriteLine("Iteration: " + i) prints the current value of i.
  • Increment: The value of i is incremented by 1.
  • Condition: After executing the loop body, the condition i <= 5 is evaluated. If true, the loop continues; if false, it terminates.

Output:

Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5

In this example, the loop executes five times, printing the values from 1 to 5.


Key Differences Between while and do-while:

  • Execution Guarantee: The do-while loop guarantees at least one execution of the loop body, even if the condition is false. In contrast, the while loop may not execute at all if the condition is false initially.

Example with User Input:

The do-while loop can be used to repeatedly prompt the user for input until they provide a specific value.

string input; do { Console.WriteLine("Type 'exit' to stop:"); input = Console.ReadLine(); } while (input != "exit");

Explanation:

  • The loop prompts the user to type "exit" and reads their input.
  • The loop continues as long as the input is not equal to "exit".

Output:

Type 'exit' to stop: hello Type 'exit' to stop: world Type 'exit' to stop: exit

The loop will keep asking for input until the user types "exit".


Using break and continue with a do-while Loop:

Just like the while loop, you can use break to exit a do-while loop early and continue to skip to the next iteration.

Using break:

int i = 0; do { i++; if (i == 3) { break; // Exit the loop when i equals 3 } Console.WriteLine("Iteration: " + i); } while (i < 5);

Output:

Iteration: 1 Iteration: 2

The loop terminates early when i reaches 3.

Using continue:

int i = 0; do { i++; if (i == 3) { continue; // Skip the iteration when i equals 3 } Console.WriteLine("Iteration: " + i); } while (i < 5);

Output:

Iteration: 1 Iteration: 2 Iteration: 4 Iteration: 5

Here, when i is 3, the continue statement skips that iteration, so it is not printed.


When to Use a do-while Loop:

  • When the Body Needs to Execute At Least Once: Use a do-while loop when you want to ensure that the loop body runs at least one time regardless of the condition.

Example scenarios include:

  • Displaying a menu to the user at least once before checking their choice.
  • Getting user input where you want to process the input at least once.

Summary of do-while Loop in C#:

  • The do-while loop executes the loop body at least once and checks the condition afterward.
  • It is useful for scenarios where the initial execution is necessary before validating a condition.
  • You can use break to exit the loop early or continue to skip to the next iteration, similar to other loops.