Go Programming: Control Flow

Introduction to Control Flow

Control flow refers to the order in which individual statements, instructions, or function calls are executed or evaluated. In Go, the primary control flow mechanisms are the if statement, else statement, and switch statement. These allow you to control the flow of execution based on different conditions.

If Statement

The if statement allows you to execute a block of code only if a specified condition is true. It’s the most basic form of control flow in Go.

Syntax:

if condition {
    // block of code to be executed if the condition is true
}

Here, if the condition evaluates to true, the block of code inside the if statement will execute.

Example:

package main

import "fmt"

func main() {
    age := 20

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

Explanation: In this example, the if statement checks if the age is greater than or equal to 18. If the condition is true, it prints "You are an adult!" to the console.

Else Statement

The else statement allows you to execute a block of code if the if condition evaluates to false.

Syntax:

if condition {
    // block of code to be executed if the condition is true
} else {
    // block of code to be executed if the condition is false
}

Here, if the condition is false, the block inside the else statement will execute.

Example:

package main

import "fmt"

func main() {
    age := 16

    if age >= 18 {
        fmt.Println("You are an adult!")
    } else {
        fmt.Println("You are a minor.")
    }
}

Explanation: In this case, since the condition age >= 18 is false, the else block is executed, and it prints "You are a minor."

Else If Statement

Go also allows you to check multiple conditions using the else if statement, which is used when you have multiple conditions to check sequentially.

Syntax:

if condition1 {
    // block of code to be executed if condition1 is true
} else if condition2 {
    // block of code to be executed if condition2 is true
} else {
    // block of code to be executed if none of the conditions are true
}

This allows you to chain multiple conditions together and handle each case separately.

Example:

package main

import "fmt"

func main() {
    age := 22

    if age >= 18 && age < 21 {
        fmt.Println("You are an adult but not yet 21.")
    } else if age >= 21 {
        fmt.Println("You are 21 or older.")
    } else {
        fmt.Println("You are a minor.")
    }
}

Explanation: Here, the program checks multiple conditions: first, if the person is an adult but not yet 21; second, if the person is 21 or older; and if neither condition is true, it prints that the person is a minor.

Switch Statement

Go also provides the switch statement, which is used to simplify multiple if-else conditions. It evaluates an expression once and compares it against multiple possible values. The first match will execute the corresponding block of code.

Syntax:

switch expression {
case value1:
    // block of code to be executed if expression equals value1
case value2:
    // block of code to be executed if expression equals value2
default:
    // block of code to be executed if no cases match
}

In the switch statement, Go evaluates the expression and compares it against the values listed in each case.

Example:

package main

import "fmt"

func main() {
    day := 3

    switch day {
    case 1:
        fmt.Println("Monday")
    case 2:
        fmt.Println("Tuesday")
    case 3:
        fmt.Println("Wednesday")
    case 4:
        fmt.Println("Thursday")
    case 5:
        fmt.Println("Friday")
    case 6:
        fmt.Println("Saturday")
    case 7:
        fmt.Println("Sunday")
    default:
        fmt.Println("Invalid day")
    }
}

Explanation: The switch statement checks the value of day and matches it with the correct day of the week. If no match is found, the default case is executed.

Summary

  • If: Executes a block of code if the specified condition is true.
  • Else: Executes a block of code if the if condition is false.
  • Else If: Allows checking multiple conditions sequentially.
  • Switch: Provides an easier way to compare a value against multiple possible matches, avoiding multiple if-else statements.