# Go Decision Making (if, if-else, Nested-if, if-else-if)

Decision making is an important aspect of programming, and Go provides a variety of constructs for making decisions in your code. In this article, we'll explore the different types of decision making constructs in Go, including the if, if-else, nested-if, and if-else-if constructs.

## if statement

The if statement in Go is used to execute a block of code only if a certain condition is true. Here's an example −

### Example

package main

import "fmt"

func main() {
x := 10

if x > 5 {
fmt.Println("x is greater than 5")
}
}


### Output

x is greater than 5


This program will output x is greater than 5 because the condition x > 5 is true.

## if-else statement

The if-else statement in Go is used to execute one block of code if a certain condition is true, and another block of code if the condition is false. Here's an example −

### Example

package main

import "fmt"

func main() {
x := 10

if x > 5 {
fmt.Println("x is greater than 5")
} else {
fmt.Println("x is less than or equal to 5")
}
}


### Output

x is greater than 5


This program will output x is greater than 5 because the condition x > 5 is true.

## nested if statement

The nested if statement in Go is used to check for multiple conditions. Here's an example −

### Example

package main

import "fmt"

func main() {
x := 10
y := 20

if x == 10 {
if y == 20 {
fmt.Println("x is 10 and y is 20")
}
}
}


### Output

x is 10 and y is 20


This program will output x is 10 and y is 20 because both conditions are true.

## if-else-if statement

The if-else-if statement in Go is used to check for multiple conditions and execute different blocks of code depending on the conditions. Here's an example −

### Example

package main

import "fmt"

func main() {
x := 10

if x > 10 {
fmt.Println("x is greater than 10")
} else if x < 10 {
fmt.Println("x is less than 10")
} else {
fmt.Println("x is equal to 10")
}
}


### Output

x is equal to 10


This program will output x is equal to 10 because the condition x == 10 is true.

## Conclusion

In this article, we've explored the different types of decision making constructs in Go, including the if, if-else, nested if, and if-else-if constructs. These constructs are essential for writing programs that can make decisions based on user input, system states, and other factors.

Updated on: 18-Apr-2023

101 Views