100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C#

Conditional Statements

Explains how C# branches program flow using if/else, the ternary conditional operator, and pattern-based conditions, with guidance on readability and pitfalls.

Control FlowBeginner7 min readJul 9, 2026
Analogies

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.

csharp
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.
  • is with 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#ConditionalStatements#Conditional#Statements#Else#Chains#StudyNotes#SkillVeris#ExamPrep