Golang Tutorial Index


Like humans, programming requires making decisions, so if one aspect of the program does not satisfy a condition, another part of the code gets executed. Conditional or decision-making statements are programming statements that help execute specific code based on certain conditions. This tutorial will teach you about decision statements and how to implement them in Go programming.



In Go, Decision-making is achieved using conditional statements, which allow a program to make choices and take actions based on different circumstances. There are mainly two types of conditional statements in Go: if statements and switch statements.

if Statement

In Go, an if statement is a control flow statement that allows a program to execute a certain block of code only if a specified condition is true. It has the same general syntax as in many other programming languages:

Syntax:

if condition {
    // This code will be executed if the condition is true.
}

Example:

package main
import "fmt"

func main() {
   grade := "A" // create a variable with a value

   // if statement
   if grade == "A" {
      fmt.Println("excellent")
   }
}

Program Output:

The code above will output "excellent" to the console.

In Go, the if statement is used to execute a block of code based on the value of an expression. The expression is evaluated, and if it is true, the code in the if block is executed. Otherwise, it is skipped.

if-else Statement

It is also possible to include an else clause with the if statement, which specifies a block of code to be executed if the boolean expression evaluates to false.

The syntax of an if statement with an else clause is as follows:

Syntax:

if condition {
    // This code will be executed if the condition is true.
} else {
    // This code will be executed if the condition is false.
}

Example:

package main
import "fmt"

func main() {
   grade := "B" // create a variable with a value

   // if-else statement
   if grade == "A" {
      fmt.Println("excellent")
   } else {
      fmt.Println("not excellent")
   }
}

Program Output:

The code above will output "not excellent" to the console.

In Go, the if-else statement is used to execute a block of code based on the value of an expression. The expression is evaluated, and if it is true, the code in the if block is executed. The code in the else block is executed if the expression is false.

if-else-if Statement

An if-else-if statement in Go allows programmers to specify multiple conditions and execute different code blocks for each. This allows the programmer to have multiple if statements with conditions and one optional else statement at the end. If any of the conditions are not met, the block of the final else statement is automatically executed.

Syntax:

if condition1 {
    // This code will be executed if the condition is true.
} else if condition2 {
    // This code will be executed if condition1 is false and condition2 is true.
} else {
    // This code will be executed if condition1 and condition2 are both false.
}

Example:

package main
import "fmt"

func main() {
   // create a variable with a value
   grade := "B"
   // if-else-if statement
   if grade == "A" {
      fmt.Println("excellent")
   } else if grade == "B" {
      fmt.Println("good")
   } else if grade == "C" {
      fmt.Println("average")
   } else if grade == "D" {
      fmt.Println("below average")
   } else if grade == "F" {
      fmt.Println("fail")
   } else {
      fmt.Println("invalid grade")
   }
}

Program Output:

The code above will output "good" to the console.

In Go, the if-else-if statement is used to execute a block of code based on the value of an expression. The expression is evaluated, and if it is true, the code in the corresponding if block is executed. The code in the else-if block is executed if the expression is false. This process continues until a match is found or the else block is reached. If none of the expressions are true, the code in the else block is executed (if there is an else block).

Switch Statement

A switch statement in Go allows a programmer to specify multiple cases and execute separate code blocks for each case. It is a shorthand way of writing a sequence of if-else.

Syntax:

switch {
case condition1:
    // This code will be executed if the condition is true.
case condition2:
    // This code will be executed if condition1 is false and condition2 is true.
...
}

Here is an example of a switch statement in Go:

Example:

package main
import "fmt"

func main() {
   grade := "B" // create a variable with a value

   // switch statement with multiple cases
   switch grade {
   case "A":
      fmt.Println("excellent")
   case "B":
      fmt.Println("good")
   case "C":
      fmt.Println("average")
   case "D":
      fmt.Println("below average")
   case "F":
      fmt.Println("fail")
   default:
      fmt.Println("invalid grade")
   }
}

Program Output:

The code above will output "good" to the console.

In Go, the switch statement is used to execute a block of code based on the value of an expression. The expression is compared to the value of each case, and if a match is found, the code associated with that case is executed. The code in the default case is executed if no match is found. If there is no default case and no match is found, the switch statement will do nothing.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram