GoLang If…else statements

Every language has its decision-making capabilities using some combination of if…else statements and Go is not an exception to that. In this post, we are going to learn about the decision-making capabilities of the Go language.

Let’s have a look at all of them one by one

if statement

This is the most simple statement in the decision-making capabilities of the Go language. It states that if the condition is true then execute the condition contained within the if block.

The syntax of an if statement is :

if condition{
  // code
}

Here the condition represents a boolean expression and will return either true or false.

  • If the condition returns true, then it will execute the code within the if block.
  • And if the condition returns false, then it will not execute the code within the if block.
working of if statement in golang

Code example 1

package main

import (
	"fmt"
)

func main() {

	var height int = 170

	if height > 150 {
		fmt.Println("Inside if block")
	}

	fmt.Println("Outside if block")
}

Output –

Inside if block
Outside if block

Here we can see that the if block is executed because our height variable satisfies the if statement condition and then the block outside of the if block is executed.

Code example 2

package main

import (
	"fmt"
)

func main() {

	var height int = 170

	if height < 150 {
		fmt.Println("Inside if block")
	}

	fmt.Println("Outside if block")
}

Output –

Outside if block

We can see here that as the condition failed, the code inside the if block didn’t execute and moved on to the part outside of the if block.

if…else statement

Well, if block code will be executed if the boolean expression evaluates to true but if it doesn’t, then it will continue executing the rest of the code without executing the if block. But what if want to execute some code if and only if the condition under if-statement is evaluated to false, this is where if…else statement comes into the picture.

The syntax of an if-else block will be like

if condition{
  // if block code
} else{
  // else block code
}

A very important note: We have written the else keyword in the same line where we closed the if block with a curly brace ( } ). It is important to do so because if we had written the else keyword in the next line as shown below, then it would have thrown an error.

if condition{
  // if block code
} 
else{  // this will give an error, so don't write it like this 

  //else block code
}
  • So, if the condition under the if statement evaluates to true, then the if block will be executed.
  • Otherwise, if the condition evaluates to false, then else block will be executed.

Code example 1

Here we will write a code where if statement condition will evaluate to true and thus if block will be executed and else block will be ignored.

package main

import (
	"fmt"
)

func main() {

	var height int = 170

	if height > 150 {
		fmt.Println("Inside if block")
	} else {
		fmt.Println("Inside else block")
	}

	fmt.Println("Outside if else block")
}

Output –

Inside if block
Outside if else block

Code example 2

Here we will fail the if condition and then will see whether else block will be executed or not.

package main

import (
	"fmt"
)

func main() {

	var height int = 130

	if height > 150 {
		fmt.Println("Inside if block")
	} else {
		fmt.Println("Inside else block")
	}

	fmt.Println("Outside if else block")
}

Output –

Inside else block
Outside if else block

Here we can see that the if block is not executed because the condition has failed but instead the else block was executed and then the rest of the code.

nested if…else statement

We can also use an if-else statement inside another if-else statement and this is known as a nested if-else statement.

Below is the simplest syntax of the nested if-else statement but we can take it up to any number of levels.

if condition {
   if condition{
      // some code
   } else{
     // some code
   }
}

Code example

package main

import (
	"fmt"
)

func main() {

	var height int = 170

	if height > 150 {
		if height < 200 {
			fmt.Println("Height is between 150 and 200")  // this if-block will be executed
		} else {
			fmt.Println("Height is greater than 200")
		}
	}

	fmt.Println("Outside nested if else block")
}

Output –

Height is between 150 and 200
Outside nested if else block

Here the height was 170, so it passes the condition inside first if statement ( height > 150 ), then it enters the corresponding if block and again encounters an if statement whose condition also evaluates to true ( height < 200 ), so, it enters in that if block to execute its code and so the corresponding else block is ignored. Afterward, the flow comes out of the nested if-else statements to execute other parts of the program.

if…else…if ladder

The simplest syntax of an if…else…if ladder

if condition1 {
   // some code
} else if condition2 {
  // some code
} else{
 // some code
}

We can have as many chains as we want, there is no limit here.

  • The flow will start from top to bottom, so it first checks the condition in the first if statement, and if it evaluates to true then that if block will be executed and all other else…if blocks will be ignored.
  • But if the condition evaluates to false, then the flow will go to the next else if statement to check its condition and if the condition evaluates to true, then the corresponding block will be executed and all other blocks will be ignored.
  • And If no condition evaluates to true then the last else block will be executed.

Code example

package main

import (
	"fmt"
)

func main() {

	var height int = 170

	if height < 100 {
		fmt.Println("Height is less than 100")
	} else if height > 100 && height < 200 {
		fmt.Println("Height is between than 100 and 200")
	} else {
		fmt.Println("Height is greater than 200")
	}
	fmt.Println("Outside if...else...if ladder")
}

Output –

Height is between than 100 and 200
Outside if...else...if ladder

Well, this is it for if…else statements. But we have a question in mind.

In other programming languages, it is required to surround the if statement’s condition with parentheses but you might have noticed that we hadn’t done it here in our programs.

Well, it is up to you whether you want to add the parentheses or not. But the go language doesn’t want you to add the parentheses as much as possible, so, even if you added the parentheses in your code and then run the go fmt command on your file, you will notice that it will remove your parentheses unless you are using them to set an order of operations.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.