C# C# Basics (Retired) Perform if / else

Victor Wooding
Victor Wooding
11,921 Points

Where should you place Console.WriteLine("Goodbye"); for this video?

I placed it in the code for the if statement:

if (entry == "quit") { keepGoing = false; Console.WriteLine("Goodbye"); } When I followed the video it kept saying goodbye after I entered the amount of minutes

Hendrik Heim
Hendrik Heim
1,007 Points

Hey Victor. Please provide your full code

4 Answers

Victor Wooding
Victor Wooding
11,921 Points

//full code

using System; class Program { static void Main() {

      int runningTotal = 0;
      bool keepGoing = true;

      while (keepGoing) {
           //Prompt the user for minutes exercised
        Console.Write("Enter how many minutes you exercised or type \"quit \" to exit:");

        //Store this in a variable...This collectys user input

        string entry = Console.ReadLine();

        if (entry == "quit") 
        {
          keepGoing = false;
          Console.WriteLine("Goodbye");
        }else {

        int minutes = int.Parse(entry);

          if(minutes <= 10){
            Console.WriteLine("Better then nothing, am I right?");

          }else if (minutes <= 30)
          {
            Console.WriteLine("Way to go hot stuff!");

          }else if (minutes <= 60)
          {
            Console.WriteLine("You must be a ninja!");

          }else 

          {
           Console.WriteLine("Okay, now you are just showing off!"); 

          }

        runningTotal = runningTotal + minutes;

          //Add minutes exercised to total
          //Display the total number of minutes exercised to the screen

        Console.WriteLine("You've entered " + runningTotal + " minutes");
          //Repeat until the user quits
        }

      }   


    }
}
Hendrik Heim
Hendrik Heim
1,007 Points

Your code seems correct to me. Did you try running your code in an IDE for example Visual Studio?

Alternatively, try this:

While(entry != "quit") { // handle input }
//finishing message after loop
Console.WriteLine("Goodbye");

Note you have to declare and initialize entry before entering the loop.

Victor Wooding
Victor Wooding
11,921 Points

The code runs but when I ran the code following the Video the "Goodbye" text came after I entered the exercise value. So i would type 35 and it would say Goodbye and then I would type 25 and it would say goodbye. All I was trying to say is that there seems to be an error in the code from the video.

Daniel Atonge
Daniel Atonge
4,769 Points

The code is ok with no problems. I ran both on Visual Studio. I think the confusion comes in with the ending curly brackets. As Hendrik earlier said: While(entry != "quit") { if () { } else { //Take Note } // handle input }//ending curly bracket for the While-loop //finishing message after loop Console.WriteLine("Goodbye"); Thanks