Combining Conditional Statements in Golang


Conditional statements in programming are used to execute different sets of instructions based on a condition. In Golang, we have two types of conditional statements, if and switch. In this article, we will focus on how to combine conditional statements in Golang.

Combining conditional statements in Golang allows us to perform more complex checks and execute specific blocks of code based on multiple conditions. There are three ways to combine conditional statements in Golang: nested if statements, logical operators, and switch statements.

Nested If Statements

Nested if statements are used to check multiple conditions in a hierarchical manner. The inner if statements are only executed if the outer condition is true. Here's an example −

Example

package main

import "fmt"

func main() {
   num1 := 10
   num2 := 20

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

      if num1 > num2 {
         fmt.Println("num1 is greater than num2")
      } else {
         fmt.Println("num2 is greater than num1")
      }
   }
}

Output

num1 is greater than 5
num2 is greater than num1

In this example, we check if num1 is greater than 5. If it is, we check if num1 is greater than num2 or not. Depending on the result, we print the corresponding message.

Logical Operators

Logical operators in Golang are used to combine two or more conditions and perform a single check. There are three logical operators in Golang: && (and), || (or), and ! (not). Here's an example −

Example

package main

import "fmt"

func main() {
   num1 := 10
   num2 := 20

   if num1 > 5 && num2 > 10 {
      fmt.Println("Both conditions are true")
   }

   if num1 > 5 || num2 > 30 {
      fmt.Println("At least one condition is true")
   }

   if !(num1 > 5) {
      fmt.Println("num1 is not greater than 5")
   }
}

Output

Both conditions are true
At least one condition is true

In this example, we use && to check if num1 is greater than 5 and num2 is greater than 10. We use || to check if num1 is greater than 5 or num2 is greater than 30. Finally, we use ! to check if num1 is not greater than 5.

Switch Statements

Switch statements in Golang are used to perform different actions based on multiple conditions. They are similar to nested if statements, but they provide a more concise syntax. Here's an example −

Example

package main

import "fmt"

func main() {
   num := 3

   switch num {
   case 1:
      fmt.Println("num is equal to 1")
   case 2:
      fmt.Println("num is equal to 2")
   case 3:
      fmt.Println("num is equal to 3")
   default:
      fmt.Println("num is not equal to 1, 2, or 3")
   }
}

Output

num is equal to 3

In this example, we use a switch statement to check the value of num. Depending on the value, we print the corresponding message. If none of the cases match, we print the default message.

Conclusion

Combining conditional statements in Golang allows us to perform more complex checks and execute specific blocks of code based on multiple conditions. We can use nested if statements, logical operators, and switch statements to combine conditional statements in Golang. When choosing which method to use, consider the complexity of the condition and the readability of the code.

Updated on: 07-Apr-2023

711 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements