![]() ![]() Let us understand do while loop with one real-time example. In the below example we are printing the numbers from 1 to 5 using the do while loop. For a better understanding, please have a look at the following diagram which shows the flowchart of the do-while loop.Įxample to understand do while loop in C# Language: After executing the loop body, it will check for the condition, and if the condition is true then it will again execute the loop body and if the condition is false then it will terminate the loop. In the do-while loop, first, it will execute the loop body without checking the condition. ![]() Note: When you want to execute the loop body at least once irrespective of the condition, then you need to use the do-while loop else you need to use the while loop. Therefore, the statements written inside the do-while block are executed at least once whereas we cannot give a guarantee that the statements written inside the while loop are going to be executed at least once. The difference between the do-while loop and the while loop in C# is that the do-while evaluates its test condition at the bottom of the loop whereas the while loop evaluates its test condition at the top. ![]() So, what is the difference? Which one do we use? The condition expression must be a boolean expression. Please note that the while condition statement ends with a semicolon. And after the close curly braces, you need to write the while condition. In between the open and close curly braces, you can write the statements which you want to execute at least once. The loop is created by using the do keyword followed by open and close curly braces. The following image shows the syntax to use the do while loop in the C# language. Syntax to use Do While Loop in C# Language: If this is not clear at the moment then don’t worry understand this with one menu-driven program example. That means when the end user wants then the loop is going to terminate. The do-while loop is mainly used in menu-driven programs where the termination condition depends upon the end-user. That means we need to use the do-while loop where we need to execute the loop body at least once. first it will execute the loop body and then it will be going to test the condition. The do-while loop is a post-tested loop or exit-controlled loop i.e. At the end of this article, you will understand what is the do-while loop and when and how to use a do-while loop in the C# language with examples. Please read our previous articles, where we discussed While loop in C# Language with Examples. In this article, I am going to discuss the Do While loop in C# with Examples.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |