Latest web development tutorials

C # while loop

C # loop C # loop

As long as the given condition is true, C # thewhile loop will repeat a target statement.

grammar

In C #while loop syntax:

while (condition)
{
   statement (s);
}

Here, statement (s) may be a single statement, it can also be a block composed of a few statements.condition can be any expression, any non-zero when the values are true. Conditions for the execution of the loop when true.

When the condition is false, program flow will continue with the next statement followed the loop.

flow chart

The while loop in C #

Here, the key pointswhileloop is a loop may not execute. When the condition is tested and the result is false, the loop body will skip directly to the next statement immediately while loop.

Examples

using System;

namespace Loops
{
    
    class Program
    {
        static void Main (string [] args)
        {
            / * Local variable definitions * /
            int a = 10;

            / * While loop is executed * /
            while (a <20)
            {
                Console.WriteLine ( "a value: {0}", a);
                a ++;
            }
            Console.ReadLine ();
        }
    }
} 

When the above code is compiled and executed, it produces the following results:

a value of: 10
a value of: 11
a value of: 12
a value of: 13
a value of: 14
a value of: 15
a value of: 16
a value of: 17
a value of: 18
a value of: 19

C # loop C # loop