NS2 Tcl语言基础知识加深强化

来源:互联网 发布:php面向对象编程 编辑:程序博客网 时间:2024/04/20 03:16

1 if和输入输出

puts -nonewline "Please input a number: "     ;#-nonewline表示不换行

flush stdout      ;#清空输出缓冲区
set x [gets stdin]     ;#把输入的数字赋值给x
if { $x<0 } then {
puts "The input number $x less than 0"
} elseif { $x == 0 } {
puts "The input number $x equal 0"
} else {
puts "The input number $x bigger than 0"

}


2 break

puts -nonewline "Please input a number: "
flush stdout
set x [gets stdin]
set j 0
for { set i 0 } { $i < $x } { incr i } {
if { $i > 10 } {     ;# 特别注意这一点的括号啊 一个也不能少啊,少一个就运行错误 continue也是一样的
continue
}

set j [expr $j + $i]
}
puts $j


3 continue

puts -nonewline "Please input a number: "

flush stdout
set x [gets stdin]
set j 0
for { set i 0 } { $i < $x } { incr i } {
if { $i == 11 } {
continue
}
set j [expr $j + $i]
}
puts $j

0 0
原创粉丝点击