CodeGym /Courses /C# SELF /do-while Loop: Getting to Know It and Some Gotchas

do-while Loop: Getting to Know It and Some Gotchas

C# SELF
Level 4 , Lesson 3
Available

1. Getting to Know the do-while Loop

Let's take a quick break and imagine: you decided to write a program that guesses a number the user is thinking of. You need the program to ask the user for a number at least once, and keep asking over and over until it guesses right. But if the condition isn't true on the first try, a regular while won't even go inside the loop.

This is exactly where the do-while loop comes in. Unlike while, it will always run the loop body at least once—even if the condition is false right away. It's like getting free gum no matter if you bought something or not.

do
{
    // Loop body: this code will run at least once
}
while (condition);
Syntax of the do-while loop

Notice the semicolon ; at the end after the condition's parenthesis! In a regular while it's not there, but here it is. Just a little trap for beginners :)

Steps of the do-while loop

  1. Enter the loop body (always at least once).
  2. Check the condition.
  3. If the condition is true—repeat the body.
  4. If the condition is false—exit the loop.

2. Difference from a Regular while

Let's remember the main scenario for a regular while:

while (condition)
{
    // Loop body
}

The body won't run even once if the condition is false from the start!

Now an example with do-while:

do
{
    // Loop body
}
while (condition);

The loop body will run at least once, even if the condition is false right away!

Let's show the difference with some code:

// Regular while
int count = 0;
while (count > 0)
{
    Console.WriteLine("count = " + count);
    count--;
}
// Nothing will be printed

// do-while
int count2 = 0;
do
{
    Console.WriteLine("count2 = " + count2);
    count2--;
}
while (count2 > 0);
// Will print "count2 = 0" even though the condition is false!

Sometimes this seems "weird," but sometimes this exact behavior saves your code (for example, with user input and data validation).

3. Practical Scenarios for Using do-while

Where do you see this loop most often?

Data Validation

The most popular scenario—you want the user to enter something valid (like only digits, or a password at least 6 characters long), and you keep asking until you get valid input.

string password;
do
{
    Console.Write("Enter a new password (at least 6 characters): ");
    password = Console.ReadLine();
}
while (password.Length < 6);

Console.WriteLine("Password accepted!");

In this example, the loop body runs at least once—even if the user enters a long password on the first try.

Menus in Console Apps

A super common and useful pattern: show a menu, wait for the user's command, and only exit the loop on a special command.

string command;
do
{
    Console.WriteLine("Menu:");
    Console.WriteLine("1. Show greeting");
    Console.WriteLine("2. Exit");
    Console.Write("Choose an action: ");
    command = Console.ReadLine();

    if (command == "1")
    {
        Console.WriteLine("Hello, world!");
    }
    else if (command != "2")
    {
        Console.WriteLine("Unknown command.");
    }
}
while (command != "2");

Console.WriteLine("Bye!");

4. Comparing while and Other Loops

Loop Type Condition Check How Many Times the Body Can Run When to Use
while Before 0 or more times When you don't know the number of repeats ahead of time
do-while After 1 or more times When you need to run at least once
for Before 0 or more times When you know the number of iterations for sure
2
Task
C# SELF, level 4, lesson 3
Locked
Basics of the do-while loop
Basics of the do-while loop
2
Task
C# SELF, level 4, lesson 3
Locked
Minimum value from entered numbers
Minimum value from entered numbers
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION