If & Switch Statements (Exercise) – Learn C#

2  comments

This is an exercise for this YouTube video.

Happy learning and good luck!

[mlw_quizmaster quiz=2]

 

Assignment: Write code which will determine the sign of  a number.

 

 

Click to see the answer
int myNumber = int.Parse(Console.ReadLine());
if (myNumber > 0)
    Console.WriteLine("+");
else if (myNumber < 0)
    Console.WriteLine("-");
else
    Console.WriteLine("zero");

 

 

 

 

Assignment: Make a simple chatbot using switch statement.

 

 

Click to see the answer
string myString = Console.ReadLine();
switch (myString)
{
    case "Hello":
        Console.WriteLine("Hello there!");
        break;
    case "I'm learning C#":
        Console.WriteLine("That's cool!");
        break;
    default:
        Console.WriteLine("I'm a dumb chatbot. Please write something else.");
        break;
}

 

 

 

 

About the author 

Matt Rešetár

Matt is an app developer with a knack for teaching others. Working as a freelancer and most importantly developer educator, he is set on helping other people succeed in their Flutter app development career.

You may also like

  • Hi am a fan of your work.I’ll probably be visiting this website like everyday.
    Please could you make a tutorial about arrays.
    And I will also recommend putting a few ads to help your business.

    • Hello! I’m happy that you enjoy my videos!
      Tutorial about arrays is coming up next in the Learn C# course.
      Thanks for the recommendation, but I’m not planning to use this website as a income stream (at least not yet), so I think the “huge” amounts of YouTube money I get is enough 😀

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >