The structure of while
loop is as follow:
while (condition)
{
statements
}
The condition
is tested before the statements
in the body of the while loop is executed. Hence, while
loop executes zero or more times (Docs, 2018).
int x = 10;
while (x < 20)
{
Console.WriteLine($"Value of x: {x}");
x++;
}
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). while (C# Reference). Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/while