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.

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");
}

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");
}

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"; }

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

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Variables & Data Types
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.
Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.
Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.
Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.