Chain conditions with else if. They are tested top to bottom and the first match wins.
Program.cs
using System;
class Program
{
static void Main()
{
int score = 85;
if (score >= 90) Console.WriteLine("A");
else if (score >= 80) Console.WriteLine("B");
else if (score >= 70) Console.WriteLine("C");
else Console.WriteLine("F");
}
}
Output
B
Order matters
If score >= 70 came first, an 85 would match it and print C. Always go from the most demanding condition to the most general.
When you are matching one value against a list of fixed options, switch reads better:
Program.cs
using System;
class Program
{
static void Main()
{
string day = "Sat";
switch (day)
{
case "Sat":
case "Sun":
Console.WriteLine("Weekend");
break;
default:
Console.WriteLine("Weekday");
break;
}
}
}
Output
Weekend