Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# else if Statement: Syntax, Behavior, and Usage

Learn how the C# else if construct works: syntax, evaluation order, common mistakes, and when to prefer switch or pattern matching.

~4 min readC#
#C##conditional logic#if else#switch
Diagram showing an if else if chain evaluating conditions in sequence in C#

The C# else if construct is a common way to evaluate multiple conditions in sequence. It is not a separate statement; it is the else keyword followed by another if statement, allowing you to chain additional conditions after an initial if. This article covers the syntax, evaluation order, common mistakes, and how to decide between else if, switch, and pattern matching.

How the else if Chain Evaluates

When you write an if statement followed by else if, the runtime evaluates each condition in the order they appear. As soon as one condition evaluates to true, the corresponding block executes and the rest of the chain is skipped. This short-circuit behavior is important because it means later conditions are not evaluated if an earlier one already matched.

csharp
int score = 75; if (score >= 90) { Console.WriteLine("A"); } else if (score >= 80) { Console.WriteLine("B"); } else if (score >= 70) { Console.WriteLine("C"); } else { Console.WriteLine("F"); }

In this example, score is 75. The first condition score >= 90 is false, so the second is checked. score >= 80 is also false, then score >= 70 is true, so "C" is printed. The final else block is not executed because a previous condition matched.

Writing a Simple else if Chain

The syntax is straightforward: an if statement, any number of else if clauses, and an optional final else block. Each condition must be a boolean expression. Braces are optional for single statements, but using them consistently avoids subtle bugs.

csharp
string role = "admin"; if (role == "admin") { GrantAdminAccess(); } else if (role == "editor") { GrantEditAccess(); } else if (role == "viewer") { GrantReadAccess(); } else { DenyAccess(); }

The else block is the default when no condition matches. It is not required, but without it the chain simply ends and execution continues after the entire statement.

Common Mistakes with else if

One frequent mistake is using else if when a plain if would be correct. If you need to evaluate multiple independent conditions, separate if statements are appropriate. else if is for mutually exclusive branches.

Another issue is forgetting the else keyword and writing two separate if blocks, which allows both to execute if both conditions are true. For example:

csharp
int x = 5; if (x > 0) { Console.WriteLine("Positive"); } if (x > 3) { Console.WriteLine("Greater than 3"); }

Here both messages print because the two if statements are independent. Changing the second if to else if would only print "Positive".

Condition order also matters. If you put a broad condition before a specific one, the specific branch may never be reached. For instance:

csharp
if (age > 0) { // Handles all positive ages } else if (age > 18) { // This branch is unreachable: every age that satisfies age > 18 also satisfies age > 0 }

else if vs switch: Choosing the Right Construct

else if is not always the best tool. When you are comparing a single value against many constant cases, a switch statement is often clearer and can be more efficient for large numbers of cases. The compiler can optimize switch into a jump table or a lookup, whereas else if always evaluates conditions sequentially.

Criterionelse ifswitch
Condition typeAny boolean expression, including is patternsConstant patterns, type patterns, relational patterns, or property patterns
ReadabilityGood for complex conditionsBetter for many discrete values
PerformanceSequential evaluationPotential jump table optimization
Pattern matchingPossible with is patterns, but less concise for value mappingDirect syntax for type, property, and relational patterns

Modern C# also supports switch expressions, which are more concise for returning a value.

csharp
int score = 75; string grade = score switch { >= 90 => "A", >= 80 => "B", >= 70 => "C", _ => "F" };

This is often easier to read than a long else if chain when the logic is a simple mapping. The relational patterns in this example require C# 9 or later.

Performance and Maintainability Considerations

The runtime cost of an else if chain is proportional to the number of conditions evaluated before a match. In the worst case, all conditions are checked. For a small number of branches this is negligible, but if you have dozens of conditions, a switch or a dictionary-based lookup may be more appropriate.

Maintainability is another factor. Long else if chains are harder to read and modify than a switch or a lookup table. If you find yourself adding many branches, consider extracting the logic into a separate method or using a data-driven approach.

Refactoring Long else if Chains

When an else if chain grows beyond a few branches, refactoring improves clarity. One option is to use a dictionary to map inputs to actions or values.

csharp
var handlers = new Dictionary<string, Action> { ["admin"] = GrantAdminAccess, ["editor"] = GrantEditAccess, ["viewer"] = GrantReadAccess }; if (handlers.TryGetValue(role, out var handler)) { handler(); } else { DenyAccess(); }

This approach separates the condition logic from the execution and makes it easy to add new cases. It also avoids sequential evaluation entirely.

Another modern alternative is to keep the else if chain but use property patterns in the conditions:

csharp
if (shape is Circle { Radius: > 10 } c) { // Handle large circle } else if (shape is Rectangle { Width: > 10, Height: > 10 } r) { // Handle large rectangle }

Like the switch expression example, property patterns with relational conditions require C# 9 or later.

Edge Cases and Runtime Behavior

else if conditions are evaluated from top to bottom. If a condition throws an exception, the rest of the chain is not evaluated, and the exception propagates. This is expected but worth remembering when conditions have side effects.

The else block is optional. If no condition matches and there is no else, execution simply continues after the entire statement. This is useful when you only want to handle specific cases.

Each block in an if/else if chain establishes its own scope. You can declare a variable with the same name in different branch blocks because those scopes do not overlap; the variables are independent. The conditions themselves, however, are evaluated in the enclosing scope, so variables used in a condition must be available there.

csharp
bool first = true; bool second = false; if (first) { int x = 1; } else if (second) { int x = 2; }

This is legal because the two declarations are in separate branch blocks.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.