The structure of the do...while
loop is as follow:
do
{
statements
} while (condition);
As the condition
is evaluated after executing the statements
, do…while
loop is guaranteed to be executed at least once (Docs, 2018).
int x = 10;
do
{
Console.WriteLine($"Value of x: {x}");
x++;
} while (x < 20);
Output:
Value of x: 10
Value of x: 11
Value of x: 12
Value of x: 13
Value of x: 14
Value of x: 15
Value of x: 16
Value of x: 17
Value of x: 18
Value of x: 19
Live-code example
References
- Docs, M. (2018, May 28). do (C# Reference). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/do