Go Lang - Control Flow: Mastering Conditional Statements, Loops, and Switch Statements

avinashtechlvr

Avinash Chodavarapu

Posted on April 8, 2023

Go Lang - Control Flow: Mastering Conditional Statements, Loops, and Switch Statements

Welcome back to our Go Lang tutorial series, where we aim to make learning Go as accessible and enjoyable as possible. In this post, we'll explore control flow in Go, including conditional statements, loops, and switch statements. Control flow is crucial for creating dynamic and efficient programs, as it enables you to control the execution order of your code based on specific conditions. Let's dive into each of these concepts and learn how to use them effectively in Go.

1. Conditional Statements (if, else if, and else)

Conditional statements in Go are used to execute a block of code only if a specific condition is met. The most common conditional statements in Go are if, else if, and else.

Image description

Example:

package main

import "fmt"

func main() {
    age := 18

    if age >= 21 {
        fmt.Println("You are allowed to drink.")
    } else if age >= 18 {
        fmt.Println("You are allowed to vote.")
    } else {
        fmt.Println("You are too young.")
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:


You are allowed to vote.
Enter fullscreen mode Exit fullscreen mode

2. Loops (for)

Go has only one loop construct - the for loop. There are three variations of the for loop:

  • Simple loop (with a loop counter)
  • While loop (using a condition)
  • Infinite loop

Image description

Example:


package main

import "fmt"

func main() {
    // Simple loop
    for i := 0; i < 5; i++ {
        fmt.Println("Simple loop iteration:", i)
    }

    // While loop
    j := 0
    for j < 5 {
        fmt.Println("While loop iteration:", j)
        j++
    }

    // Infinite loop
    // Uncomment the lines below to see the infinite loop in action
    // for {
    //    fmt.Println("Infinite loop")
    //}
}

Enter fullscreen mode Exit fullscreen mode

Output:


Simple loop iteration: 0
Simple loop iteration: 1
Simple loop iteration: 2
Simple loop iteration: 3
Simple loop iteration: 4
While loop iteration: 0
While loop iteration: 1
While loop iteration: 2
While loop iteration: 3
While loop iteration: 4

Enter fullscreen mode Exit fullscreen mode

3. Switch Statements

Switch statements in Go are used to execute a block of code based on the value of a variable or expression. They provide a cleaner and more readable alternative to long chains of if-else statements.

Image description
Example:


package main

import "fmt"

func main() {
    day := "Tuesday"

    switch day {
    case "Monday":
        fmt.Println("It's Monday!")
    case "Tuesday":
        fmt.Println("It's Tuesday!")
    case "Wednesday":
        fmt.Println("It's Wednesday!")
    case "Thursday":
        fmt.Println("It's Thursday!")
    case "Friday":
        fmt.Println("It's Friday!")
    case "Saturday", "Sunday":
        fmt.Println("It's the weekend!")
    default:
        fmt.Println("Invalid day.")
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:


It's Tuesday!

Enter fullscreen mode Exit fullscreen mode

In this tutorial, we have explored the basic concepts of control flow in Go, including conditional statements, loops, and switch statements. Understanding and utilizing these constructs effectively will enable you to write dynamic and powerful Go programs. In the next tutorial, we will continue to build on these fundamentals and explore more advanced topics in Go programming. Stay tuned!

💖 💪 🙅 🚩
avinashtechlvr
Avinash Chodavarapu

Posted on April 8, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related