Navigation Logo 2.4  While Loop Navigation Logo

 

 

As mentioned above, the while statement is actually a command that takes two arguments:

 
while CONDITION BODY
BODY contains a sequence of command lines to be executed repeatedly and CONDITION is the expression that tests whether the repetition is to happen again.

Expressions are explained below in more detail in Expressions. For now note that expressions can compare numbers for equality and inequality, for example,

$X == 1
$A != $B
These comparisons produce 1 if true and 0 if false.

Any expression that is passed to the while loop's CONDITION argument must evaluate to a number. The result is considered to be true if the number is nonzero and false if it is 0.

Here's a simple while loop that iterates just once doing nothing useful.

set Condition 1
while {$Condition} {set Condition 0}
If you are remembering how variable substitution works, you may think that the first argument to the while procedure is really 1 and so the loop will run forever even though the variable Condition changes. The reason why that is not so is explained in the next section. For now, just remember to use curly brackets for both of while's arguments.
 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.