C# do while

Summary: in this tutorial, you’ll learn how to use the C# do while statement to execute a block of code repeatedly as long a condition is true.

Introduction to the C# do while statement

The C# do while statement executes one or more iterations as long as a condition is true.

The following shows the syntax of the do while statement:

do
{
    // statement
} while (expression);Code language: JavaScript (javascript)

Unlike the while statement, the do while statement checks the expression at the end of each iteration. Therefore, it’s called a posttest loop.

The do while statement will always run the first iteration regardless of the expression’s result. And it’ll execute the block repeatedly as long as the expression is true.

Inside the loop, you need to change some variables to make the expression false so that the loop will terminate after some iterations. Otherwise, you’ll have an indefinite loop.

The following flowchart illustrates how the do while statement works:

C# do while

C# do while statement examples

Let’s take some examples of using the do while statement.

1) Simple C# do while statement example

The following example shows how to use the do while statement to print out five numbers from 1 to 5:

int counter = 0;
do
{
    counter++;
    Console.WriteLine(counter);

} while (counter < 5);Code language: JavaScript (javascript)

Output:

1
2
3
4
5

How it works.

First, declare the counter variable and initialize its value to 0.

Second, increase the counter by one and display it in the do while block. Repeat this step as long as the counter is less than 5. Since the counter starts at one, the do while statement executes exactly five times.

2) Using the do-while statement to create a guessing game

The following illustrates how to use the do while statement to create the “Guess the Number” game. The program will generate a random number between 1 and 10.

And you’ll have a maximum of 4 turns. If your guess is higher or lower than a secret number, you’ll get a hint:

int guess = 0, yourNumber;
string hint;


// Get a random number between 1 and 10
int secretNumber = (new Random()).Next(1, 10);

Console.WriteLine("Pick a number between 1 and 10. You'll have 4 turns.");
do
{
    guess++;

    // Get the user input
    Console.Write($"Turn #{guess}. Your number:");
    yourNumber = Convert.ToInt32(Console.ReadLine());

    // Check the input number with the secret number
    if (yourNumber < secretNumber)
    {
        hint = $"Your guess: {yourNumber}, which is too low.";
    }
    else if (yourNumber > secretNumber)
    {
        hint = $"Your guess: {yourNumber}, which is too high.";
    }
    else
    {
        hint = $"Bingo! It took you {guess} turns to guess the secret number {secretNumber}.";
    }
    Console.WriteLine(hint);

} while (guess < 4 && yourNumber != secretNumber);


if (yourNumber != secretNumber)
{
    Console.WriteLine("Oops! you lost.");
}
Code language: JavaScript (javascript)

How it works.

First, declare the guess variable to store the number of guesses and yourNumber variable to store the input number:

int guess = 0, yourNumber;

Second, get a random integer between 1 and 10:

int secretNumber = (new Random()).Next(1, 10);Code language: JavaScript (javascript)

This code creates a new instance of the Random class and call the Next() method to generate a random number between 1 and 10. Note that you’ll learn more about classes and methods in the later tutorial.

Third, show the message on the screen:

Console.WriteLine("Pick a number between 1 and 10. You'll have 4 turns.");Code language: JavaScript (javascript)

Fourth, use the do while statement to get the user’s input number.

The loop will continue if the number of guesses is less than 4 and the input number is different from the secret number.

Inside the do while loop:

1) Increase the value of the guess variable by one in each iteration:

guess++;

2) Get the input number:

Console.Write($"Turn #{guess}. Your number:");
yourNumber = Convert.ToInt32(Console.ReadLine());Code language: JavaScript (javascript)

3) Check the input number with the secret number and give a hint if the input number is lower or higher. Also, print a message if the input number equals the secret number:

if (yourNumber < secretNumber)
{
    Console.WriteLine($"Your guess: {yourNumber}, which is too low.");
}
else if (yourNumber > secretNumber)
{
    Console.WriteLine($"Your guess: {yourNumber}, which is too high.");
}
else
{
    Console.WriteLine($"Bingo! It took you {guess} turns to guess the secret number {secretNumber}.");
}Code language: JavaScript (javascript)

Finally, print a message if the input number is not equal to the secret number:

if (yourNumber != secretNumber)
{
    Console.WriteLine("Oops! you lost.");
}Code language: JavaScript (javascript)

The following shows a test:

Pick a number between 1 and 10. You'll have 4 turns.
Turn #1. Your number:5
Your guess: 5, which is too low.
Turn #2. Your number:6
Your guess: 6, which is too low.
Turn #3. Your number:7
Your guess: 7, which is too low.
Turn #4. Your number:8
Bingo! It took you 4 turns to guess the secret number 8.

Summary

  • Use the do while statement to execute a block of code repeatedly as long as a condition is true.
Was this tutorial helpful ?