Go If-Else

Introduction

Conditional statements are a fundamental part of any programming language. In Go, the if-else statement allows you to execute a block of code based on whether a condition is true or false. In this chapter, you will learn the syntax and usage of if-else statements in Go, with examples to illustrate different scenarios.

If-Else Statement Syntax

Basic If Statement

The basic if statement evaluates a condition and executes a block of code if the condition is true.

Syntax:

if condition {
    // code to execute if condition is true
}

Example:

package main

import "fmt"

func main() {
    var a int = 10

    if a > 5 {
        fmt.Println("a is greater than 5") // Output: a is greater than 5
    }
}

If-Else Statement

The if-else statement allows you to execute one block of code if the condition is true, and another block if the condition is false.

Syntax:

if condition {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}

Example:

package main

import "fmt"

func main() {
    var a int = 10

    if a > 5 {
        fmt.Println("a is greater than 5") // Output: a is greater than 5
    } else {
        fmt.Println("a is not greater than 5")
    }
}

If-Else If-Else Statement

The if-else if-else statement allows you to check multiple conditions sequentially and execute corresponding blocks of code.

Syntax:

if condition1 {
    // code to execute if condition1 is true
} else if condition2 {
    // code to execute if condition2 is true
} else {
    // code to execute if none of the conditions are true
}

Example:

package main

import "fmt"

func main() {
    var a int = 10

    if a > 15 {
        fmt.Println("a is greater than 15")
    } else if a > 5 {
        fmt.Println("a is greater than 5 but less than or equal to 15") // Output: a is greater than 5 but less than or equal to 15
    } else {
        fmt.Println("a is less than or equal to 5")
    }
}

Using Short Statement with If

In Go, you can include a short statement before the condition in an if statement. This short statement is executed before the condition is evaluated and is useful for initializing variables.

Syntax:

if statement; condition {
    // code to execute if condition is true
}

Example:

package main

import "fmt"

func main() {
    if a := 10; a > 5 {
        fmt.Println("a is greater than 5") // Output: a is greater than 5
    }
}

Nested If-Else Statements

You can nest if-else statements within each other to check multiple levels of conditions.

Example:

package main

import "fmt"

func main() {
    var a int = 10
    var b int = 20

    if a > 5 {
        if b > 15 {
            fmt.Println("a is greater than 5 and b is greater than 15") // Output: a is greater than 5 and b is greater than 15
        } else {
            fmt.Println("a is greater than 5 but b is not greater than 15")
        }
    } else {
        fmt.Println("a is not greater than 5")
    }
}

Conclusion

The if-else statement in Go is used for making decisions based on conditions. By understanding how to use basic if, if-else, if-else if-else, and nested if-else statements, you can control the flow of your programs and execute code based on various conditions. Additionally, using a short statement with if can help streamline your code and make it more readable.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top