C# Do While Loop Example

  • C#
  • 1 min read

Do While loop is very similar to While loop in C#, but the one difference is, While loop will never execute if the specified condition returns false, and Do While loop runs the statements at least once. So if your need is something like that use Do While loops in C#. Below are its syntax and example:

Syntax

do statement while (expression);

Example

using System;

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

Output

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

See also: