linux tcl 循环foreach,Tcl命令操作实验-----(6)---case foreach

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

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值