Decision Making in Go lang

decision making statements in go lang

Introduction to decision making

Sometimes we need to introduce a power in our program, that our program should automatically make the decision.

For example:  if the temperature of the day is more than 20 degree then it's hot otherwise it's cool.

To introduce this kind of power in our program, we use decision making statements as the list below...
  1. if-else statements
  2. nested if-else statements
  3. else-if ladder
  4. switch/case-control statement
Let's create a program to check whether a number is even or odd.

Program to check a number is even or odd in Go lang.

package main
import "fmt"
func main() {
var n int32
fmt.Print("Enter any number:")
fmt.Scan(&n)
if n % 2 == 0 {
fmt.Println(n,"is even")
} else {
fmt.Println(n,"is odd")
}
}

Output:

Enter any number:55
55 is odd 

Explanation:

  1. A number is even if it is divisible by 2 otherwise odd.
  2. So to check a number is even or odd, we need to check divisibility by 2.
  3. To perform the divisibility test we use mod (%) operator which returns the remainder. So if a number is divisible by 2 then print odd otherwise odd.

Syntax of if-else statement

if condition {
       statement1;
       statement2;
} else {
       statement1;
       statement2;
}

Explanation:

  1. The block followed by if statement is called if block and executed when the condition evaluates true.
  2. The block followed by else statement is called else block and executed when the condition evaluates false.

Program to find the max of two numbers

package main
import "fmt"
func main() {
var a,b int32
fmt.Print("Enter any two numbers: ")
fmt.Scan(&a,&b)
var max int32
if a > b {
max = a
} else {
max = b
}
fmt.Println("Max = ",max)
}

Output:

Enter any two numbers: 2 25
Max =  25

Program to find the max of 3 numbers

package main

import "fmt"

func main() {
var a,b,c int32
       fmt.Print("Enter any 3 numbers:")
       fmt.Scan(&a,&b,&c)
var max int32
      if a > b  {
         if (a > c) {
max = a
         } else {
max = c
         }
} else {
if b > c {
max = b
} else {
max = c
          }
     }
     fmt.Println("Max = ",max)
}

Explanation

  1. The logic to find the max of 3 number is explained in few lines below...
    1. First, suppose you have only two number a and b.
    2. Now compare a and b, if a > b then a is greater and then compare a > c then finally max is a, but if a is not greater then c the c is the max because c is greater than the greater of a & b. This condition code snippet is written as below.
      1. if a > b  {
               if (a > c) {
                          max = a
                  } else {
                           max = c
                     }
           }
    3. But if a > b is false, then we reject a, then compare b > c, if true then b is max otherwise c is max. This condition code is written as below.
      1. if b > c {
        max = b
        } else {
        max = c
        }

  2. To combine the above case, we use if statement within the if statement as well as in else statement, this is perfectly alright. We can nest if, if-else statement any number of times to create your logic. This is called a nested if-else statement.
  3. We can make the program simpler, by writing the if statement nested to else statement as given in the program below.
package main
import "fmt"
func main() {
var a,b,c int32
       fmt.Print("Enter 3 numbers: ")
        fmt.Scan(&a,&b,&c)
       var max int32
       if a > b && a > c {
max = a
       } else if(b > c) {
max = b
        } else {
max = c
        }
       fmt.Print("Max: ",max)
}
Note: The above program structure is called else if ladder, which allows you to write multiple related conditions in a simpler manner. if any of the condition evaluates true, then the block associated with that is executed or if all condition evaluates false then the else block will be executed.

Switch/Case Control statement

switch/case-control statement is another way to handle multiple conditional related statements. The following example explains the use of switch case statements.

Program to print week-days

package main
import "fmt"
func main() {
var day byte
      fmt.Print("Enter any day, say(0-6): ")
       fmt.Scan(&day)
switch day {
              case 0:
fmt.Println("Sunday")
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
case 4:
fmt.Println("Thrusday")
case 5:
fmt.Println("Friday")
case 6:
fmt.Println("Saturday")
default:
fmt.Println("Invalid input")
}
}

Explanation

  1. The above program wants to print weekday for a number, for example for 0 print Sunday, 1 for Monday and so on...
  2.  To work with the switch, we use a variable with a switch which is called switch variable (a variable which you want to check).
  3. The switch variable is matched with each case constant (a constant which is written with case), and if matched with any case constant then statement of that case block executes and control jumps out of the switch.
  4. If no case constant is matched, then the statements of the default block are executed.
  5. Like other programming languages, no need to use the break statement with each case, because in go lang it is done automatically.
  6. In Go lang, case statement can be written with the condition as given in the programs below...

Program to print days in a month

package main
import "fmt"
func main() {
var m byte
fmt.Print("Enter any month: ")
fmt.Scan(&m)
switch {
case m ==1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12 :
fmt.Println("31 days")
case m == 4 || m == 6 || m == 9 || m == 11 :
fmt.Println("30 days")
case m == 2:
fmt.Println("28 days")
default:
fmt.Println("Invalid input")
}
}
Note:  As shown, the variable is used with the condition, so skipped the from the switch.

Program to say Good morning and Good evening as time

package main
import ("fmt";"time")
func main() {
t := time.now()
switch {
case t < 12 :
fmt.Println("Good Morning")
case t < 17 :
fmt.Println("Good Afternoon")
default:
fmt.Println("Good Evening")
}
}

Comments

Popular posts from this blog

String in golang

Arrays in C Language

Literals in Java

Pointers in C Language

Inline V/S Block Level Element

Reserved Words in Java

Identifiers

Data Types in Go language

Printing in C programming

Variable Naming & Scope of Variable