C# While Loop
C# While Loop is used to execute a set of statements in a loop based on a condition.
Syntax of C# While
Following is the syntax of While Loop in C#.
</>
Copy
while(condition){
/* statement(s) */
}
where
whileis the keyword.conditionis a boolean expression.statement(s)are a set of C# statements which will be executed in a loop.
Example 1 – C# While Loop
Following program is a simple example for while loop. The C# program prints numbers from 5 to 9.
Program.cs
</>
Copy
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
int a=5;
while(a<=9){
Console.WriteLine(a);
a++;
}
}
}
}
Output
PS D:\workspace\csharp\HelloWorld> dotnet run
5
6
7
8
9
