A new product!

Social Media Toolkit

Download our all in one automation tool for various social media websites.

Gopher

If Else statement in Golang

Published on 10 May 2020
Last Updated on 10 May 2020

If statements are used for checking whether an expression is true of false.

If else statements can be used for controlling the flow of the program.

If the expression is true then a group of statements is executed if the expression is false then another group of statements is executed.

If statements do not have a parenthesis around the expression that is being checked.

Parenthesis are not required however but {} braces might be required for creating code blocks to be executed when the condition is true or false.

Program to demonstrate simple If statement

While writing an if statement, else block is optional and it can be omitted as demonstrated in the example given below:

package main

import "fmt"

func main() {
	if true {
		fmt.Println("hello")
	}
}

Above program produces following output:

hello

Example to demonstrate simple if else statement

Program given below demonstrates a simple if else statement that checks whether number a is true or false.

package main

import "fmt"

func main() {
	var a = 2
	// if statement to check whether a is even or odd
	if a%2 == 0 {
		fmt.Println("even")
	} else {
		fmt.Println("odd")
	}
}

Above program produces following output:

even

If expression provided to if else statement is true then first code block is executed, otherwise code provided in else code block is executed.

If with a short statement

Program given below demonstrates a simple if statement with a short statement that can be used for initializing variables that might be necessary for checking the expression or verifying whether condition is true or false.

package main

import "fmt"

func main() {
	if a:=2 ; a%2==0 {
		fmt.Println(a,"is an even number")
	}
}

Above program produces following output:

2 is an even number

Variables declared inside short statement are also accessible inside the else statement.

Program given below demonstrates how variable a defined using the shorthand syntax can be accessed inside the else block.

package main

import "fmt"

func main() {
	if a:=3;a%2==0 {
		fmt.Println(a,"is even")
	}else {
		fmt.Println(a,"is odd")
	}
}

Above program produces following output:

3 is odd

If else ladder

Program given below demonstrates how multiple conditions can be checked using the if else ladder.

package main

import "fmt"

func main() {
	var a=4
	if a==1 {
		fmt.Println("a is 1")
	}else if a==2 {
		fmt.Println("a is 2")
	}else if a==3 {
		fmt.Println("a is 3")
	}else if a==4 {
		fmt.Println("a is 4")
	}
}

Program output

Above program produces following output:

a is 4

Nested if else statements

If one or more if else statements are nested inside one another then they are called nested if else statements.

Program given below demonstrates how multiple if else statements can be nested inside one another.

// nested if else statements
package main

import "fmt"

func main() {
	var a = 4
	if a == 1 {
		fmt.Println("a is 1")
	} else {
		if a == 2 {
			fmt.Println("a is 2")
		} else {
			if a == 3 {
				fmt.Println("a is 3")
			} else {
				if a == 4 {
					fmt.Println("a is 4")
				}
			}
		}
	}
}

Program output

Above program produces following output:

a is 4