while syntax in c# code example

Example 1: while loop in c#

int i = 0;

while (i < 10)
{
    Console.WriteLine("Value of i: {0}", i);

    i++;
}

Example 2: c# do while or

int sugar = 0;
int salt = 0;

do {
    bottle1.choose();
    bottle2.choose();
    if ((bottle1 == 'Sugar') && (bottle2 == 'Sugar'))
    {
        Console.Write("Sugar");
        sugar++;
    }
    else if ((bottle1 == 'Salt') && (bottle1 == 'Salt'))
    {
        salt++;
        Console.Write("Salt");
    }
    else
    {
        Console.Write("None");
    }
} while ((salt < 10) || (sugar < 10));

Example 3: C# While Loop01

int counter = 0;
while (counter < 10)
{
  Console.WriteLine($"Hello World! The counter is {counter}");
  counter++;
}

Tags:

Php Example