Go language if statement


Release date:2023-09-09 Update date:2023-12-11 Editor:admin View counts:317

Label:

Go language if statement

if statement consists of a Boolean expression followed by one or more statements.

Grammar

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

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

When If is in a Boolean expression of true, the statement block immediately following is executed, and if it is false, it is not executed.

The flow chart is as follows:

Image0

Example

Use if to determine the size of a numeric variable:

Example

package main
import "fmt"
func main() {
   /* Define local variables */
   var a int = 10

   /* Using an if statement to determine a Boolean expression */
   if a < 20 {
       /* If the condition is true, execute the following statement */
       fmt.Printf("A less than 20\\n" )
   }
   fmt.Printf("The value of a is : %d\\n", a)
}

The result of the above code execution is:

A less than 20
The value of a is: 10

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