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 ...
Get Practical Programming in Tcl & Tk, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.