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#.
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.
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.
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.
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.
Again, you need to press “y” to configure it on your system as shown.
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.
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.
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.
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.
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.
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.
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.
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.
On execution of this code, only the first 6 iteration numbers got displayed.
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.
After compiling and running this code, we have got the table of 13 displayed on our console.
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.