Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Go Language Nested if Statements

Go Language Nested if Statements

Go Language Conditional Statements Go Language Conditional Statements

You can embed one or more if or else if statements within an if or else if statement.

Syntax

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



if 布尔表达式 1 {

   /* Execute when boolean expression 1 is true */

   if 布尔表达式 2 {

      /* Execute when boolean expression 2 is true */

   }

}

You can nest in an if statement in the same way else if...else Statement

Example

Using nested if statements:

Example

package

main

import

"fmt"

func

main

()

{

   

/* Define local variables */

   

var

a

int

=

100

   

var

b

int

=

200

 

   

/* Judgment condition */

   

if

a

==

100

{

       

/* Executed when the if condition is true */

       

if

b

==

200

{

         

/* Executed when the if condition is true */

          fmt

.

Printf

(

"The value of a is 100, and the value of b is 200\n"

);

       

}

   

}

   fmt

.

Printf

(

"a value: %d\n"

,

a

);

   fmt

.

Printf

(

"b value: %d\n"

,

b

);

}

The execution result of the code above is:



a 的值为 100 , b 的值为 200

a 值为 : 100

b 值为 : 200

Go Language Conditional Statements Go Language Conditional Statements