Conditional
You've learn conditional if-else
statement before.
As stated before, we need a condition where keepGoing
variable assigned false, so it will quit the loop.
In C#, if-else
statement stated below.
if (condition) {
}
else {
}
In FitnessFrog, we want to check the input from user repeatedly. When user inputs minutes, it keeps in loop function, but it will break the loop if user inputs "quit". So the condition will be entry == "quit"
. Put it in code that shown below.
if (entry == "quit") {
}
entry == "quit"
is the condition that will execute the code inside the if.
The complete code will be like this.
using System;
namespace ConsoleApp1
{
class Program
{
static void Main()
{
string entry;
bool keepGoing = true;
int totalWorkingTime = 0;
while (keepGoing){
// Prompt the for minutes exercised
Console.WriteLine("Enter how many minutes you exercised: ");
entry = Console.ReadLine();
if (entry == "quit")
{
keepGoing = false;
}
else
{
int minutes = int.Parse(entry);
// Add minutes exercised to a running total
totalWorkingTime = totalWorkingTime + minutes;
// Display the running total in minutes
Console.Write("Your total working minutes is " + totalWorkingTime + " minutes");
// Display the running total in minutes
Console.WriteLine("You entered " + minutes + " minutes");
}
}
}
}
}
Now, make the console writes "Goodbye!" when user's input is "quit", then break the loop. Where should we put that code?