Controlling flow with the if statement

The if command evaluates a condition and if the condition evaluates to true, the actions are performed. The condition must be Boolean. With the addition of the else and elseif keywords, multiple conditions may be evaluated and numerous actions can be performed.

How to do it…

In the following recipe, we will create a Tcl script to be called from the command line that evaluates the argument passed, and based on the argument provided, perform an action.

Create a text file named if.tcl that contains the following commands:

 # Set the variable x to the argument set x [lindex $argv 0] # Test for condition 1 if {$x == 1} { puts "Condition 1 - You entered: $x" # Test for condition 1 } elseif {$x == 2} { puts "Condition ...

Get Tcl/Tk 8.5 Programming Cookbook 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.