Golang Control Flow – If-Else, Switch Statements Read it later

Rate this post

When it comes to programming, one of the most essential concepts is control flow. In Golang, control flow is used to determine the order in which a program executes its statements. If-else statements and switch statements are two of the most common control flow statements used in Golang. In this article, we will discuss these statements in detail and how they can be used effectively.

Golang If-else Statements

If-else statements are used to execute a block of code based on a condition. The syntax of if-else statement in Golang is as follows:

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

Here is an example that shows how if-else statements can be used in Golang:

package main

import "fmt"

func main() {
    var age int
    fmt.Println("Enter your age:")
    fmt.Scanln(&age)

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

In the above example, the user’s age is taken as input and if the age is less than 18, the program will print “You are a minor”. Otherwise, it will print “You are an adult”.

Golang Multiple If-else Statements

Multiple if-else statements can be used to check for multiple conditions. The syntax is as follows:

if condition1 {
  // code to be executed if condition1 is true
} else if condition2 {
  // code to be executed if condition1 is false and condition2 is true
} else {
  // code to be executed if both condition1 and condition2 are false
}

Here is an example that shows how multiple if-else statements can be used in Golang:

package main

import "fmt"

func main() {
    var age int
    fmt.Println("Enter your age:")
    fmt.Scanln(&age)

    if age < 18 {
        fmt.Println("You are a minor")
    } else if age < 60 {
        fmt.Println("You are an adult")
    } else {
        fmt.Println("You are a senior citizen")
    }
}

In the above example, if the age is less than 18, the program will print “You are a minor”. If the age is between 18 and 60, it will print “You are an adult”. Otherwise, it will print “You are a senior citizen”.

Go Switch Statements

Switch statements are used to execute different actions based on different conditions. The syntax of switch statement in Golang is as follows:

switch expression {
case condition1:
  // code to be executed if expression matches condition1
case condition2:
  // code to be executed if expression matches condition2
default:
  // code to be executed if expression doesn't match any of the conditions
}

Here is an example that shows how switch statements can be used in Golang:

package main

import "fmt"

func main() {
    var grade string
    fmt.Println("Enter your grade:")
    fmt.Scanln(&grade)

    switch grade {
    case "A":
        fmt.Println("Excellent")
    case "B":
        fmt.Println("Good")
    case "C":
        fmt.Println("Average")
    case "D":
        fmt.Println("Below Average")
    default:
        fmt.Println("Fail")
    }
}

In the above example, the user’s grade is taken as input and the program will print “Excellent” if the grade is “A”, “Good” if the grade is “B”, “Average” if the grade is “C”, “Below Average” if the grade is “D”, and “Fail” for any other grade.

Fallthrough Statement in Go

In Golang, fallthrough statement can be used to transfer control to the next case in a switch statement. It is used when we want to execute the code of a specific case and the next case as well. The syntax of fallthrough statement is as follows:

switch expression {
case condition1:
  // code to be executed if expression matches condition1
  fallthrough
case condition2:
  // code to be executed if expression matches condition2 or condition1
}

Here is an example that shows how fallthrough statement can be used in Golang:

package main

import "fmt"

func main() {
    var grade string
    fmt.Println("Enter your grade:")
    fmt.Scanln(&grade)

    switch grade {
    case "A":
        fmt.Println("Excellent")
        fallthrough
    case "B":
        fmt.Println("Good")
        fallthrough
    case "C":
        fmt.Println("Average")
        fallthrough
    case "D":
        fmt.Println("Below Average")
        fallthrough
    default:
        fmt.Println("Fail")
    }
}

In the above example, if the user’s grade is “A”, the program will print “Excellent”, “Good”, “Average”, and “Below Average”. Similarly, if the user’s grade is “B”, the program will print “Good”, “Average”, and “Below Average”. And so on.

Was This Article Helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *