If Then Else

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 6.  Control Structure Commands


If Then Else

The if command is the basic conditional command. If an expression is true, then execute one command body; otherwise, execute another command body. The second command body (the else clause) is optional. The syntax of the command is:

 if expression ?then? body1 ?else? ?body2? 

The then and else keywords are optional. In practice, I omit then but use else as illustrated in the next example. I always use braces around the command bodies, even in the simplest cases:

Example 6-1 A conditional if then else command.
 if {$x == 0} {    puts stderr "Divide by zero!" } else {    set slope [expr $y/$x] } 

graphics/tip_icon.gif

Curly brace positioning is important.


The style of this example takes advantage of the way the Tcl interpreter parses commands. Recall that newlines are command terminators, except when the interpreter is in the middle of a group defined by braces or double quotes. The stylized placement of the opening curly brace at the end of the first and third lines exploits this property to extend the if command over multiple lines.

The first argument to if is a boolean expression. As a matter of style this expression is grouped with curly braces. The expression evaluator performs variable and command substitution on the expression. Using curly braces ensures that these substitutions are performed at the proper time. It is possible to be lax in this regard, with constructs such as:

 if $x break continue 

This is a sloppy, albeit legitimate, if command that will either break out of a loop or continue with the next iteration depending on the value of variable x. This style is fragile and error prone. Instead, always use braces around the command bodies to avoid trouble later when you modify the command. The following is much better (use then if it suits your taste):

 if {$x} {     break } else {     continue } 

When you are testing the result of a command, you can get away without using curly braces around the command, like this:

 if [command] body1 

However, it turns out that you can execute the if statement more efficiently if you always group the expression with braces, like this:

 if {[command]}body1 

You can create chained conditionals by using the elseif keyword. Again, note the careful placement of curly braces that create a single if command:

Example 6-2 Chained conditional with elseif.
 if {$key < 0} {    incr range 1 } elseif {$key == 0} {    return $range } else {    incr range -1 } 

Any number of conditionals can be chained in this manner. However, the switch command provides a more powerful way to test multiple conditions.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net