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.
The above is the detailed content of C# do-while loop. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages such as Python. Be careful when modifying and back up the original files.
