Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 07 Beginner

Conditional Statements in C#

Updated: May 17, 2026
5 min read

# CHAPTER 7

Conditional Statements

1. Introduction

Programs need to make decisions. Should a user be granted access? Is the player's health zero? Conditional statements allow a C# program to choose different paths of execution based on specific conditions, making your software dynamic and intelligent.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use the if statement to execute code conditionally.
  • Use if-else for two-way branching.
  • Chain conditions using else if ladders.
  • Use the switch statement for multi-way branching.
  • Use modern C# 8.0 switch expressions (Pattern Matching).

3. The if and else Statements

The if statement evaluates a boolean condition. If true, it runs the block. The else statement provides an alternative block if the condition is false.
csharp
12345678910
int age = 20;

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

4. The else if Ladder

Used to check multiple conditions in sequence. As soon as one condition is true, its block executes, and the rest of the ladder is skipped.
csharp
12345678910111213141516171819
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: F");
}
// Output: Grade: C

5. The switch Statement

The switch statement is a cleaner alternative to a long else if ladder when you are comparing a single variable against exact, distinct values.
csharp
1234567891011121314151617
int day = 3;

switch (day) 
{
    case 1:
        Console.WriteLine("Monday");
        break; // break is REQUIRED in C# to exit the switch
    case 2:
        Console.WriteLine("Tuesday");
        break;
    case 3:
        Console.WriteLine("Wednesday");
        break;
    default: // Runs if no cases match
        Console.WriteLine("Weekend!");
        break;
}

Important Difference from C/C++: In C#, you cannot "fall through" from one case to another by forgetting the break keyword. The C# compiler will throw an error to protect you from this common bug!

6. Modern C# (Switch Expressions / Pattern Matching)

Introduced in C# 8.0, the switch expression is a highly condensed, modern way to write switch logic, assigning a value directly.
csharp
123456789101112
int day = 3;

// Modern C# Switch Expression
string dayName = day switch
{
    1 => "Monday",
    2 => "Tuesday",
    3 => "Wednesday",
    _ => "Weekend" // The underscore '_' represents the 'default' case
};

Console.WriteLine(dayName);

7. Mini Project: Grade Evaluation System

csharp
1234567891011121314151617181920212223242526272829303132333435
using System;

namespace GradeCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== GRADE EVALUATOR ===");
            Console.Write("Enter your score (0-100): ");
            int score = int.Parse(Console.ReadLine());

            if (score < 0 || score > 100) 
            {
                Console.WriteLine("Error: Invalid score entered.");
            } 
            else if (score >= 90) 
            {
                Console.WriteLine("Result: A (Excellent)");
            } 
            else if (score >= 80) 
            {
                Console.WriteLine("Result: B (Good)");
            } 
            else if (score >= 70) 
            {
                Console.WriteLine("Result: C (Pass)");
            } 
            else 
            {
                Console.WriteLine("Result: F (Fail)");
            }
        }
    }
}

8. Common Mistakes

  • Using = instead of ==: Writing if (x = 5) assigns 5 to x. The C# compiler is smart and will flag this as an error (unlike C++ which allows it and causes massive bugs).
  • Missing break in a traditional switch: C# strictly enforces that every case block (that contains code) must end with a break or return.

9. Best Practices

  • Keep your if statements simple. If an if statement has 4 && and || conditions, refactor it by extracting those conditions into well-named boolean variables.
  • Use the modern C# 8 switch expression when mapping a single variable to a single return value. It is much cleaner than a traditional switch.

10. Exercises

  1. 1. Write a program to check if a given year (input by user) is a leap year.
  1. 2. Build a simple calculator using a switch statement that asks the user for an operator (+, -, *, /) and two numbers.

11. MCQs with Answers

Question 1

What happens if an if condition evaluates to false?

Question 2

Which keyword is used as the "catch-all" fallback in a traditional switch statement?

Q3. Is fall-through (forgetting the break keyword) allowed in C# switch statements? a) Yes, it runs the next case automatically b) No, the C# compiler throws an error c) Yes, but only for strings Answer: b) No, the C# compiler throws an error
Question 4

In a modern C# 8 switch expression, what symbol represents the default case?

Q5. Can if statements be nested inside other if statements? a) Yes b) No Answer: a) Yes
Question 6

What is the output of if(true) Console.Write("A"); else Console.Write("B");?

Question 7

Which operator is used to check if two variables are equal in an if condition?

Question 8

What data types can be used in a C# switch statement?

Q9. Which is generally cleaner for checking 10 exact string values? a) 10 if-else statements b) A switch statement Answer: b) A switch statement
Question 10

What does the modern => symbol mean in a switch expression?

12. Interview Questions

  • Q: Explain the difference between a traditional switch statement and a modern C# switch expression.
  • Q: Why does C# prohibit implicit fall-through in switch cases? (Answer: To prevent accidental bugs caused by forgotten break statements, a notorious issue in C/C++).

13. Summary

Conditional statements give your program decision-making power. if, else if, and else provide flexible logic evaluation. switch statements offer a cleaner syntax for matching exact values. Modern C# introduces pattern matching switch expressions, drastically reducing boilerplate code for simple logic.

14. Next Chapter Recommendation

In Chapter 8: Loops in C#, we will learn how to repeat actions automatically using for, while, and do-while loops, removing the need to copy-paste code.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·