Start learning free

LESSON 81 OF 338 · CONDITIONAL STATEMENTS

Chaining Conditionals in Go: if, else if and else

Lesson Read the full explanation

Use else if when you need to check several conditions in order.

Go evaluates the chain from top to bottom and runs only the first matching branch:

grade := 8

if grade < 5 {
    fmt.Println("Failed the test")
} else if grade <= 7 {
    fmt.Println("Passed the test")
} else if grade <= 9 {
    fmt.Println("Good Job")
} else {
    fmt.Println("Perfect")
}

Assignment
Use an if / else if / else chain to print the message for grade.


STEP 1 OF 3
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS

    Chaining Conditionals in Go, explained

    Why chain conditions

    A single if/else chooses between two outcomes. Many decisions have more: a grade can be failing, passing, good or perfect. Go handles this with else if, which adds another condition to the chain, optionally followed by a final else for everything left over.

    if first {
        // runs when first is true
    } else if second {
        // runs when first is false and second is true
    } else {
        // runs when both are false
    }
    

    Go checks the conditions from top to bottom and runs the body of the first one that is true. Once a branch has run, the rest of the chain is skipped without being evaluated. If no condition is true, the else body runs, and if there is no else, nothing runs.

    The lesson's exercise

    The code starts with one variable:

    grade := 8
    

    The program has to print a message that depends on the grade: below 5 is a fail, 5 to 7 is a pass, 8 and 9 are good, and anything higher is perfect.

    Step 1: the first branch

    Start with the lowest range: if grade < 5 prints "Failed the test". Putting this check first lets every later branch assume that the grade is at least 5.

    Step 2: two else if branches

    The next branch is else if grade <= 7, which prints "Passed the test". Because it is only reached when grade < 5 was false, it does not have to say "5 or more"; the chain already guarantees that. The branch after it, else if grade <= 9, prints "Good Job" and is reached only for grades of 8 and above. Each condition is a single upper limit, and the order of the chain supplies the lower limit.

    Step 3: the final else

    Finish with a plain else that prints "Perfect". It has no condition and catches every grade above 9, so every possible grade produces some output.

    The solution in Go

    grade := 8
    
    if grade < 5 {
        fmt.Println("Failed the test")
    } else if grade <= 7 {
        fmt.Println("Passed the test")
    } else if grade <= 9 {
        fmt.Println("Good Job")
    } else {
        fmt.Println("Perfect")
    }
    

    For grade := 8: 8 < 5 is false, 8 <= 7 is false, 8 <= 9 is true, so the program prints Good Job and skips the final else. Trying other grades gives these results:

    gradeoutput
    3Failed the test
    5Passed the test
    7Passed the test
    8Good Job
    9Good Job
    10Perfect

    The boundary grades 5, 7 and 9 are the ones to check by hand. Each belongs to the branch whose condition includes it with <=, or, for 5, to the first branch that does not match < 5.

    Chain or separate ifs?

    A chain is different from several independent if statements in a row. With separate statements, every condition is checked, and more than one body can run:

    grade := 6
    
    if grade <= 7 {
        fmt.Println("Passed the test")
    }
    if grade <= 9 {
        fmt.Println("Good Job")
    }
    

    With a grade of 6, both conditions are true, so this prints both lines. In an else if chain only the first match runs, so the same grade would print just Passed the test. Use a chain when the outcomes are mutually exclusive.

    Go details worth knowing

    • Order is part of the logic. If the chain tested grade <= 9 first, a grade of 3 would print Good Job, because 3 is less than or equal to 9 and the later branches would never be reached. Put the most specific or most restrictive checks first.
    • Layout is enforced. The else if and else keywords must sit on the same line as the closing brace before them. Starting one on a new line is a syntax error.
    • Braces and parentheses. Braces are always required, and the conditions take no parentheses.
    • An if can start with a short statement. if g := grade * 2; g > 10 { declares g and tests it, and g stays available in every else if and else branch of that chain, and nowhere after it. This lesson doesn't need it, but you will see it in Go code.
    • There is no ternary operator. Go has no condition ? a : b, so an if chain is the way to pick between values.

    Common mistakes

    • Repeating the lower bound. Writing grade >= 5 && grade <= 7 works but is redundant after the first branch; the chain already excludes anything below 5.
    • Wrong comparison at a boundary. The wording "up to 7" includes 7, so use <=. With < 7, a grade of 7 would fall through and print Good Job.
    • Using else with a condition. else grade > 9 { is invalid. A condition needs else if.
    • Starting a second chain by accident. A fresh if after a chain begins a new, independent test.

    Key takeaways

    • Conditions in an if/else if/else chain are checked in order, and only the first true branch runs.
    • Earlier branches narrow the cases for later ones, so later conditions can be shorter.
    • A final else catches everything that is left.
    • Write } else if cond { and } else { on the line of the closing brace.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free