In c#, the Do-While loop is used to execute a block of statements until the specified expression return as true.
Generally, in c# the do-while loop is same as the while loop, but only the difference is while loop will execute the statements only when the defined condition returns true, but the do-while loop will execute the statements at least once because first it will execute the block of statements and then it will check the condition.
Generally, do and while keywords are used to create a do...while loop in C#. Following is the syntax of defining a do-while loop in c# programming language to execute the block of statements until the defined condition evaluates as false.
If you observe the above syntax, the do-while loop starts with the do keyword followed by a block of statements and while with a parameter called boolean_expression.
Here the body of the do-while loop will be executed first, and the boolean_expression will be evaluated. If boolean_expression returns true again, the statements inside of the do-while loop will be executed.
In case the boolean_expression is evaluated to false, then the do-while loop stops the execution of statements, and the program comes out of the loop.
Following is the pictorial representation of the do-while loop process flow in the c# programming language.
Now we will see how to use the do-while loop in the c# programming language with examples.
Following is the example of using a do-while loop in c# programming language to execute the block of statements based on our requirements.
If you observe the above example, first we are executing the statements within the do-while loop and increasing the variable i (i++) value to 1 by using the increment operator.
After that, the condition (i <= 4) will be evaluated, and again it will execute the block of statements if the condition returns true otherwise, it terminates the loop.
When we execute the above c# program, we will get the result below.
If you observe the above result, the do-while loop has been executed until it matches the defined condition (i <= 4), and the program came out of the loop whenever the defined condition returns false.
In c#, we can use one do-while loop within another do-while loop to implement the application based on our requirements.
Following is the example of implementing a nested do-while loop in the c# programming language.
If you observe the above example, we used one do-while loop within another do-while loop to achieve nested do-while loop functionality in our application based on our requirements.
When we execute the above c# program, we will get the result below.
If you observe the above example, both do-while loops got executed and returned the result based on our requirements.
In c#, we can exit or terminate the execution of a do-while loop immediately by using the break keyword.
Following is the example of using the break keyword in a do-while loop to terminate the loop's execution in the c# programming language.
If you observe the above example, whenever the variable (i) value becomes 2, we terminate the loop using the break statement.
When we execute the above c# program, we will get the result below.
This is how we can use the break statement with a do-while loop to terminate the loop's execution based on our requirements.