Do-while Loop:
Do-while Loop is same like while loop, Only the difference is that In do-while Loop, the statement will execute at least once. even if the given condition is False. Do-while Loop execute the statement first, and then check the condition. if the condition is true, will execute the statement second time, otherwise Program will be over as shown in figure.
Syntax:
The Syntax of the do-while Loop is:
do
{ //execute this block of code:
/*One or more C# Statements:*/
};
//Until this condition is true;
while (Condition) ;
The condition is tested at the end of the loop body instead of the beginning; therefore, the body of the loop will be executed at least once before the condition is checked. If the condition is true, we jump back to the beginning of the block and execute it again. A do-while loop is basically a reversed while loop. A while loop says, "Loop while the condition is true, and execute this block of code", a do-while loop says, "Execute this block of code, and then loop back while the condition is true".
Example:
Let's have an simple example to understand how do-while-loop actually work. In this example, we will create a simple Game. we will hide a number here, and can ask to our friend to Guess the number. This loop will be execute repeatedly until your friend Guess the right Number.
/*Example: Do-While Loop: InfoBrother:*/
using System;
namespace do_while
{
class Program
{
static void Main(string[] args)
{
int number = 51;
int guess;
Console.WriteLine("Enter your Guess: ");
guess = Convert.ToInt32(Console.ReadLine());
do //Execute This block of code:
{
Console.WriteLine("Bad Luck: \nGuess Again: ");
guess = Convert.ToInt32(Console.ReadLine());
}
//until the Entered number is equal to 51:
while (guess != number);
//When user will enter right guess; This statement will be Execute:
Console.WriteLine("Congrats: That's the Right Guess: ");
Console.ReadKey();
}
}
}
do-while Loop.
Enter your Guess
54
Bad Luck:
Guess Again:
74
Bad Luck:
Guess Again:
87
Bad Luck:
Guess Again:
47
Bad Luck:
Guess Again:
51
Congrats: That's the Right Guess:
Example:
Let's have another Useful Example, which will show some real meaning of do-while Loop. in this example we will make an small calculator. this calculator will ask you to enter numbers. this statement will execute again and again until you entered zero (0). when you will entered zero, the while condition will become false and loop will terminate. after termination the Loop, it will show you the sum of all entered Numbers.
/*Example: Do-While Loop: InfoBrother:*/ using System; namespace do_while { class Program { static void Main(string[] args) { int number = 0; int sum = 0; do //Execute this block of statement: { Console.WriteLine("Enter Number: "); number = Convert.ToInt32(Console.ReadLine()); sum = sum + number; //Addition } //until entered number is not equal to zero: while (number != 0); //When user will enter "0", this statement will execute: Console.Write("The Sum of Entered Numbers is: " + sum); Console.ReadKey(); } } }
do-while Loop.
Enter Number:
24
Enter Number:
54
Enter Number:
14
Enter Number:
24
Enter Number:
0
The Sum of Entered Numbers is: 116