BEST SITE FOR WEB DEVELOPERS
Golang. Lessons for beginners

Ua

Go else if Statement


The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

if condition1 {
   // code to be executed if condition1 is true
} else if condition2 {
   // code to be executed if condition1 is false and condition2 is true
} else {
   // code to be executed if condition1 and condition2 are both false
}

Using The else if Statement

Example

This example shows how to use an else if statement.

package main
import ("fmt")

func main() {
  time := 22
  if time < 10 {
    fmt.Println("Good morning.")
  } else if time < 20 {
    fmt.Println("Good day.")
  } else {
    fmt.Println("Good evening.")
  }
}

Result:

Good evening.
Try it Yourself »

Example explained

In the example above, time (22) is greater than 10, so the first condition is false. The next condition, in the else if statement, is also false, so we move on to else condition since condition1 and condition2 are both false - and print to the screen "Good evening".

However, if the time was 14, our program would print "Good day."

Example

Another example for the use of else if.

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 more than b.")
  } else {
    fmt.Println("a and b are equal.")
  }
}

Result:

a and b are equal.
Try it Yourself »

Example

Note: If condition1 and condition2 are BOTH true, only the code for condition1 are executed:

package main
import ("fmt")

func main() {
  x := 30
  if x >= 10 {
    fmt.Println("x is larger than or equal to 10.")
  } else if x > 20 {
    fmt.Println("x is larger than 20.")
  } else {
    fmt.Println("x is less than 10.")
  }
}

Result:

x is larger than or equal to 10.
Try it Yourself »