Codectionary / Developer documentation / C#

if / else if / else

The if statement executes a block of code only when its condition evaluates to true. C# lets you chain additional conditions with else if, and provide a fallback with else for when none of the preceding conditions match. The condition must be a genuine bool expression - unlike some C-family languages, C# does not allow a number to be used directly as a truthy/falsy condition.

Read the syntax

if requires a bool condition in parentheses. C# runs the first matching branch and skips the others; braces delimit each branch’s statements.

else if tests another condition only if earlier ones were false. The conditional operator condition ? valueWhenTrue : valueWhenFalse selects a value for an expression rather than opening a statement block.

Syntax

if (condition) {\n  // code\n} else if (condition2) {\n  // code\n} else {\n  // code\n}

Examples

Basic if / else

Choosing between two branches based on a condition.

int age = 20;

if (age >= 18)
{
    Console.WriteLine("You are an adult");
}
else
{
    Console.WriteLine("You are a minor");
}
  1. Choosing between two branches based on a condition.
  2. if requires a bool condition in parentheses. C# runs the first matching branch and skips the others; braces delimit each branch’s statements.
  3. age >= 18 is true for 20, so the first branch prints You are an adult. The else branch is skipped; change age to 17 to see the other path.

Chaining with else if

Testing multiple conditions in sequence.

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");
}
  1. Testing multiple conditions in sequence.
  2. if requires a bool condition in parentheses. C# runs the first matching branch and skips the others; braces delimit each branch’s statements.
  3. 75 fails the 90 and 80 checks but passes score >= 70, so Grade: C is printed. Ordering the thresholds from highest to lowest prevents a broad early match.

The Ternary Conditional Operator

A compact way to choose between two values based on a condition, in a single expression.

int age = 20;
string status = age >= 18 ? "adult" : "minor";
Console.WriteLine(status);  // adult

// Equivalent to:
// if (age >= 18) { status = "adult"; } else { status = "minor"; }
  1. A compact way to choose between two values based on a condition, in a single expression.
  2. if requires a bool condition in parentheses. C# runs the first matching branch and skips the others; braces delimit each branch’s statements.
  3. The condition is true, so the ? expression selects adult and stores it in status. This compact form is useful when selecting one of two simple values.

Best practices

  • Always use curly braces {} even for single-statement blocks - it prevents subtle bugs when code is later added to a branch
  • Order else if conditions from most specific to least specific when ranges overlap, since C# checks them top to bottom
  • Use the ternary operator (condition ? a : b) for simple value choices, but switch to a full if/else once the logic is more involved
  • Consider a switch expression instead of a long else if chain when comparing a single variable against many discrete values

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading