While Loop

While Loop
Rating: 5 - 1 Votes

While Loop

C#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:

 

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: