The do...while
loop in C# is a type of loop that ensures that a block of code is executed at least once before checking a condition. Unlike the while
loop, where the condition is evaluated before entering the loop, the do...while
loop evaluates the condition after the loop body has been executed. This makes it useful for scenarios where you need to execute code at least once, regardless of the condition.
In this tutorial, we will cover everything you need to know about the do...while
loop, from its syntax to practical examples and common use cases.
do...while
Loop?The do...while
loop in C# is a post-test loop, which means that the condition is checked after the block of code has been executed. This guarantees that the loop will execute the code inside at least once, even if the condition is false
from the start. This behavior makes it ideal when you want the code to run at least once before evaluating the loop condition.
false
.do...while
LoopThe do...while
loop has a simple structure, starting with the do
keyword followed by a block of code inside curly braces {}
. After the loop body, the while
keyword follows with a condition inside parentheses ()
.
do
{
// Code to be executed
} while (condition);
int counter = 0;
do
{
Console.WriteLine($"Counter: {counter}");
counter++;
} while (counter < 5);
In this example:
counter
starting from 0.counter < 5
is checked. If true
, the loop continues; if false
, the loop stops.do...while
Loop WorksThe do...while
loop works by:
while
condition is evaluated.true
, the loop repeats. If false
, the loop terminates.true
, repeat from step 1; otherwise, stop.do...while
Loop in C#do...while
Loopint count = 1;
do
{
Console.WriteLine($"Count: {count}");
count++;
} while (count <= 5);
count
was greater than 5, the loop would execute at least once.int input;
do
{
Console.WriteLine("Please enter a number between 1 and 10:");
input = Convert.ToInt32(Console.ReadLine());
} while (input < 1 || input > 10);
Console.WriteLine($"You entered a valid number: {input}");
do...while
loop ensures that the user is prompted at least once.int total = 0;
int number;
do
{
Console.WriteLine("Enter a positive number to add to the total, or -1 to stop:");
number = Convert.ToInt32(Console.ReadLine());
if (number != -1)
{
total += number;
}
} while (number != -1);
Console.WriteLine($"The total sum is: {total}");
-1
to stop the loop.do...while
Loopdo...while
loop is ensuring valid input by repeatedly asking for user input until a correct value is entered. Since the prompt should appear at least once, the do...while
loop is ideal for this scenario.
string password;
do
{
Console.WriteLine("Enter your password:");
password = Console.ReadLine();
} while (password != "secret123");
Console.WriteLine("Access granted!");
bool isReady;
do
{
// Simulate checking if a device is ready
Console.WriteLine("Checking if device is ready...");
isReady = CheckDeviceStatus();
} while (!isReady);
Console.WriteLine("Device is ready!");
do...while
loop to repeat a task until the user decides to stop.
string command;
do
{
Console.WriteLine("Enter command (type 'exit' to quit):");
command = Console.ReadLine();
Console.WriteLine($"You entered: {command}");
} while (command.ToLower() != "exit");
This loop will continue running until the user types "exit".do...while
loop guarantees that the loop body will execute at least once, as the condition is checked after the execution.do...while
loops to ensure that you do not create an infinite loop by not providing a way for the condition to eventually become false
.do...while
loop is similar to the while
loop but checks the condition after executing the loop body, while the while
loop checks the condition before.The do...while
loop is a powerful and versatile looping construct in C#. It provides an easy way to ensure that a block of code is executed at least once, making it the perfect choice when you need to guarantee that some action happens before evaluating a condition. Whether you're validating user input, interacting with external systems, or performing repetitive tasks, the do...while
loop can be an invaluable tool in your programming arsenal.
By understanding its structure and use cases, you'll be able to write clearer and more efficient code when a pre-loop condition check isn't suitable. Keep the do...while
loop in mind whenever you encounter situations where code must be executed at least once!