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:
C# Sample Code - C# Examples:
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.