DEV Community

Cover image for How to Use While Loop in C# for Beginners
Emmanuel Pangan
Emmanuel Pangan

Posted on • Updated on

How to Use While Loop in C# for Beginners


Watch the full video 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.

while (condition)
{
    // Codes that will run on a loop.
}
Enter fullscreen mode Exit fullscreen mode

With this example:

// 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.");
Enter fullscreen mode Exit fullscreen mode

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.

💭 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:

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

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.

// 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.");
Enter fullscreen mode Exit fullscreen mode

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

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

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.

// 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.");
Enter fullscreen mode Exit fullscreen mode

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

// 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;
}
Enter fullscreen mode Exit fullscreen mode

Here's the final code you can view on GitHub Gist.

Thanks for reading! 📖

Check out my YouTube channel for more coding tutorials. Happy coding everyone! 🧡

Source(s):
Microsoft's documentation on While Loops
Microsoft's documentation on Continue keyword
Microsoft's documentation on Break keyword

Top comments (0)