Start learning free

LESSON 66 OF 347 · CONDITIONAL STATEMENTS

If Else in C#: Choosing Between Two Branches

Lesson Read the full explanation

Many times in our life, not only we choose to do something if a condition is met, but also choose to do something different if that condition is not met. For example:

If I'm tired:
    I take a nap
Otherwise:
    I start coding

If-else Statements:

When a condition for an if statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen.

With an else statement, we can have an alternate block of code to be executed. For example:

bool amTired = false;
if(amTired) {
    Console.WriteLine("I take a nap");
}
else {
    Console.WriteLine("I start coding");
}

The code above prints "I start coding" since the expression inside the if evaluates to false and so C# will enter the else statement and execute the code inside it.

An else statement cannot exist without a corresponding if statement. This combination is refered to as an if-else statement.


Assignment
Follow the Coding Tutorial and let's practice with if-else statements!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · CONDITIONAL STATEMENTS

    If Else in C#, explained

    What if-else does

    An if statement runs a block only when its condition is true. When the condition is false, the program skips the block and carries on. That is often not enough: you want the program to do something in the other case too. An else branch gives you that second path.

    Exactly one of the two blocks runs, never both and never neither. The condition decides which.

    The syntax

    bool amTired = false;
    if (amTired) {
        Console.WriteLine("I take a nap");
    } else {
        Console.WriteLine("I start coding");
    }
    

    Here amTired is false, so C# skips the first block and runs the one after else. The program prints:

    I start coding
    

    A few details about the shape:

    • The condition goes in parentheses after if and must be a bool. C# does not treat numbers or strings as true or false, so if (1) does not compile.
    • else has no condition and no parentheses. It runs whenever the if condition was false.
    • else always belongs to an if. On its own it is a compile error.
    • Where you put else is a style choice. Both } else { on one line and else on its own line are valid. This lesson uses the first form.

    Going beyond two branches

    When there are more than two outcomes, chain tests with else if. C# checks the conditions from top to bottom and runs the first block whose condition is true. A final plain else catches whatever is left:

    int score = 7;
    if (score >= 9) {
        Console.WriteLine("A");
    } else if (score >= 5) {
        Console.WriteLine("B");
    } else {
        Console.WriteLine("C");
    }
    

    This prints B. The first condition, 7 >= 9, is false, so C# moves on; the second, 7 >= 5, is true, so it runs that block and skips the rest. Because the order matters, put the most specific test first.

    Always use braces, even for a one-line body. C# allows an if without them, but then only the next single statement belongs to the branch, and adding a second line later is a classic way to introduce a bug.

    The exercise, step by step

    The lesson starts with a variable and an if that reports a pass:

    bool passed = false;
    if (passed) {
        Console.WriteLine("Passed the test");
    }
    

    With passed set to false, this program prints nothing at all, which is a poor way to report a result. The fix has one step: attach an else branch that prints the failure message.

    Put the else keyword right after the closing brace of the if block, open a new block, print with Console.WriteLine, and close the block with its own brace. The result:

    bool passed = false;
    if (passed) {
        Console.WriteLine("Passed the test");
    } else {
        Console.WriteLine("Failed the test");
    }
    

    Now passed is false, so the program prints Failed the test. Change the first line to bool passed = true; and it prints Passed the test instead. Try both values in the editor above.

    Common mistakes

    • Putting a condition on else. else (passed == false) is a syntax error. If you need a second test, write else if (...), which chains another if after the else.
    • Using = instead of == in a condition. if (passed = true) assigns true to passed and then tests it, so the first block always runs. The compiler warns about this. Compare values with ==, or, for a bool, just write if (passed).
    • A stray semicolon. Writing if (passed); ends the statement immediately, so the block that follows is no longer attached to the if, and a following else will not compile.
    • Using two separate if statements instead of one if-else. Two independent if blocks each test their own condition, so both could run, or neither. An else guarantees that exactly one branch runs, because it is tied to the single condition above it.
    • Wrong capitalisation. C# is case-sensitive. Else and If are not keywords, and the method is Console.WriteLine, not console.writeline.

    Key takeaways

    • if runs a block when its condition is true; else runs a block when it is false.
    • Exactly one of the two blocks runs each time.
    • The condition in C# must be a bool.
    • else takes no condition and cannot appear without an if.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free