Go If Statement

Every If statement in Go programming contains a condition or expression followed by a list of code lines. If the condition result is true, then that block of code will run. And if the condition results false, it will exit from the if statement.

The Syntax of the Go If statement is shown below.

if expression {
    block1
 }

Go If Statement Example

package main

import "fmt"

func main() {
    fmt.Print("Please enter any Integer = ")
    var num int
    fmt.Scanf("%d", &num)
    fmt.Println(num)

    if num%2 == 0 {
        fmt.Println("Even Number")
    }
}

In the above example, it checks whether the user-entered value is divisible by 2. If true, it prints the even number. What if you want to print the odd number or write some information when the condition fails? In such instances, you can use the If Else statements.

Go If Statement Example

Go If Else Condition

If the condition is True, GC will execute code inside the if block; otherwise, it runs the else code block. The Syntax of the If Else statement is

if expression {
    block1
 } else {
    block2
 }

If Else Example

package main

import "fmt"

func main() {
    fmt.Print("Please enter any Integer = ")
    var num int
    fmt.Scanf("%d", &num)
    fmt.Println(num)

    if num%2 == 0 {
        fmt.Println(num, " is an Even Number")
    } else {
        fmt.Println(num, "is an Odd Number")
    }
}
Go If Else Statement

Go Else If Statement

In real-time, there are some situations where we have to check multiple conditions to return the output. For this, Go programming supports Else If statements, which are helpful for checking various expressions. The Else if will check the expressions from top to bottom. If the first expression is evaluated as true, it will execute that code block and exit. When multiple conditions are correct, it will only run the first one and never go beyond that line of code. The syntax of the Else If in this programming is

if expression1 {
    block1
 } else if expression2 {
    block2
 } else if expression3 {
    block3
 } else {
    block4
 }

In the below example, if the user enters 520, it will execute the first block though it passes the else if condition, it will never run.

package main

import "fmt"

func main() {
    fmt.Print("Please enter the Marks (Max = 600) = ")
    var marks int
    fmt.Scanf("%d", &marks)

    if marks >= 500 {
        fmt.Println("Eligible for 10% Scholorship")
    } else if marks >= 550 {
        fmt.Println("Eligible for 20% Scholorship")
    } else if marks >= 575 {
        fmt.Println("Congratulations")
        fmt.Println("Full Schoilorship")
    } else {
        fmt.Println("Not Eligible for Scholorship")
        fmt.Println("We are Sorry")
    }
}
Else If Statement

Nested If Statement

Go programming also supports nested if statements (if inside another one). In the below example, we nested if within the else blocks. If the age is above 18, it further checks other the age is between 18 and 62. If both the conditions, i.e., (age > 18), and (age >= 18 && age <= 62) the code inside this nested if block will execute. It means Nested if helps us to check the expressions further. 

package main

import "fmt"

func main() {
    fmt.Print("Please enter the Age = ")
    var age int
    fmt.Scanf("%d", &age)

    if age < 18 {
        fmt.Println("You are Minor. Not eligible to work")
    } else {
        if age >= 18 && age <= 62 {
            fmt.Println("You can Apply for this position")
            fmt.Println("Please fill your details")
        } else {
            fmt.Println("As per Gov, you are permitted to work")
        }
    }
}
Nested If Example