English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String(String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception(Error)

Golang Miscellaneous

Go Language Conditional Statements (if, if...else, nested if)

Decision-making in programming is similar to decision-making in real life. In decision-making, a block of code is executed when certain conditions are met. Sometimes, these are also called control flow statements.GolangUse specified statements to specify the execution flow of the program based on certain conditions. These are used to advance and branch the execution flow based on changes in the program state.

The decision-making statements in Go programming are:

if statement

This is the simplest decision. It is used to decide whether to execute a statement or a block of statements, that is, if a certain condition isTruethen execute the statement block, otherwise do not execute.

if(condition_

if(condition) {
   //The statement to be executed if
   //condition is true
}

Flowchart:

Example:

//Use if statements
package main
import "fmt"
func main() {
    //Take a local variable
    var v int = 700
    //Use if statements
    //Check the condition
    if v < 1000 {
        //Print the following content, the condition evaluation is true
        fmt.Printf("v is less than1000\n")
    }
    fmt.Printf("The value of v is : %d\n", v)
}

Output:

v is less than1000
The value of v is : 700

if…else statement

The if statement tells us that if the condition is true, it will execute the statement block, and if the condition is false, it will not execute. But what if we want to do something else when the condition is false; that's when we use the else statement. When the condition is false, we can use the else statement with the if statement to execute the code block.

if(condition_

 if(condition) {}}
    // If the condition is true, execute this code block
    
}
    // If the condition is false, execute this code block
}

Flowchart:

// Use if...else statements
package main
import "fmt"
func main() {
    //Local variables
    var v int = 1200
    // Use if statements to check the condition
    if v < 1000 {
        //Print the following content if the condition evaluates to true
        fmt.Printf("v < 1000\n")
    }
        //Print the following content if the condition evaluates to false
        fmt.Printf("v > 1000\n")
    }
}

Output:

v > 1000

Nested if statements

In Go language, a nested if is an if statement that is the target of another if or else. A nested if statement is an if statement within an if statement. Yes, Golang allows us to nest if statements within if statements. We can place an if statement inside another if statement.

if(condition_

if(condition1) {
   // When the condition1is true, execute
   
   if(condition2) {
      // When the condition2is true, execute
   }
}

Flowchart:

//Use nested if statements 
package main 
import "fmt"
  
func main() { 
      
   //two local variables
   var v1 int = 400 
   var v2 int = 700 
   
   //Use if statements
   if( v1 == 400) { 
         
      // If the condition is true, execute 
      if( v2 == 700) { 
            
         // If the condition is true, execute the print output
         fmt.Printf("v1the value is400, v2the value is700\n") 
      } 
   } 
    
}

Output:

v1the value is400, v2the value is700

if..else..if statement

Here, the user can choose from multiple options. If statements are executed from top to bottom. Once any condition of the if is true, the associated statements will be executed, and the rest of the if statements will be skipped. If all conditions are not met, the final else statement will be executed.

Important note:

  • can be 0 or1can be 0 to multiple if statements, and must be before else

  • If the else if condition is true, the rest of the else if or else will not be executed

  • Syntax:

if(condition_

else if(condition_1) {
     //condition_1If true, execute the following code block
}2) {
    //condition_2If true, execute the following code block
}
    //If no condition is true, execute the following code block
}

Flowchart:

package main 
import "fmt"
  
func main() { 
      
   var v1 int = 700 
   
   if(v1 == 100) { 
         
      // If the condition is true, then display the following content 
      fmt.Printf("v1the value is100\n") 
        
   }1 == 200) { 
         
      fmt.Printf("v1the value is200\n") 
        
   }1 == 300) { 
         
      fmt.Printf("v1the value is300\n") 
        
   } 
         
      //if none of the conditions are true  
      fmt.Printf("no matching value\n") 
   } 
}

Output:

No matching value