go-tutorial

Go Decision Making

The programmer must define one or more conditions to be evaluated or tested by the programme, as well as a statement or statements to be executed if the condition is found to be true, and optionally, further statements to be executed if the condition is found to be false.

The general form of a typical decision-making framework present in most programming languages is as follows:

The decision-making statements in the Go programming language are as follows.

  1. if statement: A boolean expression is followed by one or more statements in an if statement.
  2. if…else statement: When the boolean expression is false, an if statement might be followed by an optional else statement.
  3. nested if statements: One if or else if statement can be used inside another if or else if statement (s).
  4. switch statement: A switch statement allows a variable to be compared against a list of values for equality.

if statement

if is a statement that has a boolean condition and it executes a block of code if that condition evaluates to true. It executes an alternate else block if the condition evaluates to false. In this tutorial, we will look at the various syntaxes and ways of using if statement.

If statement syntax

The syntax of the if statement is provided below

if condition { 
...
}

If the condition is true, the lines of code between the braces { and } is executed.

Unlike in other languages like C, the braces { } are mandatory even if there is only one line of code between the braces{  }.

Example

Let’s write a simple program to find out whether a number is even or odd.

package main
import ("fmt" )
func main() { 
num := 10 
if num%2 == 0 { 
   //checks if number is even 
   fmt.Println("The number", num, "is even") 
   return 
}
  fmt.Println("The number", num, "is odd")
}

In the above program, the condition num%2 in line no. 9 finds whether the remainder of dividing num by 2 is zero or not. Since it is 0 in this case, the text The number 10 is even is printed and the program returns.

If else statement

The if statement has an optional else construct which will be executed if the condition in the if statement evaluates to false.

if condition { 
....
} else {
...
}

In the above snippet, if condition evaluates to false, then the lines of code between else { and } will be executed.

Let’s rewrite the program to find whether the number is odd or even using if else statement.

import ("fmt" )
func main() { 
num := 11 
if num%2 == 0 {
   //checks if number is even 
   fmt.Println("the number", num, "is even")
} else { 
   fmt.Println("the number", num, "is odd") }
}

In the above code, instead of returning if the condition is true as we did in the previous section, we create an else statement that will be executed if the condition is false. In this case, since 11 is odd, the if condition is false and the lines of code within the else statement is executed. The above program will print.

the number 11 is odd

Nested if statement

The if statement also has optional else if and else components. The syntax for the same is provided below

if condition1 { 
   ...
} else if condition2 {
   ... 
} else { 
  ...
}

The condition is evaluated for the truth from the top to bottom.

In the above statement if condition1 is true, then the lines of code within if condition1 { and the closing brace } are executed.

If condition1 is false and condition2 is true, then the lines of code within else if condition2 { and the next closing brace } is executed.

If both condition1 and condition2 are false, then the lines of code in the else statement between else { and } are executed.

There can be any number of else if statements.

In general, whichever if or else if’s condition evaluates to true, it’s corresponding code block is executed. If none of the conditions are true then else block is executed.

Let’s write a program that uses else if.

package main
import ("fmt" )
func main() {
num := 99
 if num <= 50 { 
    fmt.Println(num, "is less than or equal to 50")
  } else if num >= 51 && num <= 100 {
    fmt.Println(num, "is between 51 and 100")
 } else { 
    fmt.Println(num, "is greater than 100") 
 }
}

In the above program, the condition else if num >= 51 && num <= 100 in line no. 11 is true

and hence the program will print

99 is between 51 and 100

Switch Statement

A switch is a conditional statement that evaluates an expression and compares it against a list of possible matches and executes the corresponding block of code. It can be considered as an idiomatic way of replacing complex if else clauses.

Example

An example program is worth a hundred words. Let’s start with a simple example which will take a finger number as input and outputs the name of that finger 🙂 . For example, 1 is thumb,

2 is index, and so on.

package main
import ("fmt" )
func main() { 
 finger := 4
  fmt.Printf("Finger %d is ", finger) 
 switch finger {
 case 1:
         fmt.Println("Thumb")
 case 2: 
         fmt.Println("Index")
 case 3: 
        fmt.Println("Middle")
 case 4: 
        fmt.Println("Ring")
 case 5: 
         fmt.Println("Pinky")
  }
}

In the above program switch finger in line no. 10, compares the value of finger with each of the case statements. The cases are evaluated from top to bottom and the first case which

matches the expression is executed. In this case, finger has a value of 4 and hence

Finger 4 is Ring is printed.

RECOMMENDED ARTICLES





Leave a Reply

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