While Loop
While loop is pretty much similar to C# for loop. But unlike for loop, while loop depends upon certain condition to be met where we cannot provide any kind of incrementation. However, you can do so by modifying your code. The condition basically returns true or false, it depends upon which scenario you want to choose. For example, loop certain piece of code until that condition is true or false. Make sure, that loop at some point should meet that condition, otherwise you will end up with an infinite loop and that will freeze your application and make it unresponsive to the end user. The c# sample code is given below:
using System; namespace MyHelloWorld { class Program { static void Main(string[] args) { int number = 0; while (number < 10) { Console.WriteLine(number); number++; } } } }
In the above C# sample code, we are making use of while loop and looping that small piece of code while the variable number is less than 10 and printing out the value of variable.
Output:
0 1 2 3 4 5 6 7 8 9