Go language if statement nesting


Release date:2023-09-13 Update date:2023-10-13 Editor:admin View counts:362

Label:

Go language if statement nesting

You can do it in the if or else if statement to embed one or more if or else if statement.

Grammar

In programming Go language the syntax of the if...else statement is as follows:

If Boolean expression 1 {
   /* Execute when Boolean expression 1 is true */
   If Boolean expression 2 {
      /* Execute when Boolean expression 2 is true */
   }
}

You can nest else if...else statements within the if statement in the same way

Example

Nested use if Statement:

Example

package main
import "fmt"
func main() {
   /* Define local variables */
   var a int = 100
   var b int = 200

   /* Judging conditions */
   if a == 100 {
       /* Execute if the conditional statement is true */
       if b == 200 {
          /* Execute if the conditional statement is true */
          fmt.Printf("The value of a is 100, and the value of b is 200\\n" );
       }
   }
   fmt.Printf("The value of a is : %d\\n", a );
   fmt.Printf("The value of b is: %d\\n", b );
}

The result of the above code execution is:

The value of a is 100, and the value of b is 200
The value of a is: 100
The value of b is: 200

Powered by TorCMS (https://github.com/bukun/TorCMS).