Vivado% case abcd in a { puts 111} *bc* {puts 333} default {puts xxx} 333
为何有输出333呢?spa
Tcl的foreach语句:code
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}才是正肯定义列表变量的语法)it
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
将语句改成上面的形式,则输出就有四行结果显示了。test
还作了一些实验,发现现象以下:变量
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
根据 上的代码作以下实验:foreach
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的时候都会跳过。command
while循环语句: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 }