C# while loop
As long as the given condition is true, in C#whilethe loop statement will repeatedly execute a target statement.
Syntax
In C#whileLoop syntax:
while(condition)
{
statement(s);
}
Here,statement(s)It can be a single statement or a code block composed of several statements.conditionIt can be any expression; when it is any non-zero value, it is true. When the condition is true, the loop is executed.
When the condition is false, program flow continues with the next statement immediately following the loop.
Flowchart
Here,whileThe key point of a loop is that the loop may not be executed at all. When the condition is tested and the result is false, the loop body is skipped, and the next statement immediately following the while loop is executed.
Example
Example
namespace Loops
{
class Program
{
static void Main(string[] args)
{
/* local variable definition */
int a = 10;
/* while loop execution */
while (a < 20)
{
Console.WriteLine("Value of a: {0}", a);
a++;
}
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following results:
a 的值: 10 a 的值: 11 a 的值: 12 a 的值: 13 a 的值: 14 a 的值: 15 a 的值: 16 a 的值: 17 a 的值: 18 a 的值: 19other extensions