By 22/07/2015 13 Comments

C# Do-While Loop

  • In C# Do-While Loop works same as while loop but unlike while loop which checks its condition at the top but do while loop checks its condition at the end means at bottom.
  • do while loop always must executes at least one time because it checks its condition at the end, if that condition becomes true it will keep continue its execution, at the other hand if the condition becomes false the execution will be exited or bypassed by compiler.

Syntax:

do {
statement;
} while (condition);

STATEMENT DESCRIPTION
do do-while loop declaration.
{ This parenthesis indicates the starting scope or body of do while loop, inside this scope you can specify any multiple statements, expression or anything which do you want to be repeated by do while loop. NOTE: This parenthesis is an optional, if you do not specify it than only single statement or expression will be repeated by do while loop, but if you want to repeat more than one statement or expressions than you must specify these parenthesis. This is also called starting scope of do while loop.
statement; This can be anything to be repeated by do while loop, mostly in do while loops it can be information from databases.
} Closing scope of do-while loop.
while (condition); while keyword indicates its declaration, where (condition) is a boolean expression such as (number <= 10).


Example:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // Initialize an int type number with 1.
            int number = 1;

         // Following is the declaration of do while loop.
            do
            { // Start do-while loop scope.
                Console.WriteLine("Number: {0}", number);

                // number variable is increament each time.
                number++;

                // A boolean condition is being checked at the end.
            } while (number <= 10); // End do-while loop.

            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 

    /*
   The Output will be:
   Number: 1
   Number: 2
   Number: 3
   Number: 4
   Number: 5
   Number: 6
   Number: 7
   Number: 8
   Number: 9
   Number: 10
  */
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .