Go Language if...else Statement
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
Syntax
The syntax for an if...else statement in Go programming language is as follows:
if booleanExpression {
/* Executed when the boolean expression is true */
} else {
/* Executed when the boolean expression is false */
}
If the boolean expression evaluates to true, the block of statements immediately following it is executed. If it is false, the block of statements following the else statement is executed.
Flowchart
Example
Using if else to determine the size of a number:
Example
package main
import "fmt"
func main() {
/* Local variable definition */
var a int = 100;
/* Check the boolean condition */
if a < 20 {
/* If condition is true then print the following */
fmt.Printf("a is less than 20\n" );
} else {
/* If condition is false then print the following */
fmt.Printf("a is not less than 20\n" );
}
fmt.Printf("The value of a is : %d\n", a);
}
The output of the above code is:
a is not less than 20
The value of a is : 100