Go else if statement

Gayathri. B

 Mastering the else if Statement in Go

Conditional logic is the backbone of every non‑trivial program. While an if…else pair is enough for many checks, real‑world code often needs to test more than two possibilities. That’s where Go’s else if shines—letting you chain additional conditions without nesting awkwardly or duplicating logic.

Why else if?

Imagine you’re building a weather‑aware app:
  1. If the temperature tops 30 °C, show a heat‑warning banner.
  2. Otherwise, if it’s under 10 °C, flash a cold‑alert.
  3. For everything in between, just greet the user with “Have a nice day!”.
Trying to model this with plain if…else would force you into either deep indentation or repeated checks. else if solves the problem elegantly.

Syntax 

if condition1 {
    // Executes when condition1 is true
} else if condition2 {
    // Executes when condition1 is false *and* condition2 is true
} else {
    // Executes when both condition1 and condition2 are false
}

Think of else if as a middle step: the runtime evaluates condition1. If it’s false, Go immediately evaluates condition2. Only when every prior test fails does the final else clause run.

Here’s a clean, slightly refactored version that still demonstrates the use of else if:

Program:

package main

import "fmt"

func main() {
    currentHour := 22 // 24‑hour clock time

    if currentHour < 10 {
        fmt.Println("Good morning.")
    } else if currentHour < 20 {
        fmt.Println("Good day.")
    } else {
        fmt.Println("Good evening.")
    }
}

Output:

Good evening.

How the else if Logic Works in This Example

In the first snippet, the value of time is 22.

  1. The program checks if time < 10 — this is false because 22 is not less than 10.
  2. It then evaluates the else if time < 20 branch, which is also false because 22 is not less than 20.
  3. Since both previous conditions failed, control falls through to the final else, and the program prints “Good evening.”
Change time to 14 and the second condition (time < 20) becomes true, so the output would instead be “Good day.”

Another else if Example

Below is a second demonstration using two variables, a and b. The program determines whether a is less than, greater than, or equal to b, and prints the appropriate message.

package main

import "fmt"

func main() {
    a := 14
    b := 14

    if a < b {
        fmt.Println("a is less than b.")
    } else if a > b {
        fmt.Println("a is greater than b.")
    } else {
        fmt.Println("a and b are equal.")
    }
}

Output when both are 14:

a and b are equal.

Important: When multiple conditions could be true, only the first true branch runs.
In this example x >= 10 is checked before x > 20, so the second test is never reached if the first one succeeds.

package main

import "fmt"

func main() {
    value := 30

    if value >= 10 {
        fmt.Println("value is 10 or greater.")
    } else if value > 20 {
        fmt.Println("value is greater than 20.")
    } else {
        fmt.Println("value is less than 10.")
    }
}

Output with value := 30:

value is 10 or greater.

Because the first condition (value >= 10) is true, the program executes that branch and skips the remaining tests. Try changing value to see the other paths in action.




Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send