Go language if...else statement

Go language conditional statement

An optional else statement can be used after an if statement. The expression in the else statement is executed when the boolean expression is false.

Syntax

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

if 布尔表达式 {
   /* 在布尔表达式为 true 时执行 */
} else {
  /* 在布尔表达式为 false 时执行 */
}

If the boolean expression is true, the statement block immediately following it is executed; if it is false, the else statement block is executed.

The flowchart is as follows:

Example

Use if else to determine the size of a number:

Example

package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 100;
 
   /* evaluate the boolean expression */
   if a < 20 {
       /* if the condition is true, execute the following statement */
       fmt.Printf("a is less than 20\n" );
   } else {
       /* if the condition is false, execute the following statement */
       fmt.Printf("a is not less than 20\n" );
   }
   fmt.Printf("The value of a is : %d\n", a);

}

The execution result of the above code is:

a 不小于 20
a 的值为 : 100

Go language conditional statement

other extensions