C# While Loop - c# - c# tutorial - c# net

C# While Loop
What is While loop in C#?
- In C#, the while statement executes a statement or a block of statements until a specified expression evaluates to false

Syntax:
while (condition is true)
{
code will be executed;
}
C# Sample Code - C# Examples:
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace wikitechy_while_loop
{
classProgram
{
staticvoid Main(string[] args)
{
int a = 5;
while (a < 10)
{
Console.WriteLine("Current value of a is {0}", a);
a++;
}
Console.ReadLine();
}
}
Code Explanation:

- In this example, int a = 5is an integer variable.
- while(a>10) is the while loop which will continue to run as long as a=5 is less than10. In this example Console.WriteLine,the Main method specifies its behavior with the statement "Current value of a is {0}".WriteLine is a method of the Console class defined in the System namespace. This statement reasons the message"Current value of a is {0}"to be displayed on the screen.
- a++; specifies to a =5 will increase by 1 each time the loop runs (a++)variable till 9.
- Here Console.ReadLine();specifies to reads input from the console. When the user presses enter, it returns a string.
Sample C# examples - Output :

- Here in this output, the while loop will continue to run as long as a=5 is less than 10 which will increase by 1 each time the loop runs (a++)till 9.