while Loop in C#
Looping in a programming language is a way to execute a statement or a set of statements multiple number of times depending on the result of the condition to be evaluated. while loop is an Entry Controlled Loop in C#.
The test condition is given in the beginning of the loop and all statements are executed till the given boolean condition satisfies, when the condition becomes false, the control will be out from the while loop.
Syntax:
while (boolean condition)
{
loop statements...
}
Flowchart:

Example 1:
// C# program to illustrate while loop using System; class whileLoopDemo { public static void Main() { int x = 1; // Exit when x becomes greater than 4 while (x <= 4) { Console.WriteLine("GeeksforGeeks"); // Increment the value of x for // next iteration x++; } } } |
GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
Example 2:
// C# program to illustrate while loop using System; class whileLoopDemo { public static void Main() { int x = 10; // Exit when x becomes less than 5 while (x > 4) { Console.WriteLine("Value " + x); // Decrement the value of x for // next iteration x--; } } } |
Value 10 Value 9 Value 8 Value 7 Value 6 Value 5
Recommended Posts:
- C# | foreach Loop
- C# | Using foreach loop in arrays
- C# Tutorial
- Role of SemiColon in various Programming Languages
- File.WriteAllBytes() Method in C# with Examples
- File.SetAttributes() Method in C# with Examples
- File.SetLastWriteTimeUtc() Method in C# with Examples
- File.GetAttributes() Method in C# with Examples
- File.GetLastAccessTimeUtc() Method in C# with Examples
- File.GetLastAccessTime() Method in C# with Examples
- File.OpenText() Method in C# with Examples
- File.OpenWrite() Method in C# with Examples
- File.OpenRead() Method in C# with Examples
- File.Delete() Method in C# with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

