Looping with while
The while
command implements a loop and applies the action as long as the condition remains true, as seen in the for
command example. However, as the while
command provides looping functionality, the action is repeated numerous times, as in the foreach
command.
How to do it…
In the following recipe, we will create a Tcl script, to be called from the command line, that increments the value of x
and prints out the value as in the for
command recipe.
Create a text file named while.tcl
that contains the following commands:
set x 1 while {$x < 11} { puts "x = $x" incr x }
Now invoke the script using the following command line:
tclsh85 while.tcl x = 1 x = 2 x = 3 x = 4 x = 5 x = 6 x = 7 x = 8 x = 9 x = 10
How it works…
The action was invoked a total of 10 times as in the previous example.