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
ifstatement to execute code conditionally.
-
Use
if-elsefor two-way branching.
-
Chain conditions using
else ifladders.
-
Use the
switchstatement for multi-way branching.
-
Use modern C# 8.0
switchexpressions (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
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
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
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, theswitch expression is a highly condensed, modern way to write switch logic, assigning a value directly.
csharp
7. Mini Project: Grade Evaluation System
csharp
8. Common Mistakes
-
Using
=instead of==: Writingif (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
breakin a traditional switch: C# strictly enforces that every case block (that contains code) must end with abreakorreturn.
9. Best Practices
-
Keep your
ifstatements simple. If anifstatement has 4&&and||conditions, refactor it by extracting those conditions into well-named boolean variables.
-
Use the modern C# 8
switchexpression when mapping a single variable to a single return value. It is much cleaner than a traditionalswitch.
10. Exercises
- 1. Write a program to check if a given year (input by user) is a leap year.
-
2.
Build a simple calculator using a
switchstatement 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?
Question 4
In a modern C# 8 switch expression, what symbol represents the default case?
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?
Question 10
What does the modern => symbol mean in a switch expression?
12. Interview Questions
-
Q: Explain the difference between a traditional
switchstatement and a modern C#switchexpression.
- 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 usingfor, while, and do-while loops, removing the need to copy-paste code.