Do While Loops

Visual C# do while Loop

It is used when it is not known how many times the loop will return, as in the while loop. The most important difference is that the condition is checked at the end of the loop. Therefore, when we use the do while loop, it means that the operations in the loop will be performed at least once.

Its structure is as follows:

do

{

Transactions...

}

while( running condition of the loop );

Do While Loop Example

The user will enter a number between 0 and 100 in the text box, the computer will generate a random number consecutively and compare it with this number. The loop will continue until the generated number is the same as the entered number. Each number produced by the computer will be written in label1, and the number of attempts will be written into label2 using a counter.

int counter = 0;
 
label1.Text = "";
 
int a = int.Parse(textBox1.Text);
int generatedNumber;
 
Random random = new Random();
 
do
{
   generatedNumber= random.Next(0, 100);
   counter++;
   label1.Text += generatedNumber.ToString() + "\n";
}
while (generatedNumber != a);
 
label2.Text=counter.ToString();

Do While Loop Example 

A Simple Game Like Black Jack:

With the InputBox object, the program that asks the user to enter numbers one after the other until the sum exceeds 21, finishes the loop when the total exceeds 21, and writes the number of times and the sum of the numbers to the screen:

int number;
int total = 0;
int counter = 0;

do
{
   number = Convert.ToInt32(Microsoft.VisualBasic.Interaction.InputBox("New Issue", "Title", "", 200, 200));
   total = total + number;
   counter++;
}
while (total < 21);

label1.Text = counter+ " rounds: " + total;

Number Guessing Game with Do While Loop

private void Startgame_Click(object sender, EventArgs e)
{
  attempts.Text = "";
 
  int counter= 10;
  int points= 110;
  txt1.Text = counter.ToString();
  string guessText;
  int guess;
 
  Random random = new Random();
  int generatedNumber= random.Next(0, 1000);
 
   do
    {
 
     guessText= Microsoft.VisualBasic.Interaction.InputBox("Enter your guess:", "Forecast", "", 400, 400);
 
     if (guessText != "") guess= int.Parse(guessText);
     else guess = 0;
 
     attempts.Text += guess.ToString() + "\n";
 
     points -= 10;
 
       if (guess > generatedNumber) hint.Text = "Down";
       else if (guess < generatedNumber) hint.Text = "Up";
       else if (guess == generatedNumber)
       {
          greeting.Text = "CONGRATULATIONS...\nYour score: " + points;
          hint.Text = "";
        }
 
        counter--;
        txt1.Text = counter.ToString();
        if (counter == 0)
        { 
           congratulation.Text = "GAME OVER !!!\nNumber Kept: "+generatedNumber.ToString();
           hint.Text = "";
        }
    }
    while (guess != generatedNumber && counter>0);
 
}

 

visual C# do while loop tutorials, do while examples, use of do while loop, do while

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 893 times.

Online Users: 3864



do-while-loop