do (C# Reference)
The do statement executes a statement or a block of statements enclosed in {} repeatedly until a specified expression evaluates to false. In the following example the do loop statements execute as long as the variable y
is less than 5.
Example
// statements_do.cs
using System;
public class TestDoWhile
{
public static void Main ()
{
int x = 0;
do
{
Console.WriteLine(x);
x++;
}
while (x < 5);
}
}
Output
0 1 2 3 4
Remarks
Unlike the while statement, the body loop of the do statement is executed at least once regardless of the value of the expression.
C# Language Specification
For more information, see the following sections in the C# Language Specification:
5.3.3.8 Do statements
8.8.2 The do statement
See Also
Reference
C# Keywords
The do-while Statement (C++)
Iteration Statements (C# Reference)