WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - if...else statement


The optional else statement can be used after the

if statement, and 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 Boolean expression {< Span class="pln">
    /* Execute when the Boolean expression is true */
} else {
   /* Execute when the Boolean expression is false */
}

If when the Boolean expression is true, the next statement block is executed, and if false, the else statement block is executed.

The flow chart is as follows:

if-else-statement

Instance

Use if else to determine the size of a number:

Instance

package main

import "fmt"

func main() {
   /* Local variable definition */
   var a int = 100;
 
   /* Judged Boolean expression */
   if a < 20 {
       /* If the condition is true Then execute the following statement */
       fmt.Printf("a Less than 20\n" );
   } else {
       /* If the condition is false Then execute the following statement */
       fmt.Printf("a not less than 20\n" );
   }
   fmt.Printf("a Value : %d\n", a);

}

The above code execution result is:

a not less than 20
a Value : 100