Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
if Statement
2.1.
Syntax
2.2.
Flow Chart
2.3.
Example
3.
if-else Statement
3.1.
Syntax
3.2.
Flow Chart
3.3.
Example
4.
Nested if Statement
4.1.
Syntax
4.2.
Flow Chart
4.3.
Example
5.
if-else-if Ladder
5.1.
Syntax
5.2.
Flow Chart
5.3.
Example
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Go Decision Making

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this blog, we will be covering the go decision making, which includes simple if, if-else, nested if. However, moving to the topic, we need to brush up few concepts before moving to the actual topics. Like What is a Decision Making?. It is a type of programming similar to making decisions in real life. Go lang uses control statements to control the program's execution flow based on certain conditions.

if Statement

This is the most simple decision-making statement. This Statement is used to decide whether a specific message or block of the Statement will execute or not. If a specific condition is true, a block of code will execute.

Syntax

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

Flow Chart

Example

package main
 
import "fmt"
 
func main() {
     
   
   var v int = 500
   
      if v < 1000 {
         
     
      fmt.Printf("v is less than 1000\n")
   }
     
   fmt.Printf("Value of v is: %d\n", v)
     
}

Output

v is less than 1000
Value of v is: 500

In this example, we have checked the simple if Statement against some input and just printed the output.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

if-else Statement

This if-else Statement tells us that if a condition is true, it will execute the if code block otherwise, it will execute the else statement block. We can implement the else Statement with if Statement to execute a code block of when the condition is false.

Syntax

if condition {

//Execute this code block if the condition is true
} else {


//Execute this code block if the condition is false
}

Flow Chart

Example

package main
 
import "fmt"
 
func main() {
     
 
   var v int = 1200
   
 
   if v < 1000 {
         
     
      fmt.Printf("v is less than 1000\n")
       
   } else {
         
     
      fmt.Printf("v is greater than 1000\n")
   }
     
}

Output

v is greater than 1000

In this example, we have used the if-else Statement. Here we first checked the if Statement and then else the Statement afterward. After doing this, we just printed the output.

Nested if Statement

In Go, language, a nested if Statement is the target of another. They were nested if statements mean if Statement is within an if Statement. 

Syntax


if condition1 {
// Executes when the condition1 is true 
if condition2 {
// Executes when condition2 is true
}
}

Flow Chart

Example

package main
import "fmt"
 
func main() {
     
   var v1 int = 400
   var v2 int = 700
   
 
   if( v1 == 400 ) {
         
     
      if( v2 == 700 ) {
           
        fmt.Printf(" The Value of v1 is 400 and v2 is 700\n" );
      }
   }
   }

 Output

The value of v1 is 400 and v2 is 700

In this example, we checked two if statements one after another. If the value of the first one is correct, it will only move to the next block; otherwise, it will terminate after we print the output. 

if-else-if Ladder

When a user has more than one option of choosing if-else, then if-else statements are executed from the top to down, making it a ladder. As soon as the first condition becomes true, the below Statement has bypassed a ladder. If no need is verified, the control will move to the other block.

Syntax

if condition1 {
// this block will execute if the condition1 is true
}
else if condition2 {
// this block will execute if the condition2 is true
}
.
.
.
else{
// this block will execute if none of the conditions are true
}

Flow Chart

Example

package main
import "fmt" 
func main() {     
 
   var v1 int = 700  
 
   if v1 == 100 {        
     
      fmt.Printf("Value of v1 is 100\n")
       
   } else if v1 == 200 {
         
      fmt.Printf("Value of a is 20\n")
       
   } else if v1 == 300 {
         
      fmt.Printf("Value of a is 300\n")
       
   } else {
             fmt.Printf("None of the values is matching\n")
   }
}

Output

None of the values is matching

In this example, we have used the if-else if ladder to check specific values. If the values get verified against the condition, we have printed the output. 

FAQs

  1. What is the decision-making in Golang?
    There are four decision-making statements in Golang: if Statement, if-else Statement, nested if Statement, and if-else-if ladder statement.
     
  2. What is the decision-making structure in the Go language?
    The decision-making structure requires that the programmer should specify one or more conditions to be checked by the program, along with a statement or two.
     
  3. What is the expression of the if-else Statement in Go lang?
    The syntax of an if-else statement in Go lang is - if the boolean expression is checked to be true, then the block of code will be executed. Otherwise, the else block will execute.

Key Takeaways

In this article, we have extensively discussed Go Decision Making and its implementation in GoLang. We briefly introduced what decision-making is. We also discussed different decision-making types, including simple if, if-else, nested if, and if-else if ladder. We also discussed their flow chart and their syntax and example.

We hope that this blog has helped you enhance your knowledge regarding Go Decision Making and if you would like to learn more, check out our articles on Go loop control statement.

Don’t stop here, explore Golang with Golang Archives from coding ninjas. Do upvote our blog to help other ninjas grow. Happy Coding!”

Previous article
Go Type Casting
Next article
Go Loops
Live masterclass