Go from Beginner to Expert: A Complete Guide to Learn Golang PART-4

Step-by-Step Guide to understand Control Statements(if-else) of Golang

Md. Faiyaj Zaman
4 min readJan 8, 2023

--

Welcome to this tutorial on control statements (if-else) in Golang. In this tutorial, we will be discussing the various control statements available in Golang and how to use them effectively in your code.

To understand Variables, constant and types in Golang, Please check the previous step by step guide.

Control statements are an essential part of programming, as they allow you to control the flow of your code execution. They allow you to specify the conditions under which certain code blocks should be executed, which can save you time and effort when writing code.

There are several types of control statements available in Golang, including if-else statements, switch statements, for loops, and break and continue statements. Each of these control statements serves a specific purpose and can be used in various ways to achieve different outcomes in your code.

if-else statements

Let’s start by looking at if-else statements. If-else statements are a type of control statement that allows you to specify a set of conditions and the code that should be executed if those conditions are met.

1Syntax and examples of using if-else statements

Here is the syntax for an if-else statement in Golang:

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

Here is an example of an if-else statement in action:

package main

import "fmt"

func main() {

x := 10 y := 5

if x > y {

fmt.Println("x is greater than y")

} else {

fmt.Println("y is greater than x")

}

}

In this example, we are declaring two variables, x and y, and assigning them the values of 10 and 5, respectively. Then, we have an if-else statement that is checking whether x is greater than y. If this condition is true, the code block fmt.Println(“x is greater than y”) will be executed. If the condition is false, the code block fmt.Println(“y is greater than x”) will be executed instead.

2Nesting if-else statements

It is also possible to nest if-else statements within one another. This allows you to specify multiple conditions and the code that should be executed based on those conditions. Here is an example of nested if-else statements in Golang:

package main

import "fmt"

func main() {

x := 10
y := 5
z := 15

if x > y {

if x > z {

fmt.Println("x is the greatest")

} else {

fmt.Println("z is the greatest")
}

} else {

if y > z {

fmt.Println("y is the greatest")

} else {

fmt.Println("z is the greatest")

}

}

}

In this example, we are declaring three variables, x, y, and z, and assigning them the values of 10, 5, and 15, respectively. The first if-else statement is checking whether x is greater than y. If this condition is true, the second if-else statement is then checking whether x is greater than z. Depending on the result of these conditions, the appropriate code block will be executed.

3Using if-else statements with short statements

Golang also allows you to use short statements before the condition in an if-else statement. These short statements are usually used to initialize a variable that is only used within the if-else statement. Here is an example of using a short statement in an if-else statement in Golang:

package main

import "fmt"

func main() {

x := 10
y := 5

if z := x + y; z > x {

fmt.Println("z is greater than x")

} else {

fmt.Println("x is greater than z")

}

}

In this example, the short statement z := x + y is initializing the variable z with the value of x + y. This variable is then used in the condition z > x, which determines which code block is executed.

These are just a few examples of how to use if-else statements in Golang. As you can see, they are a powerful tool for controlling the flow of your code execution based on certain conditions.

In the next section of this tutorial, we will be discussing switch statements in Golang. Stay tuned for more on control statements in Golang!

To stay up-to-date on our latest posts, please click on the Follow button and subscribe to our email notifications.

Kindly explore the complete series on Golang, which presents a step-by-step guide, available here.

--

--