Go if...else Statement
Go Language Conditional Statements
An optional else statement can follow an if statement; the expression in the else statement executes when the boolean expression is false.
Syntax
The syntax of the if...else statement in the Go programming language is as follows:
if 布尔表达式 {
/* execute when the boolean expression is true */
} else {
/* execute when boolean expression is false */
}
If the boolean expression is true, the block immediately following it executes; if it is false, the else block executes.
The flowchart is as follows:
Example
Use if else to judge the magnitude of a number:
Example
package
main
import
"fmt"
func
main
()
{
/* Local variable definition */
var
a
int
=
100
;
/* Judge the boolean expression */
if
a <
20
{
/* If the condition is true, execute the following statements */
fmt
.
Printf
(
"a is less than 20\n"
);
}
else
{
/* If the condition is false, execute the following statements */
fmt
.
Printf
(
"a is not less than 20\n"
);
}
fmt
.
Printf
(
"The value of a is : %d\n"
,
a
);
}
The execution result of the code above is:
a 不小于 20
a 的值为 : 100