Home > Article > Backend Development > C# do-while loop
Programming is fun, especially when you are working with the OOPs based concept. Because of the different requirements specified by the client, we may come through different situations for which solutions are different. Many of the times, there are situations where we want to repeat things in a particular order. We do not wish to the entire function or program to execute but an only specific block of statements for a limited number of times. The possible solution for these types of situations is Loops. There are many loops available in C#, like “for,” “while,” and “do-While” loop. In this article, we are going to discuss the “do-while” loop along with the examples, how it could help you to overcome situations like this. Today we’ll talk about the most widely used do-while Loop.
Like many other loops available in OOPs based languages, C# fully supports do-while Loop. Circuits are used to run a particular block of statements until the condition is true. Let’s understand how does this works.
“Do this while the condition is true.” In a layman term, suppose you are a programmer, and when you are at work, you write codes and execute. If we try to understand this situation in, do while loop aspect, it would be:
do { (write codes) (execute codes) } While { (You are on duty) }
Till the time, the while condition is true, the code block which is written in do block will keep executing. A do-while loop is mostly used where the number of repetitions is not known. Exactly we do not know how many times the code will run until the condition is checked at runtime. Any which ways, this will execute at least one time. Let’s discuss with a real-life example for the loops available with c#.
Below are the things you should know before using the do-while loop.
The syntax for using a do-while:
do { //code that needs to be executed } While(condition);
Whatever that is required when the condition is true, should be put in the “do” part of the code. The condition should be defined in “while” part of the code.
As can be seen, the statement will execute, regardless of the condition is valid for the first time. Once the report is completed, the situation will be checked. If the condition is true, it will go back to the statement. However, if the state is false, it will exit the Loop and move on to the next part of the program.
Examples
using System; public class Example { public static void Main(string[] args) { int i = 1; do { Console.WriteLine(i); i++; } while (i <= 10) ; } }
Explanation:
In the above example the integer “i” has been defined. In the do part we have established what to do with this integer. We have applied a ++ operator that will add 1 to its previous value and print it. This will continue to happen until the integer “i” is equals or less than 10. Hence the output of this program will be:
If the condition is set to be a Boolean value, the do-while loop will continue to execute infinitely. It is suggested that a numeric condition is given. The endless loop can be terminated by pressing Ctrl + c,
Example:
using System; public class Example1 { public static void Main(string[] args) { do{ Console.WriteLine("endless loop"); } while(true); } }
Output
//can be terminated by pressing Ctrl + c
The above is the detailed content of C# do-while loop. For more information, please follow other related articles on the PHP Chinese website!