Home » C# » C# While Loop Example

C# While Loop Example

The definition of while loops are "while the specified condition is true, do this task.". In C#, while loops require an expression to evaluates to true or false, and while the condition is true, it executes the block of code. Below are the syntax and example:

Syntax

while (expression)
{
statement;
}

Example

using System;

namespace WhileLoopExample
{
    class WhileLoopExample
    {
        static void Main(string[] args)
        {
            int i = 0;
            while (i < 10)
            {
                Console.WriteLine("i: {0}", i);
                i++;
            }
            Console.ReadLine();
            return;
        }
    }
}

Output

i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9

See also: