TCL if語句

if語句包含一個布爾運算式後跟一個或多個語句。

語法

Tcl語言的if語句的語法是:

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

如果代碼裏布爾運算式的值為真,那麼if語句塊將被執行。如果 if 語句的結束(右大括弧後)布爾運算式的值為false,那麼第一套代碼會被執行。

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決策 下一篇: Tcl if...else語句