if语句可以跟着一个可选的else语句,else语句块执行时,布尔表达式是假的。
语法
在Tcl语言的if ... else语句的语法是:
if{boolean_expression}{# statement(s) will execute if the boolean expression is true}else{# statement(s) will execute if the boolean expression is false}
如果布尔表达式的值为true,那么if代码块将被执行,否则else块将被执行。
TCL语言使用expr内部命令,因此它不是明确地使用expr语句所需的。
流程图
示例
#!/usr/bin/tclshseta100#check the boolean conditionif{$a<20}{#if condition is true then print the followingputs"a is less than 20"}else{#if condition is false then print the followingputs"a is not less than 20"}puts"value of a is : $a"
当上述代码被编译和执行时,它产生了以下结果:
aisnotless than20;value of ais:100
if...else if...else 语句
if语句可以跟着一个可选的else if ... else语句,使用单个if 测试各种条件if...else if 声明是非常有用的。
当使用if , else if , else语句有几点要记住:
一个if可以有零或一个else,它必须跟在else if之后。
一个if语句可以有零到多个else if,并且它们必须在else之前。
一旦一个 else if 成功,任何剩余else if 或else 不会再被测试。
语法
Tcl语言的 if...else if...else语句的语法是:
if{boolean_expression1}{# Executes when the boolean expression 1 is true}elseif{boolean_expression2}{# Executes when the boolean expression 2 is true}elseif{boolean_expression3}{# Executes when the boolean expression 3 is true}else{# executes when the none of the above condition is true}
示例
#!/usr/bin/tclshseta100#check the boolean conditionif{$a==10}{# if condition is true then print the followingputs"Value of a is 10"}elseif{$a==20}{# if else if condition is trueputs"Value of a is 20"}elseif{$a==30}{# if else if condition is trueputs"Value of a is 30"}else{# if none of the conditions is trueputs"None of the values is matching"}puts"Exact value of a is: $a"
当上述代码被编译和执行时,它产生了以下结果:
None of the values is matching
Exact value of a is: 100
¥ 我要打赏
纠错/补充
收藏
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。