Lesson 1
Introduction to Control Structures in Go
Introduction to Control Structures

Are you ready to dive deeper into Go? In this lesson, we will learn about control structures. Control structures are fundamental building blocks in programming that empower your code to take different actions based on various situations.

What You'll Learn

We'll be focusing on the if and else statements. These are the cornerstones of decision making in Go. To illustrate, suppose you want to travel, but your ability to do so depends on whether you have a passport. In programming terms, we model this real-world scenario as follows:

Go
1package main 2 3import "fmt" 4 5func main() { 6 7 hasPassport := true 8 9 if hasPassport { 10 fmt.Println("You are eligible to travel.") 11 } else { 12 fmt.Println("You cannot travel without a passport.") 13 } 14}

As you can see, the if statement checks whether the condition — in this case, having the passport being true is met. If so, the action within the if block, printing "You are eligible to travel," is executed. Otherwise, the code within the else block, which states "You cannot travel without a passport," is executed.

A Note on Syntax

In addition to understanding the if and else statements, mastering the syntax, particularly the use of braces {} that delineate blocks of code, is crucial. Braces, in combination with the if and else keywords, define the scope and block of instructions attached to each condition in Go.

Go
1if passport { 2 fmt.Println("You are eligible to travel.") // This statement belongs to the if condition 3 // Any additional code dependent on the passport being true would be placed here 4} else { 5 fmt.Println("You cannot travel without a passport.") // This statement belongs to the else condition 6 // Code to execute when the passport condition is false would be placed here 7} 8// Any code here would not be part of the if-else block and executes regardless of the passport condition

After each if or else statement, a block of code is enclosed within braces {} to introduce the instructions that should be executed if the condition is met. This syntax structure ensures your program can clearly follow which instructions belong to which condition, thereby facilitating an organized and error-free decision-making process.

Role of Parentheses in if Statements

Unlike some other programming languages, Go does not require parentheses around the condition of an if statement. This helps make the code cleaner and easier to read.

Go
1if hasPassport { 2 fmt.Println("You are eligible to travel.") 3}

However, if you prefer, you can still use them for clarity, though it is not a common practice in Go:

Go
1if (hasPassport) { 2 fmt.Println("You are eligible to travel.") 3}
Boolean Checks and Operators

In Go, you can use both shorthand and full syntax for boolean checks. The shorthand simply uses the boolean variable itself:

Go
1if hasPassport { 2 fmt.Println("You can travel.") 3}

Alternatively, you can compare the boolean variable with a boolean literal using equality/inequality operators. This is often used for clarity or when dealing with more complex boolean expressions:

Go
1if hasPassport == true { 2 fmt.Println("You can travel.") 3} else if hasPassport != true { 4 fmt.Println("You cannot travel.") 5}
Using "if" Without "else"

In Go, an if statement can also be used independently, without the accompanying else block. This allows you to execute a block of code only when a certain condition is true, without needing to define alternate actions if the condition is false. This can be particularly useful when you only need to check for or handle specific cases. Here’s an example:

Go
1package main 2 3import "fmt" 4 5func main() { 6 temperature := 30 7 8 if temperature > 25 { 9 fmt.Println("It's a hot day!") // This statement will execute only if the temperature is greater than 25 10 } 11 // No else block is used here, so nothing else is executed only if the temperature is not greater than 25 12}

In this example, "It's a hot day!" is printed only if the temperature is greater than 25. If the condition isn't met, the program simply continues without executing any additional actions related to this condition. Keep in mind that any code following the if check is still executed, regardless of the condition state.

Why It Matters

Control structures are essential because they enable our programs to react differently to various inputs or conditions. Consider it this way — if we couldn't make decisions based on different circumstances in our lives, life would be chaotic! The same goes for your code. By using if and else, you can adapt your code to different inputs or situations, a fundamental characteristic of intelligent applications.

Ready for this exciting journey? Let's delve into conditional statements through practice.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.