Loops are mainly used to perform some particular task repeatedly in programming. Among many types of loops, the “While” loop is quite popular after the “for” loop. The “While” loop continues to work until the specified condition is true. After that condition got failed, the loop will automatically end. On the other hand, you can make a “while” loop execute without stopping it with the use of the “true” condition that will be discussed in the examples. Let’s get started with this tutorial guide on the use of the “while” loop in C#.

Pre-Installations

Start your console application from your application area of the Ubuntu 20.04 Linux system. Thus, the Ubuntu 20.04 system provides us with the “apt” package instruction to update it in one step. Use the shown-beneath instruction at your shell and your system is ready.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image1-17.png" data-lazy- height="55" src="data:image/svg xml,” width=”666″>

Before looking at an example for C# while loop, we have to configure a software platform for C# in Ubuntu 20.04. For this, we must install the “mono-runtime” C# framework in Ubuntu 20.04 that is used among developers to create cross-platform applications for “.NET”. Before the installation of the C# compiler, we need to install it via the installation “apt” instruction with the “mono-runtime” keyword as shown within the image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image3-14.png" data-lazy- height="72" src="data:image/svg xml,” width=”526″>

Within the installation, you have to press “y” to proceed with its installation and the runtime software platform for the .Net framework will be installed efficaciously.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image2-15.png" data-lazy- height="39" src="data:image/svg xml,” width=”623″>

After the configuration of the mono-runtime open-source platform, it’s high time to configure the C# compiler. For that, you have to utilize the keyword “mono-mcs” in your “apt” instruction for the installation of the mono-mcs compiler in the Ubuntu 20.04 system.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image5-13.png" data-lazy- height="92" src="data:image/svg xml,” width=”495″>

Again, you need to press “y” to configure it on your system as shown.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image4-13.png" data-lazy- height="40" src="data:image/svg xml,” width=”630″>

It’s time to create a C# file with a “cs” extension at its end utilizing Ubuntu’s “touch” instruction displayed in the below image. It will be quickly created in your current home directory as far as the ist “ls” command is showing. While using the “mcs” compiler to compile the “while.cs” file on your shell, it will be throwing an error that the file doesn’t contain any main() method. This is because we haven’t added any code to the file yet.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image7-11.png" data-lazy- height="143" src="data:image/svg xml,” width=”718″>

Example 01:

It’s time to create our new illusion to reveal the use of the “while” loop in C#. Before using that, we have to utilize the “System” library of C# to make use of basic classes and functions in our code. Use the keyword “using” along with the word “System” followed by the “;” sign. We have been creating a new user-defined class named “While” in our code file and initializing it with the main() function of C#.

The main() function must be of the static void return type. Within our main() method, we have been performing some repetitive tasks using a while loop. Before that, we have initialized a counter integer variable “c” to 0. The simple while loop starts with the condition “c<10”. It means that the loop will be executed until the given condition got false. While it’s true, the “WriteLine()” function of the “Console” class for the “System” package in C# will continue to display the variable “c” value on the screen. Along with that, the counter “c” value will be incremented by 1. After some iterations, when the value of “c” reaches 10, the loop will not be executed anymore. The while loop, main() function, and the class “While” are completed here.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image6-12.png" data-lazy- height="184" src="data:image/svg xml,” width=”473″>

After saving this code in the C# file, we have been compiling it with the “mcs” compiler of C#. On listing the contents of the home folder, we have got to know that the “exe” file for this C# code file has been created successfully for execution. So, we have used the run-time “mono” executor to run the “exe” file and got the shown-below output on our shell screen. The loop got executed until the variable “c” reached value 10 and displayed all the values on the shell.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image9-7.png" data-lazy- height="273" src="data:image/svg xml,” width=”698″>

Example 02:

The “while” loop can be utilized with the “do” statement in the C# code to place the execution part in it. The purpose of using “do” is to separate the condition part and the execution part in the code. So, we have been using the quite same code while making use of the “do” part for the “while” loop. In our code, the “do” part code will only get executed when the condition of a “while” part got satisfied. This means until the value of counter “c” doesn’t reach “6”, it will continue to execute the “do” part i.e., display the counter number and increment it. If you want to use some string along with the variable in a “WriteLine” function to be displayed, you have to use the double inverted commas and curly brackets to catch the variable value in a sequence. The sequence of variable numbers must be defined as 0,1,2 and so on.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image8-9.png" data-lazy- height="204" src="data:image/svg xml,” width=”548″>

After this code compilation with the ‘mcs’ C# compiler, the while.exe file got created. After running the “while.exe” file, our loop got executed and 6 values have been displayed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image12-4.png" data-lazy- height="196" src="data:image/svg xml,” width=”693″>

Example 03:

Let’s take a look at the use of the “true” condition in a “while” loop. Without stating anything in the “while” loop, we have only used the boolean “true” in the “while” part of a loop within simple brackets. The overall code was left unchanged.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image10-6.png" data-lazy- height="183" src="data:image/svg xml,” width=”546″>

After the compilation and running of this C# script, the while loop continues to display the iteration numbers on each iteration without breaking. You have to press Ctrl Z to stop it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image11-5.png" data-lazy- height="522" src="data:image/svg xml,” width=”710″>

The “if” statement can also be used to specify the condition and stop the “while” loop execution on reaching a certain point as shown in the below code. The break keyword is utilized to stop the execution purposely.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image13-4.png" data-lazy- height="223" src="data:image/svg xml,” width=”597″>

On execution of this code, only the first 6 iteration numbers got displayed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image14-3.png" data-lazy- height="197" src="data:image/svg xml,” width=”692″>

Example 04:

Within this example, we will be performing a random calculation to create a table of 13. Within the main() function, we have initialized two variables x, and y with values 1 and 13 respectively. The variable “z” has only been defined. The “while” loop is here to iterate itself until the value of variable “x” approaches 10. On each iteration, the value of variable “y” will be multiplied by the value of variable “x” and saved to variable “z”. The WriteLine() function is used to display the values in a table form. The “x” will be incremented at each iteration.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image15-1.png" data-lazy- height="217" src="data:image/svg xml,” width=”609″>

After compiling and running this code, we have got the table of 13 displayed on our console.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image16.png6253a60a62c4b.jpg" data-lazy- height="233" src="data:image/svg xml,” width=”375″>

Conclusion

The purpose of employing “while” loops in coding, specifically in C#, is explained in the first paragraph of this article. The first example is demonstrating the use of the “while” statement single-handedly while the second is showing the significance of using the “do” statement in a “while” loop. The 3rd example is showing how the “true” keyword in the “while” loop can affect the output and how the “if” statement can be utilized to minimize the effect. Along with all these examples, you can also make use of nested “while” loops in C# without overdoing yourself.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Author-Image-150×150.jpg6253a60a68e85.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.