Control Statements


Conditional Statements: if, else, and elseif

Below are valid structures for if control structures:

if {$i == "help"} {echo Sorry, no help is on-line}
if {$a > 10} { 
         set a 10 
         }
if {$a > 20} { 
         set a 20 
         } elseif {$a < 0} { 
                 set a 0 
         } else { 
                 $a = $a + $b 
         }
It is also important to note (as in the examples above) that, when variables are compared with strings, the strings are quoted, since this is how TCL distinguishes strings from invalid numeric constants. Note also that elseif is always one word.

Although BTCL does not generally pay attention to how your script is structured, you should remember that each command line is terminated by a carriage return. In the last example an open brace ({ ) is placed after the if test to escape the carriage return. The following example lacks this brace and would produce an error, since TCL does not find a second argument for the if command:

if {$a > 10} 
         { 
         set a 10 
         }

Conditional Looping Statements: while, for, and foreach

The BTCL while command takes two arguments, the second argument (the body) being evaluated repeatedly so long as the first argument (the test) evaluates to 1. The following example script prints all even numbers between 0 and 24:

$i = 0 
 while   {$i < 25} { 
         echo $i 
         $i = $i+2 
         }
The BTCL for command is similar to the while command except that it takes two extra arguments: one that is evaluated before the test and body arguments and one that is evaluated after the body and before the test arguments. By using the for command, the previous example can be written more concisely:

for {$i = 0} {$i < 25} {incr i 2} { 
                 echo $i 
                }
In this example, the statement $i = $i+2 is replaced by the more efficient BTCL command incr i 2, where the incr command is used to increment the variable i by 2. If the last argument to incr is omitted, the increment value is 1.

The BTCL foreach command is used to loop through elements of a list. The simplest and most common form of list in BTCL is a string of words separated by spaces.

The following example shows how the foreach command functions:

BTCL >   $a = "first second third" ; foreach i $a {echo $i}
         first 
         second 
         third
You may want to skip the remainder of the body script or abandon a loop under certain conditions. The subloop commands continue and break enable this. For example:

BTCL >   eval { 
         set a {4.0 1.25 10000 1e+90 0.0 20.0}
         foreach i $a { 
                 if {$i > 1e+80} { 
                         echo reciprocal = 0.0 
                         continue
                 } elseif {$i == 0.0} { 
                         echo cannot take the reciprocal of zero
                         break 
                 } 
                 echo reciprocal of $i = [expr 1.0/$i]
         } 
         echo ok 
         }
 reciprocal of 4.0 = 0.25 
 reciprocal of 1.25 = 0.8 
 reciprocal of 10000 = 0.0001 
 reciprocal = 0.0 
 cannot take the reciprocal of zero 
 ok
Here, it was necessary to use 1.0/$i to generate the reciprocal value. This is because BTCL uses integer arithmetic by default, so that 1/$i would have given the result 0 for 1/10000.


Main access page Advanced-Use access.

BTCL Language access The eval and expr Commands Procedures

Copyright Biosym/MSI