C# do While loop

tutorial and example
tutorial and example

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the number of iteration is not known in advance and you want to run the loop at least once.

Syntax:

Let’s see a simple example of do while loop.

C# Do while loop example: A user responsive program

Output:

Nested do while loop:

Like for and while loop, we can nest do while loop also. The outer loop will run n number of times for each iteration of the outer loop.

Let’s see a simple example of nested while loop:

Output:

Infinite do while loop:

An infinite do while loop is the one which iterate infinite times means which is never going to stop by itself. We can make do while loop an infinite loop by providing a condition in the while statement which is never going to fail.

Let’s see a simple example of infinite do while loop