ideas | discover | explore

Thursday, October 6, 2011

C# 9 - while-loop statement

1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.

    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0;
            
            while (counter < 10)
            {
                Console.WriteLine("Hello World from a while loop.");
                counter++;
            }
 
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
    }

3. Press F5 to run and see the results.

4. Thank you.


The while-loop statement is used perform a piece of code multiple times as long as the condition set is evaluated to true. Its syntax is:

while(<condition>)
{
    //code to execute
}

No comments: