Vivado% case abcd in a { puts 111} *bc* {puts 333} default {puts xxx}
333
为什么有输出333呢?
Tcl的foreach语句:
Vivado% set lt {1,2,3,4}
1,2,3,4
Vivado% foreach i $lt {
puts "value=$i"
}
value=1,2,3,4
为什么只输出一行value=1,2,3,4,而不是输出value=1 value=2 value=3 value=4呢?(后来发现列表变量定义有问题:set lt {1 2 3 4}才是正确定义列表变量的语法)
Vivado% foreach i {0 3 2 1} {
switch $i {
1 {puts 111}
2 {puts 222}
3 {puts 333}
default {puts xxx}
}
}
xxx
333
222
111
将语句改为上面的形式,则输出就有四行结果显示了。
还做了一些实验,发现现象如下:
Vivado% foreach i {0,1,2,3} {
puts "value=$i"
}
value=0,1,2,3
Vivado% foreach i $lt {
switch $i {
1 {puts 111}
2 {puts 222}
3 {puts 333}
default {puts xxx}
}
}
xxx
Vivado% foreach i lt {
switch $i {
1 {puts 111}
2 {puts 222}
3 {puts 333}
default {puts xxx}
}
}
xxx
始终不能理解上面的实验结果?(也是列表变量的定义语法有问题 )
根据 上的代码做如下实验:
Vivado% set values {1 3 4 7 2 4 6 8}
1 3 4 7 2 4 6 8
Vivado% foreach x $values {
puts "$x/t [expr {$x**2}]/t [expr {$x**3}]"
}
1/t 1/t 1
3/t 9/t 27
4/t 16/t 64
7/t 49/t 343
2/t 4/t 8
4/t 16/t 64
6/t 36/t 216
8/t 64/t 512
怎么又能够输出多行多个值呢?(也是列表变量定义原因)
break语句:中断循环执行并退出
continue语句:下一循个迭代环
Vivado% foreach x $values {
if {$x==6} {
break
}
if {$x==4} {
continue
}
puts "$x/t [expr {$x**2}]/t [expr {$x**3}]"
}
1/t 1/t 1
3/t 9/t 27
7/t 49/t 343
2/t 4/t 8
上面代码每次遇到4的时候都会跳过。
while循环语句:
Vivado% set i 1
1
Vivado% while {$i != 5} {
puts $i
incr i
}
1
2
3
4
如果按下面的语法书写代码,全部都不正确:
Vivado% while $i != 5 {
puts $i
incr i
}
wrong # args: should be "while test command"
Vivado% while ($i != 5) {
puts $i
incr i
}
wrong # args: should be "while test command"
Vivado% while "$i != 5" {
puts $i
incr i
}