Tcl Conditional Statements: If This Then What?

The conditional statement syntax is a little strange at first but it helps to remember that Tcl is a scripting language where everything is a string and new lines indicate the end of a command. The conditional statement syntax is

if expression1 then {body1} elseif expression2 then {body2}… elseif expressionN then {bodyN} else {bodyN+1}

That looks horrible! Yes, which is why no one should ever write it like that. Instead, it should be formatted for better readability as

if {expression1} then {
body1 
} elseif {expression2} then{
body2 
} … elseif {expressionN} then{
bodyN 
} else {
bodyN+1 
}

There, isn’t that cleaner? Note: Make sure to always put spaces between the commands and braces, it will throw an error otherwise. There are two optional words that can be omitted, then and else. Meaning it could look like

if {expression1} {
        body1 
} elseif {expression2} {
        body2 
} … elseif {expressionN} {
        bodyN 
} {
        bodyN+1 
}

Typically, then is omitted but else is retained for clarity. The expressions are evaluated using the same rules and syntax as the expr command covered in the previous section. This means that best practice is to always wrap the expression in braces as it prevents double substitution and is normally faster to evaluate.

Most of the time expressions are conditional statements. A conditional statement resolves to a boolean (true or false). An example of a conditional statement is 4<5 which returns true. Writing 4>5 is also a valid conditional statement though in this case, it would return false. There are many instances when composite conditional statements are desired. Composite conditional statements are created by using a logical and (&&) or logical or (||).  Look at the table of operators for expr from the previous section to know what can be used within expressions. 

If expression1 evaluates to anything other than zero, including negative numbers then body1 is executed and all other conditions and bodies are skipped. If expression1 evaluates to zero then expression2 is evaluated and on and on until either one of the expressions evaluates to some non zero value and the corresponding body is executed or the else statement is triggered. If there is no else condition then nothing happens.

Notice that the bodies are wrapped in braces. Conditional statements perform a single round of substitution within the braces so you can still use variables within the body.  Here are a few examples.

#While you can write a conditional statement like this, please don't
set x -1
if $x==1 then {puts "x = 1"} elseif $x==2 then {puts "x = 2"} else {puts "x = $x"}

set y 4
if {$y} {
	puts "\$y is non zero"; #Don't be tricked this line will be executed
} elseif {$y < 3} {
	puts "y is less than 3"
} else {
	puts "y must be equal to or greater than 3"
}

set one 1
set onePtzero 1.0
if {$one eq $onePtzero} {
	puts "one and onePtzero are equal"; #this won’t be triggered
} else {
	puts "$one and $onePtzero are not the same because eq\
     	  compares strings"
}

set z 10
if {$z>0 && $z <= 10} {
        puts "z is between 0 and 10"
} elseif {$z > 10} {
        puts "z is greater than 10"
} else {
        puts "z is negative"
}


JOIN OUR NEWSLETTER
I agree to receive email updates when new content is posted and receive promotional emails when new courses are available
Enjoying the article? Receive free updates when new content is published and courses are available.
We hate spam. Your email address will not be sold or shared with anyone else.
Christopher Hogstrom
christopher.hogstrom@grittyengineer.com

Leave a Reply

Your email address will not be published. Required fields are marked *