c sharp

C# Do While

The loops are the mainstream structures in any programming language for these are used to minimize the complexity and perform many iterative tasks. One of the most-known loops is the “while” loop which is used to execute the part of a code until the given condition. The condition chooses whether the loop will carry on executing or breaking. This article is to discuss the use of the Do-while loop in C#. The “do-while” loop is like the “while” loop but more enhanced and presentable. Its “do” part always executes its code part at least a single time whatever the situation is.

Example 01

Getting started with the use of the “do-while” loop in C#. Before that, create a C# file to use for coding. We have been trying the simple “touch” instruction on Ubuntu 20.04 shell to do so.

Before illustrating the do-while loop, we must take a look at the simple while loop in C#. For this, we need a library called “System” which is a must in any C# code for utilizing general-purpose reading and writing functions. This has to be included via the “using” keyword. We have to add a namespace in our code. We have been naming this user-defined namespace as “Test”. After this, we have been creating a “Test” class within the namespace “Test”.

This is the utmost simple construction of any c# code. The execution of C# code always starts from its main driver program, thus we have included it within the “Test” class. We will be performing everything within it without using the other user-defined function. Within our main() method, we have initialized an integer type variable “z” with its value “1”. This variable will be utilized as a counter for the while loop and determines the number of iterations for while loop execution.

We have started the while loop with the condition “z<8”. This means, our loop will continue to execute its inner part until the value of variable “Z” is less than 8. While the condition in the “while” loop is true, it will continue to display the current iteration number on the shell using the Console class’s “WriteLine” built-in function statement. After displaying the iteration number, the value of variable “z” will be incremented by 1 using the post-increment operator. The while loop, main() method, class, and namespace “Test” are completed here. This code needed to be saved with the “Ctrl+S” and we have to open the shell again for its execution.

We need to compile this C# code to make it bug-free via the “mcs” Ubuntu compiler for “C#”. This compilation will create an “exe” file for this code. We need to use the “mono” runtime executor of c# to run the newly generated “exe” file for this code as shown beneath. The result is showing us the display of the iteration number for each iteration using the “z” variable.

This was on the use of a simple “while” loop in any C# basic code. Now, we will be using the “do-while” loop in our C# code. For this, we will be updating the above code. So, we will be initializing the variable “z” to “0” instead of “1” and initializing a string variable “str” with a value “Hello World”. Then, we will be starting the “do-while” loop with its “do” part first. Within this part, we will be adding all the statements or code that will be executed upon the loop starts and continue to execute until the loop ends.

So, it contains the increment of variable “z” and the WriteLine() function to display the value of “z” and print the string “str” value i.e. “Hello World”. The “do” part of the loop is started and finished with curly brackets.  After the “do” part, we have used the ‘while’ condition part taking “z<5” as its condition. This means until the value of “z” is less than 5, the “do” part of the loop will continue to execute its inner statements. This program is complete here and ready for use on the shell.

After compiling and executing the code, we have a total of 5 iteration results as below. You can see that there is not much difference between the simple while loop and the “do-while” loop. But, the “do-while” loop is more expressive, more presentable, and easier to understand.

Example 02

Let’s use the “do-while” loop to perform some mathematical operations in C#. So, we have decided to find out the sum using two variables. Within the main() function, we have initialized 0 to “z” and 3 to “sum”. The “do” part will continue to add up the previous value of the sum and z together and override it within the “sum” variable. The Console.WriteLine() statement is here to display the current iteration value of “z” and sum at each iteration. Subsequently, the value of “z” will be incremented by 2. The “while” part illustrated that the loop will continue to work until the value of “z” is less than 15.

After running this c# code, we have shown the result below where the only even iterations have been counted as per “z=z+2”. The result of the “sum” variable at iteration got displayed.

Example 03

The C# do-while loop can also be utilized to simply multiply numbers. So, we have decided to make a table pattern of “17”. For this, we have initialized “I” to 1 and “t” to 17 and declared an integer variable “mul” within the main() method. The “do” part has been started with the calculation of the variable “mul” value using the variable “t” and “I”. We have been using the “Console.WriteLine()” function line to display the variable “t” multiplied by variable “I” equals to variable “mul” in a table format.

After this, the value of iterator “I” will be summed up by 1 at each iteration. After the “do” part ends, we have stated the condition within the “while” part as “i<=10”. This means we will be calculating the table of 17 up to 10 iterations and it will continue to display each iteration value on the shell. Let’s save the updated code to see its results on the shell after compiling and executing it.

We have compiled the C# code file and executed its “exe” file using the “mcs” and “mono” instructions presented below. The table of 17 has been successfully displayed on our shell with a proper format and accurate multiplication values at each iteration i.e. 1 to 10.


We have elaborated the use of the C# do-while loop in the introduction  by comparing it with the simple “while” loop. After that, we explained it by taking some of the simplest examples for the do-while loop in C#. The examples has simple “while” loop and demonstrated the simple syntax and use of the do-while loop within our first example. Then, we have used the do-while loop to calculate the sum of numbers and calculated the multiplication result.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.