Start learning free

LESSON 83 OF 404 · CONDITIONAL STATEMENTS

Chaining Conditional Statements in Go

Lesson

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 4
    Ctrl + Enter to check