Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between Do while and While loop in C#

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.35k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: