O'Reilly logo

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

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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]
}

Curly brace positioning is important.

The ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required