The difference between While and Do Whileis :
Whileloop checks the condition given with it first and if the condition is true then it enters inside the loop . That means the statements inside the loop is executed only if the condition is true.
while (true)
{
}
Do While executes the statements inside the loop and then checks the condition , that means the code inside the loop is executed atleast once .
do
{
} while(true)
Lets check the difference with the below examples for both :
Example 1:
int i=1;
while (i<1)
{
System.Console.WriteLine(Value of i={0} + i);
i++;
}
// Condition is false so no Output will be generated
Example 2:
int i=1;
do{
System.Console.WriteLine(Value of i={0} + i);
i++;
} while (i<1)
// Statement inside the loop will be executed once.Hence the Output is : Value of i=1
0 Comment(s)