- Go 教程
- Go - 主頁
- Go - 概覽
- Go - 環境設定
- Go - 程式結構
- Go - 基本語法
- Go - 資料型別
- Go - 變數
- Go - 常量
- Go - 運算子
- Go - 決策
- Go - 迴圈
- Go - 函式
- Go - 作用域規則
- Go - 字串
- Go - 陣列
- Go - 指標
- Go - 結構體
- Go - 切片
- Go - 範圍
- Go - 字典
- Go - 遞迴
- Go - 型別轉換
- Go - 介面
- Go - 錯誤處理
- Go 有用資源
- Go - 問題與解答
- Go - 快速指南
- Go - 有用資源
- Go - 討論
Go - if 語句
一個 if 語句包括一個布林表示式,後跟一個或多個語句。
語法
一個 if 語句在 Go 程式語言中的語法為 −
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_decision_making.htm
廣告