How to Use While Loop in C# for Beginners
Tags: csharp, dotnet, tutorial, beginners
Published: 2023-11-23 04:20:34

{% youtube https://www.youtube.com/watch?v=8HruJlQGziY %}
Watch the [full video](https://www.youtube.com/watch?v=8HruJlQGziY) on YouTube.

While loop repeats the code while its condition is true. And it consists of... 

A **while**, **parentheses** and a **condition**. And **curly brackets**. With codes that will run on a loop ‘til the condition is false.

```csharp
while (condition)
{
    // Codes that will run on a loop.
}
```

With this example:

```csharp
// While loop repeats the code while its condition is TRUE.
double tuitionFee = 5000;

while (tuitionFee > 0)
{
    Console.WriteLine($"Tuition Fee:\n{tuitionFee}");
    Console.WriteLine("Please enter your new payment:");
    double payment = double.Parse(Console.ReadLine());

    // Subtract the payment from the tuition fee.
    tuitionFee -= payment;
}

Console.WriteLine("Your tuition fee is now fully paid.");
```

While the tuition fee is greater than zero, it will continue to ask for your payment. And when fully paid, it will exit the while loop and displays:

![Your tuition fee is now fully paid.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1bnws88sz0icedj5lu2.png)

## 💭 So, how does this work exactly?

1. First, we initialize the tuitionFee to have a value of 5000.

2. Next, we ask if our tuitionFee is greater than zero. If it is, then we enter the loop.

3. Inside the loop, we get the payment from the user, and subtract it to the tuitionFee.

4. And this condition and codes inside these curly brackets would repeat again and again, until this tuitionFee reaches zero or negative.

## ⏫ Add Keywords to Jump the Loop

You can also use keywords to make this loop more interesting.

You can use the **continue** keyword to skip the rest of the codes. With this condition:

```csharp
// If payment is zero or negative.
if (payment <= 0)
{
    // Skip the rest of the codes.
    continue;
}
```

First, we ask if the payment is less than or equals to 0, and if it is, we will skip the rest of the codes, below, to avoid getting zero or negative payments from the user.

```csharp
// While loop repeats the code while its condition is TRUE.
double tuitionFee = 5000;

while (tuitionFee > 0)
{
    Console.WriteLine($"Tuition Fee:\n{tuitionFee}");
    Console.WriteLine("Please enter your new payment:");
    double payment = double.Parse(Console.ReadLine());

    // If payment is zero or negative.
    if (payment <= 0)
    {
        // Skip the rest of the codes.
        continue;
    }

    // Subtract the payment from the tuition fee.
    tuitionFee -= payment;
}

Console.WriteLine("Your tuition fee is now fully paid.");
```

Another thing is the **break** keyword to stop the loop immediately. 

```csharp
// If payment is greater than the tuition fee.
if (payment > tuitionFee)
{
    break;
}
```

We can ask if the user’s payment is greater than the tuition fee. Then if it’s true, we will stop and exit the loop.

```csharp
// While loop repeats the code while its condition is TRUE.
double tuitionFee = 5000;

while (tuitionFee > 0)
{
    Console.WriteLine($"Tuition Fee:\n{tuitionFee}");
    Console.WriteLine("Please enter your new payment:");
    double payment = double.Parse(Console.ReadLine());

    // If payment is zero or negative.
    if (payment <= 0)
    {
        // Skip the rest of the codes.
        continue;
    }

    // If payment is greater than the tuition fee.
    if (payment > tuitionFee)
    {
        break;
    }

    // Subtract the payment from the tuition fee.
    tuitionFee -= payment;
}

Console.WriteLine("Your tuition fee is now fully paid.");
```

But displaying nothing is kind of awkward, so, we can display the user’s change instead:

```csharp
// If payment is greater than the tuition fee.
if (payment > tuitionFee)
{
    // Get the change.
    double change = payment - tuitionFee;
    Console.WriteLine($"Your change is:\n{change}");
    tuitionFee = 0;
    break;
}
```

Here's the final code you can [view on GitHub Gist](https://gist.github.com/emmanuel-pangan/36e8a7859273872c507232c1f7495a18).
{% embed https://gist.github.com/emmanuel-pangan/36e8a7859273872c507232c1f7495a18.js %}

Thanks for reading! 📖

Check out my [YouTube channel](https://www.youtube.com/@Emmanuel-Pangan) for more coding tutorials. Happy coding everyone! 🧡

Source(s):
[Microsoft's documentation on While Loops](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-while-statement)
[Microsoft's documentation on Continue keyword](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/jump-statements#the-continue-statement)
[Microsoft's documentation on Break keyword](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/jump-statements#the-break-statement)