Mastering Conditional Logic in C#: if, else, and switch

Conditional logic is the decision-making backbone of any C# application. Whether you’re building a console game, desktop app, or a web API, you’ll use if, else, and switch statements to control your program’s flow based on different conditions.

In this post, we’ll explore how these structures work, when to use them, and show clear examples to help you build smarter, more dynamic C# programs.


✅ The if Statement

The if statement is the simplest form of conditional logic. It allows your program to execute code only if a condition is true.

🔹 Syntax:

if (condition)
{
    // Code runs if condition is true
}

🔸 Example:

int age = 18;

if (age >= 18)
{
    Console.WriteLine("You are an adult.");
}

🔁 The if...else Statement

Use else to handle the false condition:

if (age >= 18)
{
    Console.WriteLine("Access granted.");
}
else
{
    Console.WriteLine("Access denied.");
}

🔗 The else if Ladder

Chain multiple conditions together using else if:

int score = 75;

if (score >= 90)
{
    Console.WriteLine("Grade: A");
}
else if (score >= 80)
{
    Console.WriteLine("Grade: B");
}
else if (score >= 70)
{
    Console.WriteLine("Grade: C");
}
else
{
    Console.WriteLine("Grade: D");
}

💡 Use else if when only one condition should be true.


🔁 Boolean Logic Inside if Conditions

C# allows you to combine conditions using logical operators:

bool isMember = true;
int age = 17;

if (isMember && age >= 18)
{
    Console.WriteLine("Discount applied.");
}
else
{
    Console.WriteLine("No discount.");
}

🔄 The switch Statement

switch is a cleaner alternative when checking one variable against many values.

🔹 Syntax:

switch (expression)
{
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // fallback code
        break;
}

🔸 Example:

string day = "Monday";

switch (day)
{
    case "Monday":
        Console.WriteLine("Start of the week!");
        break;
    case "Friday":
        Console.WriteLine("Weekend is near.");
        break;
    case "Saturday":
    case "Sunday":
        Console.WriteLine("It's the weekend!");
        break;
    default:
        Console.WriteLine("Just another day.");
        break;
}

💡 Use break; to prevent fall-through between cases.
💡 You can group multiple case labels together.


🧪 Practice Example

Console.Write("Enter a number between 1 and 3: ");
int input = int.Parse(Console.ReadLine());

switch (input)
{
    case 1:
        Console.WriteLine("You picked One.");
        break;
    case 2:
        Console.WriteLine("You picked Two.");
        break;
    case 3:
        Console.WriteLine("You picked Three.");
        break;
    default:
        Console.WriteLine("Invalid number.");
        break;
}

🤔 When Should I Use if vs switch?

Use if when…Use switch when…
Comparing complex expressionsComparing one value to fixed cases
Combining multiple conditionsMatching one variable to known constants
Working with ranges (>, <)Checking for specific values only

🧠 Tips for Writing Clean Conditionals

  • ✅ Use meaningful variable names
  • ✅ Keep conditions simple and readable
  • ✅ Avoid deeply nested if blocks
  • ✅ Consider using functions to encapsulate logic

🎯 Next Step: Practice Makes Perfect

Try writing a small program that:

  • Asks the user for a number
  • Uses if/else to describe whether it’s positive, negative, or zero
  • Then uses switch to print the number in words (1 → “One”, 2 → “Two”, etc.)

💬 Questions or Feedback?

If you’re stuck, or want feedback on your practice code, don’t hesitate to contact us. We’re here to help you master every line of C#!