Tcl基础学习(4):Tcl决策语句和循环
决策语句
决策结构需要程序员指定的一个或多个条件进行评估,或由程序进行测试,如果条件被确定为真以及一条或多条语句,任选的其它语句,如果条件被确定为假则被执行。决策语句包含以下五种:
- if语句
- if...else语句
- if语句的嵌套
- switch语句
- switch语句的嵌套
循环语句
Tcl脚本语言中的程序是顺序执行的,而循环语句就是用来让程序中的语句或代码组来进行有限或者无限次的执行。
- while循环
- for循环
- 嵌套循环(while / for)
补充:终止循环的语句
break:当循环中遇到break语句,循环立即终止。
continue:不是强制终止,但是,继续强制循环的下一个迭代发生,跳过中间的代码。
if语句
句式:
if {boolean_expression} {
# statement(s) will execute if the boolean expression is true
}
boolean_expression : 判断条件(布尔表达式)
示例:
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"
>> 10
a is less than 20
value of a is : 10
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
}
boolean_expression : 判断条件(布尔表达式)
示例:
set a 100
#check the boolean condition
if {$a < 20 } {
#if condition is true then print the following
puts "a is less than 20"
} else {
#if condition is false then print the following
puts "a is not less than 20"
}
puts "value of a is : $a"
>> 100
a is not less than 20
value of a is : 100
if语句的嵌套
句式:
if { boolean_expression 1 } {
# Executes when the boolean expression 1 is true
if {boolean_expression 2} {
# Executes when the boolean expression 2 is true
}
}
boolean_expression 1 : 判断条件(布尔表达式1)
boolean_expression 2 : 判断条件(布尔表达式2)
示例:
set a 100
set b 200
# check the boolean condition
if { $a == 100 } {
# if condition is true then check the following
if { $b == 200 } {
#if condition is true then print the following
puts "Value of a is 100 and b is 200"
}
}
puts "Exact value of a is : $a"
puts "Exact value of b is : $b"
>> 100
200
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200
switch...case语句
句式:
#1.在switch后未加“{}”
switch switchingString matchString1 {body1} matchString2 {body2} ... matchStringn {bodyn}
#2.在switch后加“{}”
switch switchingString {
matchString1 {
body1
}
matchString2 {
body2
}
...
matchStringn {
bodyn
}
}
switchingString:用来判断的字符或字符串
matchString:用来比较的字符或字符串
body*:执行的内容
-
在switch语句中使用的 switchingString 通过比较 matchString 使用在不同块之间。
-
在一个switch内可以任何数量 matchString 块。
-
switch语句可以具有可选默认块,其中必须出现在开关的末尾。缺省情况下,可用于执行任务时没有一个case是真实的
示例:
set grade B;
switch $grade {
A {
puts "Well done!"
}
B {
puts "Excellent!"
}
C {
puts "You passed!"
}
F {
puts "Better try again"
}
default {
puts "Invalid grade"
}
}
puts "Your grade is $grade"
>> B
Excellent!
Your grade is B
switch语句的嵌套
句式:
switch switchingString {
matchString1 {
body1
switch switchingString {
matchString1 {
body1
}
matchString2 {
body2
}
...
matchStringn {
bodyn
}
}
}
matchString2 {
body2
}
...
matchStringn {
bodyn
}
}
示例:
set a 100
set b 200
switch $a {
100 {
puts "This is part of outer switch"
switch $b {
200 {
puts "This is part of inner switch!"
}
}
}
}
puts "Exact value of a is : $a"
puts "Exact value of a is : $b"
>> 100
200
This is part of outer switch
This is part of inner switch!
Exact value of a is : 100
Exact value of a is : 200
while循环
句式:
while {condition} {
statement(s)
}
condition:循环条件,为真(非零值)才能跳入while循环
statemen(s):执行的语句/语句块
示例:
set a 3
#while loop execution
while { $a < 6 } {
puts "value of a: $a"
#incr : a = a + 1
incr a
}
>> 3
value of a: 3
value of a: 4
value of a: 5
for循环
句式:
for {initialization} {condition} {increment} {
statement(s);
}
initialization:初始条件
condition:条件经过运算后的判断,为真进入循环体,为假跳过for循环执行后面的语句
increment:增量语句,初始条件递增
示例:
# for loop execution
for { set a 15} {$a < 20} {incr a} {
puts "value of a: $a"
}
>> value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
循环嵌套
句式1:for
for {initialization} {condition} {increment} {
for {initialization} {condition} {increment} {
statement(s);
}
statement(s);
}
句式2:while
while {condition} {
while {condition} {
statement(s);
}
statement(s);
}
示例:for嵌套
set j 0;
for {set i 2} {$i<20} {incr i} {
for {set j 2} {$j <= [expr $i/$j] } {incr j} {
if { [expr $i%$j] == 0 } {
break
}
}
if {$j >[expr $i/$j] } {
puts "$i is prime"
}
}
>> 2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime