Conditional Statements
Conditional statements let a program take different paths depending on runtime data. C#'s core tool for this is the if/else if/else chain, which evaluates boolean expressions in order and executes the block belonging to the first true condition. Alongside if, C# offers the ternary conditional operator (?:) for compact single-expression branching, and — increasingly in modern code — pattern-matching conditions (is, relational patterns, and switch expressions) that combine type checks and value tests in a single readable condition.
Cricket analogy: A captain's DRS decision tree checks 'was it out LBW? else check inside edge, else check bat-pad' in order, just as if/else-if evaluates conditions sequentially until the first true branch fires.
if / else if / else Chains
An if statement executes its block when the condition is true; an optional else runs when it's false. Chaining else if lets you test multiple mutually exclusive conditions in sequence — evaluation stops at the first branch whose condition is true, so branch order matters when conditions overlap. Braces { } around single-statement bodies are technically optional in C#, but omitting them is widely considered a readability and maintenance risk, since a later added statement can silently fall outside the intended block.
Cricket analogy: If a bowler is under the powerplay over limit, the field stays up; otherwise it drops back — but forgetting to bracket 'field back, rotate strike' as one block risks a fielder silently drifting out of the intended restriction.
The Ternary Operator and Pattern-Based Conditions
The ternary operator condition ? whenTrue : whenFalse compresses a simple if/else that produces a value into one expression, commonly used for inline assignment or interpolated strings. Modern C# also lets you fold type checks and conditions together using the is operator with patterns, e.g. if (shape is Circle { Radius: > 0 } c), which simultaneously checks the runtime type, destructures a property, and tests a condition — all in one readable line, avoiding the verbose cast-then-check pattern from older C#.
Cricket analogy: Instead of a full if/else, a scorer writes 'result = runsScored >= target ? "Won" : "Lost"' in one line — and modern scoring software can check 'if (delivery is Six { Runs: 6 } s)' to match type and value together.
int score = 82;
// Classic if/else if/else chain
string grade;
if (score >= 90) grade = "A";
else if (score >= 80) grade = "B";
else if (score >= 70) grade = "C";
else grade = "F";
Console.WriteLine(grade); // "B"
// Ternary operator for a simple two-way branch
string status = score >= 60 ? "Pass" : "Fail";
// Pattern-based condition combining a type check and a property test
object shape = new Circle(Radius: 4.5);
if (shape is Circle { Radius: > 0 } c)
{
Console.WriteLine($"Valid circle with radius {c.Radius}");
}
record Circle(double Radius);Unlike some languages, C# does not allow implicit truthy/falsy conversion of non-bool types in an if condition — you cannot write if (someInt); the condition must be a genuine bool expression (e.g. if (someInt != 0)). This eliminates a common class of bugs seen in C or JavaScript where 0, empty strings, or null are silently treated as false.
A classic pitfall is using = (assignment) instead of == (comparison) inside a condition. In C, if (x = 5) compiles and is a notorious bug source. C# prevents this for bool conditions because an int assignment expression's type (int) doesn't satisfy the required bool type — but the mistake can still occur, and compile, if both sides happen to be bool variables, so it's worth double-checking conditions during review.
- if/else if/else chains evaluate top to bottom and execute only the first branch whose condition is true.
- C# requires conditions to be genuine bool expressions — there is no implicit numeric-to-bool 'truthy' conversion.
- The ternary operator
?:compresses a value-producing if/else into a single expression. iswith patterns lets you combine a type check, property destructuring, and a condition in one expression.- Always use braces around if/else bodies to avoid maintenance bugs when adding statements later.
- Branch order matters whenever conditions can overlap, since only the first true branch executes.
Practice what you learned
1. In an else-if chain, which branch executes if multiple conditions would independently evaluate to true?
2. Which of the following is valid as an `if` condition in C#?
3. What does the expression `score >= 60 ? "Pass" : "Fail"` represent?
4. What does the pattern `shape is Circle { Radius: > 0 } c` do?
5. Why is omitting braces around a single-statement if body considered risky?
Was this page helpful?
You May Also Like
Operators in C#
A tour of C#'s arithmetic, comparison, logical, assignment, and null-conditional operators, including precedence rules and operator overloading basics.
Switch Statements and Expressions
Learn how C# branches on a value using the classic switch statement and the modern, expression-based switch introduced in C# 8, including pattern matching arms.
Pattern Matching
Survey C#'s pattern matching features — type patterns, property patterns, relational and logical patterns, and switch expressions — for expressive, safe branching.
Loops in C#
Covers C#'s four looping constructs — for, while, do-while, and foreach — along with break, continue, and guidance on choosing the right loop for the job.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics