Tcl - If 語句



if 語句由布林表示式後跟一條或多條語句組成。

語法

Tcl 語言中“if”語句的語法如下 −

if {boolean_expression} {
   # statement(s) will execute if the Boolean expression is true
}

如果布林表示式計算結果為true,則if 語句內的程式碼塊將被執行。如果布林表示式計算結果為false,則“if”語句結尾處(在結束花括號後面)的第一組程式碼將被執行。

Tcl 語言在內部使用expr 命令,因此我們無需顯式使用expr 語句。

流程圖

If Statement

示例

#!/usr/bin/tclsh

set a 10
 
#check the boolean condition using if statement
if { $a < 20 } {
   # if condition is true then print the following 
   puts "a is less than 20" 
}
puts "value of a is : $a" 

編譯並執行以上程式碼後,將產生以下結果 −

a is less than 20
value of a is : 10
tcl_decisions.htm
廣告