Forgotten your password?

Forum Index > Tcl/Tk > For loop
Author Message
EXAMPLES

Print a line for each of the integers from 0 to 10:
1
2
3
for {set x 0} {$x<10} {incr x} {
   puts "x is $x"
}

Either loop infinitely or not at all because the expression being evaluated is actually the constant, or even generate an error! The actual behaviour will depend on whether the variable x exists before the for command is run and whether its value is a value that is less than or greater than/equal to ten, and this is because the expression will be substituted before the for command is executed.

1
2
3
for {set x 0} $x<10 {incr x} {
   puts "x is $x"
}

Print out the powers of two from 1 to 1024:
1
2
3
for {set x 1} {$x<=1024} {set x [expr {$x * 2}]} {
   puts "x is $x"
}
nice! keep on going :luigi:
Forum Index > Tcl/Tk > For loop