while Loop in C#
Last Updated :
25 Aug, 2020
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:
using System;
class whileLoopDemo {
public static void Main()
{
int x = 1;
while (x <= 4) {
Console.WriteLine( "GeeksforGeeks" );
x++;
}
}
}
|
Output:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
Example 2:
using System;
class whileLoopDemo {
public static void Main()
{
int x = 10;
while (x > 4) {
Console.WriteLine( "Value " + x);
x--;
}
}
}
|
Output:
Value 10
Value 9
Value 8
Value 7
Value 6
Value 5
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...