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
广告
© . All rights reserved.