Go - if 語句



一個 if 語句包括一個布林表示式,後跟一個或多個語句。

語法

一個 if 語句在 Go 程式語言中的語法為 −

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

如果布林表示式被評估為 true,則將執行 if 語句中的程式碼塊。如果布林表示式被評估為 false,則將執行 if 語句結束後的第一組程式碼(在結束大括號後)。

流程圖

Go if statement

示例

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_decision_making.htm
廣告
© . All rights reserved.