Decision Control in Golang5 min read

What is Decision Control in Golang?

Decision Control in Golang requires the program to evaluate or test one or more conditions, as well as a statement or statements to be executed if the condition is true, and optionally, other statements to be executed if the condition is false. It is similar to making decisions in real life. For example, if marks are greater than 90%, pursue Btech, else, pursue BCA. Control flow statements are another name for these statements. Control statements in Golang are used to control the flow of a program’s execution based on particular conditions.

Golang includes the below decision control statements,

Types of Decision Control statements in Golang

1. if Statement

This is the most basic statement decision control statement used in programming. It is used to test the condition. It executes the if block if the condition is true otherwise not.

Syntax:

if(condition) {
   // Statements to execute if
   // the condition is true
}

Flowchart:

if statement decision control

Example:

// Go program to illustrate the
// use of if statement
package main

import "fmt"

func main() {
	
var h int = 50

// using if statement for
// checking the condition
if(h > 20) {
		
	// print the following if
	// the condition evaluates to true
	fmt.Printf("h is greater than 20\n")
}
	
fmt.Printf("Value of h = %d\n", h)
	
}

Output:

C:\Users\Hiberstack\GoPrograms>go run IfDemo.go
h is greater than 20
Value of h = 50

2. if-else statement

The if-else decision control statement includes an if block and an else block. As the name suggests, the group of statements will be executed in the if block if the condition is true and the group of statements will be executed in the else block if the condition is false.

Syntax:

if (condition) {
  // Executes this block if
  // the condition is true
} else {
  // Executes this block if
  // the condition is false
}

Flowchart:

if-else statement decision control

Example:

// Go program to illustrate the
// use of if-else statement
package main

import "fmt"

func main() {

var h int = 50
  // using if statement for
  // checking the condition
  if(h > 100) {
    // print the following if
    // the condition evaluates to true
	fmt.Printf("h is greater than 100\n")
  }else{
    fmt.Printf("h is less than 100\n")
  }
  fmt.Printf("Value of h = %d\n", h)
}

Output:

C:\Users\Hiberstack\GoPrograms>go run IfElseDemo.go
h is less than 100
Value of h = 50

3. if-else-if ladder

Here, there are multiple if-else blocks that execute from top to bottom. If any of the conditions from any of the if block is true, that block will be executed and the rest of the blocks will be skipped. If none of the conditions are satisfied, the last else block will be executed.

Syntax:

if(condition1) {
  // this block will be executed if condition1 is true
} else if(condition2) {
  // this block will be executed if condition2 is true
}
.
.
. else {
  // this block will be executed when none of the conditions are true
}

Flowchart:

if-else-if ladder statement decision control

Example:

// Go program to illustrate the
// use of if..else..if ladder
package main
import "fmt"

func main() {
  // taking a local variable
  var h int = 100

  // checking the condition
  if(h == 10) {
    // if condition is true then
    // display the following */
    fmt.Printf("Value of h is 10\n")
  } else if(h == 200) {
    fmt.Printf("Value of h is 200\n")
  } else if(h == 300) {
    fmt.Printf("Value of h is 300\n")
  } else {
    // if none of the conditions is true
    fmt.Printf("None of the values match\n")
  }
}

Output:

C:\Users\Hiberstack\GoPrograms>go run IfElseIfDemo.go
None of the values match

4. Nested if statement

Writing an if statement inside another if statement is referred to as a nested if statement. If we have multiple conditions to evaluate, we use a nested if statement. There is no limit for nesting the if statement.

Syntax:

if (condition1) {
  // Executes when condition1 is true
  if (condition2) {
    // Executes when condition2 is true
    if (condition3) {
      // Executes when condition3 is true
    }
  }
}

Flowchart:

nested-if statement decision control

Example:

// Go program to illustrate the
// use of nested if statement
package main
import "fmt"

func main() {
  // taking two local variable
  var h1, h2 int = 5, 20

  // using if statement
  if( h1 < 10 ) {

    // if condition is true then
    // check the following
    if( h2 > 10 ) {
      // if condition is true
      // then display the following
      fmt.Println("Value of h1 is", h1, "and h2 is", h2);
    }
  }
}

Output:

C:\Users\Hiberstack\GoPrograms>go run NestedIfDemo.go
Value of h1 is 5 and h2 is 20

Share:

Leave a Reply