Menu
Coddy logo textTech

If vs. Switch Comparison

Part of the Logic & Flow section of Coddy's C# journey — lesson 10 of 66.

C# provides two primary ways to handle multiple conditions: if-else statements and switch statements. Each has its strengths in different scenarios.

If-else statements are flexible and can handle any type of condition:

string grade = "B";

if (grade == "A")
{
    Console.WriteLine("Excellent");
}
else if (grade == "B")
{
    Console.WriteLine("Good");
}
else if (grade == "C")
{
    Console.WriteLine("Satisfactory");
}
else
{
    Console.WriteLine("Needs improvement");
}

Switch statements are cleaner when comparing a single variable against multiple constant values:

string grade = "B";

switch (grade)
{
    case "A":
        Console.WriteLine("Excellent");
        break;
    case "B":
        Console.WriteLine("Good");
        break;
    case "C":
        Console.WriteLine("Satisfactory");
        break;
    default:
        Console.WriteLine("Needs improvement");
        break;
}

Switch can group multiple cases that share the same outcome by stacking case labels together — this is called fall-through. When several cases have no code between them, they all execute the same block:

char dayCode = 'S';

switch (dayCode)
{
    case 'M':
    case 'T':
    case 'W':
    case 'R':
    case 'F':
        Console.WriteLine("Weekday");
        break;
    case 'S':
    case 'U':
        Console.WriteLine("Weekend");
        break;
    default:
        Console.WriteLine("Unknown day");
        break;
}

Here, 'S' and 'U' both fall through to the same Console.WriteLine("Weekend") call. Stacking empty case labels like this is the standard way to group cases in C#.

challenge icon

Challenge

Medium

Create a method called convertGradeToPoints that:

  1. Takes a letter grade as a string parameter
  2. Converts it to a numeric grade point using both switch and if-else approaches
  3. The method should implement both approaches and return the result of the switch implementation
  4. Use these grade conversions:
    • "A" or "a" = 4
    • "B" or "b" = 3
    • "C" or "c" = 2
    • "D" or "d" = 1
    • "F" or "f" = 0
    • Any other input = -1

Cheat sheet

C# provides two primary ways to handle multiple conditions: if-else statements and switch statements.

If-else statements are flexible and can handle any type of condition:

string grade = "B";

if (grade == "A")
{
    Console.WriteLine("Excellent");
}
else if (grade == "B")
{
    Console.WriteLine("Good");
}
else if (grade == "C")
{
    Console.WriteLine("Satisfactory");
}
else
{
    Console.WriteLine("Needs improvement");
}

Switch statements are cleaner when comparing a single variable against multiple constant values:

string grade = "B";

switch (grade)
{
    case "A":
        Console.WriteLine("Excellent");
        break;
    case "B":
        Console.WriteLine("Good");
        break;
    case "C":
        Console.WriteLine("Satisfactory");
        break;
    default:
        Console.WriteLine("Needs improvement");
        break;
}

Switch statements can group multiple cases (fall-through) to share the same outcome by stacking case labels:

char dayCode = 'S';

switch (dayCode)
{
    case 'M':
    case 'T':
    case 'W':
    case 'F':
        Console.WriteLine("Weekday");
        break;
    case 'S':
    case 'U':
        Console.WriteLine("Weekend");
        break;
    default:
        Console.WriteLine("Unknown day");
        break;
}

Try it yourself

public class ConvertGradeToPoints
{
    // Implement the convertGradeToPoints method
    public static int convertGradeToPoints(string letterGrade)
    {
        // Write your code here
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow