C# If...Else, Switch

Last modified: July 11, 2021
If...Else
  • Less than: obj1 < obj2
  • Greater than: obj1 > obj2
  • Less than or equal to: obj1 <= obj2
  • Greater than or equal to: obj1 >= obj2
  • Equal to obj1 == obj2
  • Not Equal to: obj1 != obj2
static void Main(string[] args) { var score = 88; if (score >= 90) { Console.WriteLine("Excellent"); } else if (score >= 80) { Console.WriteLine("Good"); } else if (score >= 70) { Console.WriteLine("Pass"); } else { Console.WriteLine("Try next time"); } }
Switch
class Program { static void Main(string[] args) { var numberCase = 2; switch(numberCase) { case 1: Console.WriteLine("One!"); break; case 2: Console.WriteLine("Two!"); break; case 3: Console.WriteLine("Two!"); break; default: Console.WriteLine("Not match!"); break; } } }