The C# While Loop

c# while loopC# programming is an object oriented based language. Commonly used for various applications, such as design and video game creation, there are several aspects of C# programming that helps people with bringing their virtual dreams come to life.

Being a computer programming language, C# programming isn’t an easy thing to pick up on. Just like any other language, it requires a bit of time to become fluent. Don’t let that scare you though, as there are tons of tutorials and classes you can use to teach you about the language. In fact, the Udemy course C# Introduction teaches you about the core mechanics of C# and what it can do.

Contrary to popular belief you don’t have to have any kind of experience to start programming. You can do it right now. The Udemy course Programming for Complete Beginners in C# is perfect for you if you’ve only had an interest in programming and have always wanted to give it a try and see what it’s all about.

Below we will explore a mechanic in C# called a while loop. Loops are an essential aspect of C# and it gives you the ability to create instances where the program essentially runs on its own.

What is a Loop?

A loop in programming is a method in which the program continues to do something for a specific number of times or over the course of a duration of time. For example, you can have a program where you make a loop that adds 1 to a number over and over again 10 times. This is a simple loop, but it does it automatically and you don’t have to manually write the code.

In a more video game like context, consider a situation where a character is poisoned. You would create a loop that says while the character is poisoned their health will drop at increments of 3 until that health reaches 0. Once the health reaches 0 the loop will cease to work.

Notice the keyword used in that previous example. The keyword was while, which means that the loop continued to do the same thing over and over while a certain condition was met. 

Defining a While Loop

A while loop statement is one that repeatedly executes a statement as long as a condition is true. Just like in the example before, the while loop continued to reduce the character’s health while the character was poisoned. In this situation, the executed statement was the reduction health and the true condition was being poisoned.

The idea surrounded by while loops are simple and C# while loops have several implications to make programming and design easy. The examples mentioned above are used in a gaming context, but these loops can be used for computational purposes as well to make the process easier on the programmer.

Basic Setup of a While Loop

This is the basic skeleton of a while loop.

While (condition)
{
Statements;
}

Looks simple enough as you are just setting the condition and putting in statements for that condition during the period of time that it is true.

Let’s look at an example of a while loop and see how it works out.

int x = 0;
While (x < 10)
{
Console.WriteLine(“Hello,  my name is Joe. I am “x “years old”);
X++;
} 

This is a fairly simple while loop. What happens is that the loop continues 11 times and writes a statement. It then increments the X plus 1 and it checks to see if the condition is true again before the loop decides whether or not to repeat the same pattern. Let’s look at the output of the loop and examine what has happened.

While Loop Output 

If we were to look at the Output of the while loop right now, the following statements will be printed out by the program.

Hello, my name is Joe. I am 0 years old

Hello, my name is Joe. I am 1 years old.

Hello, my name is Joe. I am 2 years old.

Hello, my name is Joe. I am 3 years old.

Hello, my name is Joe. I am 4 years old.

These lines of statements will continue until Joe says that he is 9 years old. After that the loop will stop completely and end. Breaking down the while loop we can examine why this happens and how it happens.

At the beginning of the loop, we set it up with the following instantiation of a variable.

int x = 0;

This creates our variable x, which is an integer. It then sets a value to x, which is in this case 0.

We then put in the following line of code.

While (x < 10)

This is the start of our while loop. In C# programming all you have to do for your while loop is type in the word while and place your condition in parentheses. After a few examples, you will get the hang of it.

The next statements are the execution of the loop and it is what the loop does when the condition is true.

Console.WriteLine(“Hello,  my name is Joe. I am “x “years old”);

The first of these statements is what is written when the loop is running. Note the x in the sentence. That’s the same x in the variable that we initiated before, which is why you will see the number change as the program continues to write out the statement and follow the loop.

The next part of the program is the most important because it limits the number of times that the loop runs.

X++;

The above statement increments x by 1, but you can also write that statement as x += 1; It doesn’t matter as long as you set the condition.

With this statement placed inside the loop, the program automatically increases the value of x by 1 each time the loop is ran. So the loop starts out at 0, but the first time it writes its sentence, the loop changes x to 1 and then it sees if the condition is true.

Since 1 < 10 the condition is still true and the while loop continues to write. It will do this over and over again, but as you may have noticed once Joe says that he is 9 years old the loop stops.

After Joe says that he is 9 years old, the loop increases x by 1 and then it checks to see if the condition true, but that is no longer the case.

Observe the fact that the value of x after this instance is 10. The condition we set is that as long as x is less than 10 the loop will continue. Seeing as that is no longer the case, the loop will cease since the condition is no longer true.

Understanding Programming and Loops 

This may all seem like an information overload to you, but don’t worry about it. Just take it one step at a time and breakdown the loop until you understand it. The best way to get programming is to try it yourself. So create a few more loops and see if you can get them to work. If you want a more in-depth analysis of programming then try the two courses Learn C# 2010 Part I and Learn C# 2010 Part II.