Friday 17 August 2018

C#: while loop

while statement executes a block of statements until a particular condition is true

Syntax
while (expression) {
      //statements
}

The expression in the while statement must evaluates to a Boolean value, otherwise compiler throws an error. While statement first evaluates the expression, until the expression evaluates to true, it executes the code inside the while block, otherwise it comes out of the while loop.

Program.cs

using System;

class Program
{
    static void Main(string[] args)
    {
        int var1 = 0;
        while (var1 < 10)
        {
            Console.WriteLine(var1);
            var1++;
        }
    }
}

Output

0
1
2
3
4
5
6
7
8
9



Previous                                                 Next                                                 Home

No comments:

Post a Comment