C# While Loop

tutorial and example
tutorial and example

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on the user or not known In advance. Let’s see a simple example of while loop.

Syntax

C# While loop example:

Output

C# nested while loop:

Nested while loop is almost similar to nested for loop. We can use while loop inside a while loop. The inner will execute n number of time for each iteration of outer loop.

C#  Nested While loop Example:

Output

C# infinite while loop example:

If we put a condition in while loop that never fails then the loop becomes an infinite loop .