Go語言if語句

if語句由布爾運算式後跟一個或多個語句組成。

語法

Go編程語言中if語句的語法是:

if(boolean_expression)
{
   /* statement(s) will execute if the boolean expression is true */
}

如果布爾運算式的計算結果為true,那麼執行if語句中的代碼塊。如果布爾運算式的計算結果為false,則將執行在if語句結束後(在關閉大括弧之後)的第一組代碼。

流程圖

示例

package main

import "fmt"

func main() {
   /* local variable definition */
   var a int = 10

   /* check the boolean condition using if statement */
   if( a < 20 ) {
       /* if condition is true then print the following */
       fmt.Printf("a is less than 20\n" )
   }
   fmt.Printf("value of a is : %d\n", a)
}

當上述代碼被編譯和執行時,它產生以下結果:

a is less than 20;
value of a is : 10

上一篇: Go語言條件和決策 下一篇: Go語言迴圈