:TITLE: Numeric Comparisons 101 - if ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl8.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: Tcl uses an if construct similar to that used by other languages. The syntax is:

The words then and else are optional, and may be included if you think they improve the readability of the code, or left out, as you prefer.

The test expression following the word if is evaluated in the same manner as in the expr command. Hex strings 0xXX will be converted to their numeric equivalent before evaluation.

The test expression following if should return one of:

                      False            True
                      -------          ------ 
 a numeric value      0                all others
 yes/no               no               yes 
 true/false           false            true

The case of a textual return is not checked.

The test expression following the word if is evaluated in the same manner as in the expr command. Hex strings 0xXX will be converted to their numeric equivalent before evaluation.

The test expression following if may be enclosed within quotes, or braces. If it is enclosed within braces, it will be evaluated within the if command, and if enclosed within quotes it will be evaluated during the substitution phase, and then another round of substitutions will be done within the if command. :TEXT_END:

:LESSON_TEXT_START_LEVEL 1: Like most languages, Tcl supports an if command. The syntax is:

The words then and else are optional, and may be included if you think they improve the readability or left out. If you are used to programming in a shell language, you may want to leave them in. If you are used to C, you will prefer leaving them out.

The test expression following if should return one of:

                      False            True
                      -------          ------ 
 a numeric value      0                all others
 yes/no               no               yes 
 true/false           false            true

If the test expression returns a string "yes"/"no" or "true"/"false", the case of the return is not checked. True/FALSE or YeS/nO are legitimate returns.

If the test expression evaluates to True, then body1 will be executed.

If the test expression evaluates to False, then the word after body1 will be examined. If the next word is elseif, then the next test expression will be tested as a condition. If the next word is else then the final body will be evaluated as a command.

The test expression following the word if is evaluated in the same manner as in the expr command. Hex strings 0xXX will be converted to their numeric equivalent before evaluation.

The test expression following if may be enclosed within quotes, or braces. If it is enclosed within braces, it will be evaluated within the if command, and if enclosed within quotes it will be evaluated during the substitution phase, and then another round of substitutions will be done within the if command. :TEXT_END:

:LESSON_TEXT_START_LEVEL 2: The switch command allows you to compare a string to other strings and find a match. Another feature of most computer languages is the ability to choose between two courses of action based on a numeric test. Like most other languages, Tcl supports this functionality with the if command.

The syntax for the if command is:

The words then and else are optional, you may include them if you think they improve the readability of your code, or leave them out if you think that looks better.

When an if command is encountered the Tcl interpreter evaluates test1 using the expr command. If that test is true, the Tcl interpreter executes body1. If the test expression evaluates to False, then the word after body1 will be examined. If the next word is elseif, then the next expression will be evaluated as a test condition. If the next word is else then the final body will be evaluated as a command.

The test expression following if should return one of these values:

                      False            True
                      -------          ------ 
 a numeric value      0                all others
 yes/no               no               yes 
 true/false           false            true

If the test expression returns a character string like "yes", "no", "true" or "false", the case of the return is not checked. "True", "FALSE", "YeS" or "nO" are legitimate returns.

The test following if may be enclosed within quotes, or braces. If it is enclosed within braces, it will be parsed by expr within the if command. If the test is enclosed within quotes substitutions will be done during the Tcl parsing substitution phase, and then another round of substitutions will be done within the if command.

Since the test in an if is evaluated in the same manner as in the expr command, hex strings 0xXX will be converted to their numeric equivalent before evaluation. You can think of the test in the if command as being executed by expr, and all the same rules will apply.

if {$i == 1} {puts "MATCH}

is the same as:

if {[expr $i == 1]} {puts "MATCH}

The body code may be enclosed within quotes or braces. Since body will be evaluated just like any other Tcl command, body can be placed within braces and variable substitution will still occur. Note that the opening quote or bracket must be on the same line as the if command, however.

:TEXT_END: :CODE_START: set x 1; if {$x == 2} {puts "$x is 2"} else {puts "$x is not 2"} if {$x != 1} { puts "$x is != 1" } else { puts "$x is 1" } if $x==1 {puts "GOT 1"} ;# Two passes of substitution will be done on this test string ;# The initial pass replaces $y with x ;# Within the if statement, $x is replaced with 1 set y x; if "$$y != 1" { puts "$$y is != 1" } else { puts "$$y is 1" } :TEXT_END: