Go Nested if Statement

In Go, a nested if statement is an if statement that is placed inside another if statement. It allows you to create a more complex decision-making structure by evaluating multiple conditions in a hierarchical manner. Here’s the syntax of a nested if statement in Go:

if condition1 {
    // Code to be executed if condition1 is true

    if condition2 {
        // Code to be executed if condition2 is true
    }
}

Here’s an example of using a nested if statement in Go:

age := 25
if age >= 18 {
    fmt.Println("You are an adult")

    if age >= 60 {
        fmt.Println("You are eligible for senior citizen benefits")
    } else {
        fmt.Println("You are not eligible for senior citizen benefits")
    }
} else {
    fmt.Println("You are not an adult")
}

In the above example, the outer if statement checks if the age is greater than or equal to 18. If it’s true, it prints “You are an adult” and proceeds to the nested if statement. The nested if statement checks if the age is greater than or equal to 60, and based on that, it prints the corresponding message.

Nested if statements can be used to evaluate multiple conditions in a hierarchical manner and execute different blocks of code based on those conditions. They provide flexibility in handling complex decision-making scenarios in your Go programs.