GoLang - If..Else

3 minute read

In this tutorial, you will learn about If..Else in Go Programming with the help of example.

In Go programming, the 'if...else' statement allows you to make decisions and execute different blocks of code based on certain conditions. It is a fundamental control structure that enables your program to perform different actions based on specific criteria. In this tutorial, we will explore the basics of the 'if...else' statement in Go programming with examples, using simple and easy-to-understand language. So let’s dive in and learn about 'if...else' in Go!

Syntax:

The general syntax of the 'if...else' statement in Go is as follows:

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

1. Simple If…Else Statement

The simplest form of the 'if...else' statement executes a block of code if a given condition is true and another block if the condition is false.

Example:

package main

import "fmt"

func main() {
    var num = 10

    if num > 0 {
        fmt.Println("The number is positive.")
    } else {
        fmt.Println("The number is non-positive.")
    }
}

In the example above, we have a variable 'num' with a value of 10. The “if” condition 'num > 0' checks if the number is positive. If the condition is true, it prints “The number is positive.” Otherwise, it executes the code in the 'else' block and prints “The number is non-positive.”

2. If…Else If…Else Statement

The 'if...else if...else' statement allows you to test multiple conditions and execute different blocks of code based on the result of those conditions.

Example:

package main

import "fmt"

func main() {
    var num = 0

    if num > 0 {
        fmt.Println("The number is positive.")
    } else if num < 0 {
        fmt.Println("The number is negative.")
    } else {
        fmt.Println("The number is zero.")
    }
}

In this example, we have a variable 'num' with a value of 0. The “if” condition checks if the number is positive, and if true, it prints “The number is positive.” If the first condition is false, it moves to the next condition using the “else if” statement. Here, it checks if the number is negative and, if true, prints “The number is negative.” If both conditions are false, the code in the “else” block executes, and it prints “The number is zero.”

Nested If…Else Statement

You can nest 'if...else' statements within one another to create more complex decision-making structures.

Example:

package main

import "fmt"

func main() {
    var num = 10

    if num > 0 {
        fmt.Println("The number is positive.")
    } else {
        if num < 0 {
            fmt.Println("The number is negative.")
        } else {
            fmt.Println("The number is zero.")
        }
    }
}

In this example, we again have a variable 'num' with a value of 10. The outer “if” condition checks if the number is positive. If true, it prints “The number is positive.” If the condition is false, it moves to the nested 'if...else' statement. Here, it checks if the number is negative and, if true, prints “The number is negative.” If the nested condition is false, it executes the code in the inner “else” block and prints “The number is zero.”

Remember to pay attention to the syntax of the 'if...else' statement and ensure that the condition is enclosed within parentheses. Use comparison operators to formulate conditions and produce the desired output based on the evaluation of those conditions.

By mastering the 'if...else' statement, you can create programs that adapt to different scenarios and perform actions based on specific conditions. Practice using 'if...else' statements in your Go programs to gain a better understanding of their flexibility and power.

Conclusion

The 'if...else' statement in Go programming is a powerful tool for making decisions and executing different blocks of code based on conditions. In this tutorial, we explored the basics of using 'if...else' statements in Go with simple and easy-to-understand examples. We learned how to use a simple 'if...else' statement, an “if…else if…else” statement for multiple conditions, and even nested 'if...else' statements for more complex decision-making!

Congratulations on completing this beginner’s guide to 'if...else' in Go programming! With this knowledge, you’re now equipped to make decisions in your programs and create more dynamic and responsive applications. So go ahead, experiment with 'if...else' statements, and unlock the full potential of decision-making in your Go programs!

Updated: