FAQ: Conditional Statements - Review

This community-built FAQ covers the “Review” exercise from the lesson “Conditional Statements”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello, fellow learners!
I can’t get it why the system keeps telling me that I cannot convert a string to the double and cannot consequently cannot compare string and double. Please help!

double guess = 5;
      Console.WriteLine("Type any number from 1 to 10");
      string userGuess = Console.ReadLine();
     userGuess = Convert.ToDouble(userGuess);
      if (guess == userGuess)
      {
        Console.WriteLine("Correct");
      }
      else if (guess >= userGuess)
      {
        Console.WriteLine("Try higher");
      }
      else 
      {
        Console.WriteLine("Way too high");
      }

userGuess is of type string, so you cannot assign a double to userGuess. For string and double comparison make them both a double first (which is what you are trying to do) and then compare.

1 Like

Here is my code for the first suggestion of this challenge. Would there be a better way to write this?

using System;

namespace Review
{
  class Program
  {
    static void Main(string[] args)
    {
      /* use this space to write your own short program! 
      Here's what you learned:

      CONDITIONALS: if, if...else, else if
      SWITCH STATEMENT: switch (condition)
      TERNARY OPERATOR: (condition) ? true : false

      Good luck! */
      int realNumber = 6;
      
      Console.WriteLine("Pick a number between 1 and 10");
      int userInput = int.Parse(Console.ReadLine());
      
      if (userInput == realNumber)
      {
        Console.WriteLine("Correct!");
        
      }
      else if (userInput < realNumber)
      {
        Console.WriteLine("Too low");
      
      }
      else if (userInput > realNumber)
      {
        Console.WriteLine("Too high");
        
      }
      
    }
  }
}

1 Like

Hi, your code works great. I tried the option with the ternary operator just for fun and this is what I came up with:

using System;

namespace Review
{
  class Program
  {
    static void Main(string[] args)
    {
      int number = 6;
      Console.WriteLine ("Guess the number");
      int userGuess = int.Parse(Console.ReadLine());
      
     string message = (userGuess == number) ? "Correct" : (userGuess<number) ? "too low" : "too high";
      
      Console.WriteLine (message);

    }
  }
}

3 Likes

using System;

namespace SwitchStatement

{

class Program

{

static void Main(string[] args)

{

  Console.WriteLine("Choose a number from 10: ");



int answer = Console.ReadLine()

switch (answer)

{

case "1": 

 Console.WriteLine ("Way to low");

break;

case "2": 

 Console.WriteLine ("to low");

break;

case "3": 

 Console.WriteLine ("to low");

break; 

case "4": 

 Console.WriteLine ("low but almost there"); 

break; 

case "5": 

 Console.WriteLine ("Correct, nice job");

break; 

case "6": 

Console.WriteLine ("high but almost there"); 

break; 

case "7": 

 Console.WriteLine ("to high"); 

break;

case "8":

 Console.WriteLine ("to high");

break;

case "9":

 Console.WriteLine ("to high");

break;

case "10":

 Console.WriteLine ("Way to high"):

break;



default

 Console.WriteLine ("Invalid number");

break;

}

}

}

}

WHY ISN’T THIS CODE WORKING CAN SOMEONE PLEASE HELP ME

This is my birthday checker. I have no idea how to do an ASCII celebratory banner so I didn’t.

            DateTime today = DateTime.Today;

            Console.WriteLine("Input your birth year: ");
            int year = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Input your birth month: ");
            int month = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Input your birth day: ");
            int day = Convert.ToInt32(Console.ReadLine());

            DateTime userBday = new DateTime(year, month, day);


            if (userBday.Month == today.Month && userBday.Day == today.Day)
            {
                Console.WriteLine("Congratulations, it's your birthday!");
            }
            else {

                //We'll add your age to your birthday year, so that we establish your birthday date on the current year.
                DateTime next = userBday.AddYears(today.Year - userBday.Year);

                // checks if the current year's birthday is less (already passed) than the current date. If so, we add another year to create the next year's birthday date.
                if (next < today) 
                    next = next.AddYears(1);

                int numDays = (next - today).Days;
                Console.WriteLine("Your birthday is still "+ numDays + " days away.");
            }

This is my number guesser. I wanted to create one where the chosen number is always random and not fixed.

            Console.WriteLine("Guess a number between 1 to 10: ");

            Random rnd = new Random();
            int chosenNumber = rnd.Next(1, 11);

            int answer = Convert.ToInt32(Console.ReadLine());

            string result = (chosenNumber == answer) ? "correct" : (answer < chosenNumber) ? "too low" : 
            "too high";

            Console.WriteLine("Your guess was " + result + "! The number was " + chosenNumber);

Hallo everyone!
I have a problem for the review exercise, I can’t figure out why the system keeps telling me “syntax error” with my code. Can anybody tell me where I did wrong with my code?

 using System;
namespace Review
{
class Program
{
static void Main(string[] args)
{
 Console.WriteLine("please type a number between 1-10");
 int standard = 6;
 int a= int.Parse(Console.ReadLine());
 switch(a)
 {
   case a < 6:
      Console.WriteLine("too low");
      break;
  case a = 6:
      Console.WriteLine("correct");
      break;
  case a > 6:
      Console.WriteLine("too high");
      break;
  default:
      Console.WriteLine("please try again");
      break;
    }
  }
}
}