Conditional Statements in Go



In this tutorial, we will learn about conditional statements in Go. We will cover the basics of using if, if-else, and switch statements to control the flow of a program based on conditions.


Understanding Conditional Statements in Go

Conditional statements are used to perform different actions based on different conditions. Go supports several types of conditional statements: if, if-else, and switch.


If Statement

An if statement in Go is used to execute a block of code only if a specified condition is true.

if condition {
    // statements
}

If-Else Statement

An if-else statement in Go is used to execute one block of code if the condition is true, and another block of code if the condition is false.

if condition {
    // statements
} else {
    // statements
}

If-Else If-Else Statement

An if-else if-else statement in Go is used to test multiple conditions and execute corresponding blocks of code.

if condition1 {
    // statements
} else if condition2 {
    // statements
} else {
    // statements
}

Switch Statement

A switch statement in Go is used to execute one block of code among many options. It is an alternative to using multiple if-else if statements.

switch expression {
case value1:
    // statements
case value2:
    // statements
default:
    // statements
}


Example 1: Using If Statement

We can use an if statement in Go to execute a block of code only if a specified condition is true.

For example,

  1. Create a variable named age and assign it a value.
  2. Use an if statement to check if the value of age is greater than or equal to 18.
  3. If the condition is true, print a message to the console.

Go Program

package main
import "fmt"
func main() {
    age := 20
    if age >= 18 {
        fmt.Println("You are an adult.")
    }
}

Output

You are an adult.


Example 2: Using If-Else Statement

We can use an if-else statement in Go to execute one block of code if a condition is true, and another block of code if the condition is false.

For example,

  1. Create a variable named num and assign it a value.
  2. Use an if-else statement to check if the value of num is even or odd.
  3. Print a corresponding message to the console.

Go Program

package main
import "fmt"
func main() {
    num := 7
    if num%2 == 0 {
        fmt.Println("The number is even.")
    } else {
        fmt.Println("The number is odd.")
    }
}

Output

The number is odd.


Example 3: Using If-Else If-Else Statement

We can use an if-else if-else statement in Go to test multiple conditions and execute corresponding blocks of code.

For example,

  1. Create a variable named score and assign it a value.
  2. Use an if-else if-else statement to check the value of score and print a grade based on the value.

Go Program

package main
import "fmt"
func main() {
    score := 85
    if score >= 90 {
        fmt.Println("Grade: A")
    } else if score >= 80 {
        fmt.Println("Grade: B")
    } else if score >= 70 {
        fmt.Println("Grade: C")
    } else if score >= 60 {
        fmt.Println("Grade: D")
    } else {
        fmt.Println("Grade: F")
    }
}

Output

Grade: B


Example 4: Using Switch Statement

We can use a switch statement in Go to execute one block of code among many options.

For example,

  1. Create a variable named day and assign it a value.
  2. Use a switch statement to print the name of the day based on its value.

Go Program

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")
    default:
        fmt.Println("Invalid day")
    }
}

Output

Wednesday