Go language if…else statement


Release date:2023-09-13 Update date:2023-10-13 Editor:admin View counts:348

Label:

Go language if…else statement

if statement, you can use the optional else statement, else expression in the statement is the Boolean expression false when it is executed.

Grammar

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

If Boolean expression {
   /* Execute when Boolean expression is true */
} else {
  /* Execute when Boolean expression is false */
}

When If is in a Boolean expression of true, the block of statements immediately following the execution of the false then execute ``else``sentence block.

The flow chart is as follows:

Image0

Example

Use if else to judge the size of a number:

Example

package main
import "fmt"
func main() {
   /* Definition of Local Variables */
   var a int = 100;

   /* Judging Boolean Expressions */
   if a < 20 {
       /* If the condition is true, execute the following statement */
       fmt.Printf("A less than 20\\n" );
   } else {
       /* If the condition is false, execute the following statement */
       fmt.Printf("A not less than 20\\n" );
   }
   fmt.Printf("The value of a is: %d\\n", a);
}

The result of the above code execution is:

A not less than 20
The value of a is: 100

Powered by TorCMS (https://github.com/bukun/TorCMS).