Updated March 15, 2023
Introduction to 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.
Detailed Explanation
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#.
For Loop, While Loop and do-while Loop: A difference
- All these three loops are used to repeat the specific block execution of a particular condition. However, there are a few differences between these three. Let’s take an example if you want to travel, and you got three options, your vehicle, flight, and bus.
- In the first condition, if you want to travel with your vehicle, the available petrol (condition) should be checked. You will travel only if petrol (condition) for the distance you want to go (repetition) is available. Similarly, For Loop will first check the condition and know the number of times the Loop will repeat, then run the function.
- The other condition, if you want to travel on a flight. First, you will have to book a ticket for the plane. First, you will have to satisfy the condition then you can board the flight. That’s how While Loop works. The function will run only if the condition is met. Several loops are not known.
- Third, if you want to go by bus, first you board the bus and then take the ticket. That’s how the do-while Loop works. First, it will proceed then it will check the condition and will continue to repeat until the condition is true.
Explain Syntax
Below are the things you should know before using the do-while loop.
- The keywords that are to be used are: do and while
- The Loop will execute at least one time regardless of what condition says.
- The condition will be checked after the body of the Loop is executed.
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.
Flowchart of C# Do-While Loop
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:
Infinite do-while Loop
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
- It is notable that, even if the condition does not meet defined in while part, it will still show the initial value of the integer, which is 1.
- In the first round, it does not check the condition. Condition is checked only after the execution of the statement.
Conclusion
- C# is a powerful language for software development, and it is essential that you master even the little things. If the right functions of the language not used at the right time, not only the result get affected but also it shows the incompetence of the program.
- The do-while Loop is widely used while making programs in certain conditions, and the pros and cons should be well known by the program to use it effectively.
- In a nutshell, it repeats the function until the defined condition is true. However, the difference between while loop and do-while loop is, while Loop will execute only when the state is true, but do-while will run even one time regardless if the condition is met.
Recommended Articles
This has been a guide to the C# do-while loop. Here we have discussed the differences of While Loop and do-while loop, a flowchart of C# Do-While Loop with respective syntax and code samples. You can also go through our other suggested articles to learn more –