Go > Core Go Basics > Control Flow > If statements

'if-else if-else' Statement in Go

This snippet demonstrates the 'if-else if-else' statement in Go, which allows for multiple conditional checks.

The 'if-else if-else' structure

This code snippet declares an integer variable 'score' and initializes it to 75. The code uses an 'if-else if-else' statement to check the score against different ranges. If 'score' is greater than or equal to 90, it prints "Excellent!". If not, it checks if 'score' is greater than or equal to 80, and so on. If none of the 'if' or 'else if' conditions are true, the 'else' block is executed, printing "Needs improvement."

package main

import "fmt"

func main() {
	score := 75

	if score >= 90 {
		fmt.Println("Excellent!")
	} else if score >= 80 {
		fmt.Println("Very good!")
	} else if score >= 70 {
		fmt.Println("Good.")
	} else {
		fmt.Println("Needs improvement.")
	}
}

Concepts Behind the Snippet

The 'if-else if-else' statement provides a way to check multiple conditions sequentially. The conditions are evaluated from top to bottom. As soon as one condition is true, the corresponding code block is executed, and the rest of the 'else if' and 'else' blocks are skipped.

Real-Life Use Case

This structure is useful when you need to categorize data based on different ranges or levels. For example, grading systems, classifying customers into different tiers based on their spending, or determining the severity of an error based on its type.

Best Practices

  • Ensure that the conditions in the 'if' and 'else if' statements are mutually exclusive where possible to avoid unexpected behavior.
  • Use a clear and consistent naming convention for variables used in the conditions.
  • The 'else' block is optional but recommended as a catch-all for cases that don't match any of the specified conditions.

Interview Tip

Be prepared to explain the difference between a series of independent 'if' statements and an 'if-else if-else' chain. Understand the performance implications of each approach, particularly when dealing with a large number of conditions.

Alternatives

In some cases, a 'switch' statement can be a more readable alternative to a long 'if-else if-else' chain, especially when checking the value of a single variable against multiple possible values.

Pros

  • Clear and concise way to handle multiple conditional checks.
  • Easy to understand and maintain.

Cons

  • Can become difficult to read and maintain if the number of 'else if' blocks is too large.
  • May not be the most efficient solution for complex conditional logic.

FAQ

  • What happens if multiple conditions in an 'if-else if-else' statement are true?

    Only the code block corresponding to the first true condition is executed. The remaining conditions are not evaluated.
  • Is the 'else' block required in an 'if-else if-else' statement?

    No, the 'else' block is optional. If none of the 'if' or 'else if' conditions are true, and there is no 'else' block, then no code is executed.