Conditional Statements in C#: if , if..else, Nested if, if-else-if

Learn how conditional statements work in C#, including if, if-else, nested if, and if-else-if ladders, with examples for each.

The Basic if Statement

The if statement executes a block of code only when a specified condition evaluates to true, forming the most basic building block of decision-making in C#.

The if-else Statement

Adding an else block allows your program to execute an alternative set of instructions when the original condition is false, covering both possible outcomes of a single condition.

Nested if Statements

Nested if statements place one if block inside another, allowing you to check additional conditions only after an outer condition has already been satisfied, useful for multi-layered decision logic.

The if-else-if Ladder

When you need to check multiple mutually exclusive conditions in sequence, an if-else-if ladder lets you test each condition in order and execute the first matching block, falling back to a final else if none match.

Conclusion: Conditional statements are at the heart of decision-making in any C# program. Mastering if, if-else, nested if, and if-else-if ladders gives you the tools to handle virtually any branching logic your application needs.

Each Form in Code

Reading about branching only goes so far. The snippets below show the exact shape of every form, using a simple temperature value so you can trace what prints.

int temp = 32;

// if
if (temp > 30)
{
    Console.WriteLine("It is hot today.");
}

// if-else
if (temp >= 18)
    Console.WriteLine("No jacket needed.");
else
    Console.WriteLine("Take a jacket.");

// nested if
bool isRaining = false;
if (temp > 25)
{
    if (!isRaining)
    {
        Console.WriteLine("Good day for the beach.");
    }
}

The condition inside the brackets must be a bool. Unlike C or C++, C# will not treat 0 or 1 as false or true, so if (count) is a compile error. That rule quietly prevents a whole family of bugs.

Worked Example: Grading Marks With an else-if Ladder

A ladder is checked from top to bottom, and the first true condition wins. That is why the highest threshold must come first.

Console.Write("Enter marks (0-100): ");
int marks = int.Parse(Console.ReadLine() ?? "0");

string grade;
if (marks < 0 || marks > 100)
    grade = "Invalid";
else if (marks >= 90)
    grade = "A";
else if (marks >= 75)
    grade = "B";
else if (marks >= 50)
    grade = "C";
else
    grade = "Fail";

Console.WriteLine($"Grade: {grade}");

For an input of 82, the first check (invalid) is false, 90 is false, 75 is true, so the program assigns "B" and skips every remaining branch. If you swapped the order and tested marks >= 50 first, every passing student would get a C, because that branch would always match before the others.

When a switch or Ternary Reads Better

An if-else ladder is flexible, but it is not always the cleanest choice. Pick the tool that matches the shape of the decision:

SituationGood fit
Ranges, mixed conditions, several variablesif / else-if ladder
One variable compared against fixed valuesswitch statement
Picking a value based on patterns or rangesswitch expression
Choosing between two values in one lineternary ? :

Modern C# switch expressions support relational patterns, so the grading logic above can also be written compactly:

string grade = marks switch
{
    < 0 or > 100 => "Invalid",
    >= 90        => "A",
    >= 75        => "B",
    >= 50        => "C",
    _            => "Fail"
};

string label = marks >= 50 ? "Pass" : "Fail";

Pitfalls to Watch For

  • Skipping braces. Without braces only the next single statement belongs to the if. Adding a second line later silently runs it every time.
  • Dangling else. In nested code, an else pairs with the nearest unmatched if. Braces make the pairing explicit.
  • Deep nesting. Three or four levels become hard to follow. Return early or combine conditions with && instead.
  • Overlapping ranges. Leave no gaps and no overlaps between ladder branches, and keep a final else for unexpected values.
  • Comparing strings carelessly. For user input, use string.Equals(a, b, StringComparison.OrdinalIgnoreCase) so "Yes" and "yes" match.

Takeaway: put the most specific or highest-priority condition first, always use braces, and switch to a switch expression when a single value drives the decision.

Ready to Master C# Programming Course?

Join Uncodemy's hands-on training program with expert mentors and real-world projects.