Go If Else

In this tutorial, you will learn about the if-else statement in the Go language along with its syntax, usage with the help of examples.


The if..else statement in the Go programming language is used as a control statement. It is used to perform operations based on some specific condition. If the condition evaluates to true the body of the statement is executed. The body of the statement is skipped if the condition evaluates to false.

There are the following variants of the if statements available in the Go language.

  • If statement
  • If-else statement
  • If else-if ladder
  • Nested if

Now let’s understand each of the above variants of the if…else statement one by one.


Go if Statement

The if statement is used to perform certain operations only if a given condition is true. If the condition is false then the body of the if the condition is skipped.

Syntax

if test_expression {
//statement(s) to be executed if the condition is true
}

Note that the parentheses around conditions in Go are not required, but that the braces are required.

Example 1

Let’s check a simple example of if statement in the Go language.

// Go Program to display a message if the number is less than 100
package main
import "fmt"

func main() {
    var val = 89
    if val < 100 {
        fmt.Println("The number is: ", val)
    }
    fmt.Println("Next statement after if condition")
}

Output

The number is:  89
Next statement after if condition

From the above example, you can see that the number is less than 100. So the if condition becomes true and the statement inside the if condition is printed in the output.

Example 2

Now let’s check an example with a number that is greater than 100.

// Go Program to display a message if the number is less than 100
package main
import "fmt"

func main() {
    var val = 112
    if val < 100 {
        fmt.Println("The number is: ", val)
    }
    fmt.Println("Next statement after if condition")
}

Output

Next statement after if condition

In this case, the body of the if condition is skipped since the number is greater than 100 and the condition becomes false.


Go if…else Statement

The if…else statement is the extension of the if statement. The if statement performs an operation only if the given condition is correct. Using the if…else statement two operations can be performed i.e. one for the correctness of the given condition another is incorrectness of the given condition. Here, you must remember that the if and else block cannot be executed simultaneously. Either of the ones will be executed.

Syntax

The syntax of the if…else statement is as below.

if(test expression){ 
//code to be executed if condition is true 
}else{ 
//code to be executed if condition is false 
}

Example

Let’s check a simple example of the if…else statement in Go language.

// Go if...else example
package main
import "fmt"

func main() {
    num := 145
    if num < 100 {
        fmt.Println("The number is: ", num)
        fmt.Println("The number is less than 100")
    } else {
        fmt.Println("The number is: ", num)
        fmt.Println("The number is greater than 100")
    }

    fmt.Println("Next statement after if...else condition")
}

Output

The number is: 145
The number is greater than 100
Next statement after if…else condition

Example: with input from the user

// Go if...else example
package main

import "fmt"

func main() {
    fmt.Println("Enter a number")
    var num int
    fmt.Scanln(&num)
    if num < 100 {
        fmt.Println("The number is: ", num)
        fmt.Println("The number is less than 100")
    } else {
        fmt.Println("The number is: ", num)
        fmt.Println("The number is greater than 100")
    }
    fmt.Println("Next statement after if...else condition")
}

Output

Enter a number
95
The number is: 95
The number is less than 100
Next statement after if…else condition

C if…else if Ladder

The if…else statement checks for two possibilities depending upon whether the test expression is true or false. Sometimes it is required to make decisions from more than two choices.

The if…else if ladder allows you to check more than two possibilities and perform different operations according to the test conditions.

Syntax

The syntax of if…else if ladder in the Go language is as follows.

if (test expression1) {
// code to be executed if test expression1 is true
}
else if(test expression2) {
// code to be executed if test expression2 is true
}
else if (test expression3) {
// code to be executed if test expression3 is true
}
.
.
else {
// code to be executed if all the test expressions are false  
}

Example

// Go if...else if ladder example
package main
import "fmt"
func main() {
    fmt.Println("Enter marks")
    var marks int
    fmt.Scanln(&marks)
    if marks >= 90 {
        fmt.Println("Grade A+")
    } else if marks >= 80 && marks < 90 {
        fmt.Println("Grade A")
    } else if marks >= 60 && marks < 80 {
        fmt.Println("Grade B")
    } else if marks >= 40 && marks < 60 {
        fmt.Println("Grade C")
    } else {
        fmt.Println("Fail")
    }
}

Output 1

Enter marks
78
Grade B

Output 2

Enter marks
35
Fail

C Nested if…else

It is also possible to place an if…else statement inside the body of another if..else statement. Note that the same thing also can be achieved using the if…else ladder. In this case one statement from multiple conditions.

Syntax

if (condition1) 
{
   // Executes when condition1 is true
   if (condition2) 
   {
      // Executes when condition2 is true
   }
}

Example

// Go Nested if...else example
package main

import "fmt"

func main() {

    // Local variable declaration
    var marks int

    // Taking input from the user
    fmt.Println("Enter marks")
    fmt.Scanln(&marks)

    // Outer if condition
    if marks > 80 {

        //Body of outer if condition
        fmt.Println("Marks is above 80")

        //Nested if condition
        if marks > 90 {
            fmt.Println("Marks is above 90")
        }
    }
    fmt.Printf("Marks is: %d", marks)
}

Output

Enter marks
92
Marks is above 80
Marks is above 90
Marks is: 92
Enter marks
85
Marks is above 80
Marks is: 85

 

Please get connected & share!

Categories

Advertisement