Conditional Statements in Golang

Conditional Statements in Golang
GolangImage

Like many languages Golang also have support to perform conditional logic. Conditional statements are the building block of programming languages. In Golang we have the following conditional statements:

  • The if statement – executes code if condition is true.
  • The if…else statement – executes some if a condition is true and another code if that condition is false.
  • The if…else if….else statement – executes different codes for more than two conditions. Also called as if else ladder.
  • The switch…case statement – selects one of many blocks of code to be executed.

Golang – if Statement

The if statement is used to execute a block of code only if the specified condition evaluates to true.

Syntax :

if condition {
       // code to be executed if condition is true
}

Let’s see an example to understand it. In below example if the condition is true then only the code block will get executed. As 4 is even number the x%2 == 0 statement is true. Thus the code block is executed.

package main
 
import (
	"fmt"
)
 
func main() {
	var s = "This will be printed on screen"
	x := 4
	
	if x % 2 == 0 {
		fmt.Println(s)
	}
	
}

Golang – if…else Statement

The if….else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false.

Syntax :

if  condition { 
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

Let’s see an example of Odd/Even number. A non-zero number is either odd or even. This is a perfect case to use IF-ELSE conditional statement.

In below code 100 is a even number hence the output will be Even.

package main
 
import (
	"fmt"
)
 
func main() {
	x := 100
 
	if x % 2 == 0 {
		fmt.Println("Even")
	} else {
		fmt.Println("Odd")
	}
}

In below code 101 is a odd number hence the output will be Odd.

package main
 
import (
	"fmt"
)
 
func main() {
	x := 101
 
	if x % 2 == 0 {
		fmt.Println("Even")
	} else {
		fmt.Println("Odd")
	}
}

Golang – if…else if…else Statement

This statement is also called as if-else ladder as it contains multiple check point where the condition is evaluated and if it does not satisfy the criteria, the code execution flow moves to other conditional statement.

Syntax:

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

Let’s see the example code. We have declared x equal to 150. We have now multiple checks to display number of points a user have. In this case the last ELSE code block will get executed as none of the IF or ELSE-IF condition are satisfied.

package main
 
import (
	"fmt"
)
 
func main() {
	x := 150
 
	if x == 10 {
		fmt.Println("You have 10 points.")
	} else if x == 50 {
		fmt.Println("You have 50 points.")
	} else {
		fmt.Println("You have more than 100 points.")
	}
}

Golang – Nested If Else

Nested IF-ELSE blocks are also possible in Golang. The general format remains the same. The format for only nested IF is show below.

if condition {
  //Do something
  if condition2 { 
    //Do something
  }
  //Do something
}

Format for Nested IF-ELSE

if condition1 {
   //....
   if condition2 {
      //...
   } else {
      //...
   }
  //...
}

Below combination is also possible for nested if else

if condition1 {
   //...
} else {
   //...
   if condition2 {
      //...
   } else {
     //....
   }
   //....
}

Let’s see a working example of nested if else. In below program we print the max of three numbers using nested if else.

package main

import "fmt"

func main() {
    a := 1
    b := 2
    c := 3
    if a > b {
        if a > c {
            fmt.Println("Biggest is a")
        } else if b > c {
            fmt.Println("Biggest is b")
        }
    } else if b > c {
        fmt.Println("Biggest is b")
    } else {
        fmt.Println("Biggest is c")
    }
}

If with short statement

If statement also supports a statement before the condition. This statement will be executed before the condition. There can also be new initialized variable in the statement. Below is the format for that.

if statement; condition {
   //Do something
}

The initialization if present in the statement will be a short declaration. Notice that var keyword is not supported in the statement. Let’s see a working example

package main

import "fmt"

func main() {
    if a := 6; a > 5 {
        fmt.Println("a is greater than 5")
    }
}

The variable that is initialized in if statement is available inside all the branches. As in below example variable a is also available in the else block.

package main

import "fmt"

func main() {
    if a := 1; a > 5 {
        fmt.Println("a is greater than 5")
    } else {
        fmt.Println("a is less than 5")
    }
}

There are number of examples that you can play around. That’s all floks.

Leave a Reply

Your email address will not be published.