Go logo

The Decision-making statements in Go – If statement

Just like in real life, we make decisions based on certain choices or available options; the decision making in Go and many other languages is done by using the if..else statements, switch case, etc.

In the if..else statement, a block of code is executed when the specified condition is fulfilled.

A few points about the Go If statement

  • The if statement in Go is used to check the condition.
  • If it evaluates to true, the body of the statement is executed.
  • If it evaluates to false, the if block is skipped.
  • You may use the else to execute another block of code in case the condition is false. (Explained in next section)
  • You may also use if the else-if chain and nested if statements in Go. Explained in the last part of this tutorial.
  • Sometimes these decision-making statements also are termed as the Control flow statements.

Now let us go through if, else, if else-if and nested if statements one by one with examples.

The if statement in Go

The general way of using the Go if statement is:

if(boolean_expression) { 

   /* If the condition is true, code here will be executed */ 

}
  • If the boolean expression evaluates to authentic, then the block of code within the if statement is executed.
  • If the boolean expression evaluates to false, then the first set of code following the end of the if statement (following the closing curly brace) is executed.
Further Reading:  An introduction to Maps in Go (GoLang)
An example of a simple if statement

In this example, we will check the value of the variable in the if statement. If it is 5 or more than the statement will execute:

package main


import "fmt"


func main() {

   var x int = 5

   if( x >= 5 ) {

      fmt.Printf("The value of x = 5 or more" )

   }

}

 

Output:

The value of x = 5 or more

What is else statement

An if statement can be followed by an optional else statement, which executes if the Boolean expression is false.

The general structure of if else:

if(boolean_expression) { 

      /* If the condition is true, code here will be executed */

} else { 

   /* If the condition is false, code here will be executed */

}
An example of using the else

Extended the above example and added an else statement to execute a statement if the condition is false.

package main

import "fmt"

func main() {

   var x int = 4;

   if( x >= 5 ) {

      fmt.Printf("The value of x = 5 or more" );

   } else {

      fmt.Printf("The value of x is less than 5" );

   }

}

Output:

The value of x is less than 5

The else part executed as the value of variable x is 4.

Further Reading:  Arrays in Go Language
Go If else-if ladder

Rather than just two options, you may have three or more options to decide from. The if else-if ladder allows you to translate this situation in Go coding.

For example, if the color selected by the user is Black then perform one block of code. In the case of green, some other block of code, and so on.

The if statement starts executing from the top towards down. Wherever it is evaluated as true, it will execute that block of code, and the rest of the conditions are bypassed.

We can have N numbers of if-else statements. It has no limitations.

The curly braces{] is compulsory in the if-else statement even in case you’ve got one statement in it. The else-if and else keyword must be on the same line following the closing curly brace}.

An example of if else-if statement
package main

import "fmt"

func main() {


var x int = 10

// start checking value of x

if(x == 0) {
                fmt.Printf("The value of variable x = 0");
} else if(x == 5) {
                fmt.Printf("Value of a is 5");
} else if(x == 10) {
                fmt.Printf("Value of a is 10");

} else {
                fmt.Printf("All conditions are false!");

}

}

Output

Value of a is 10

As the third else-if statement is evaluated as true, so it executed the statement in it.

Further Reading:  What is a pointer in Go Language?
The nested if statements in Golang

It’s always legal in Go language to nest if-else statements, which means that you can use one if or else if statement inside another if or else if statement(s).

The general structure of nested if

if( boolean_expression 1) {

   /* Execute the code when the Boolean expression 1 is true */

   if(boolean_expression 2) {

      /* Execute the code when the Boolean expression 2 is true */

   }

}
An example of nested if

We will execute a statement inside the inner (nested) if statement:

package main 

import "fmt" 

func main() { 

   var x int = 5 

   var y int = 10 

   if( x >=5 ) { 

      if( y >= 10 )  { 

         fmt.Printf("We are inside the nested if statement" ); 

      } 

   } 

}

Output:

We are inside the nested if statement