Skip to content

Instantly share code, notes, and snippets.

@emmanuel-pangan
Created October 25, 2023 02:59
Show Gist options
  • Save emmanuel-pangan/36e8a7859273872c507232c1f7495a18 to your computer and use it in GitHub Desktop.
Save emmanuel-pangan/36e8a7859273872c507232c1f7495a18 to your computer and use it in GitHub Desktop.
How to Use While Loop - How to C# for Beginners (Tutorial 06)
// 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)
{
// Get the change.
double change = payment - tuitionFee;
Console.WriteLine($"Your change is:\n{change}");
tuitionFee = 0;
break;
}
// Subtract the payment from the tuition fee.
tuitionFee -= payment;
}
Console.WriteLine("Your tuition fee is now fully paid.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment