Module 47 min

Control Flow

if, for, while, and the brace rule

Control flow in TCL is straightforward once you accept one thing: the braces are part of the syntax and their placement matters.

if / else

tcl
set slack -0.05
if {$slack < 0} {
  puts "VIOLATION: slack is $slack"
} else {
  puts "Timing met"
}

for and while

tcl
for {set i 0} {$i < 4} {incr i} {
  puts "metal layer M[expr {$i + 1}]"
}

set n 8
while {$n > 1} {
  set n [expr {$n / 2}]
}
Watch out

the opening brace must sit on the same line as if, for, and while. Putting it on the next line breaks TCL, because it reads the command as finished at the end of the line. This is a real, frequent error: keep the { on the line with the keyword.

Comparisons

Use the usual operators inside the condition: <, >, ==, !=, <=, >=, and && and || to combine them. For string comparison use eq and ne, which compare as strings rather than numbers.

tcl
if {$corner eq "ss" && $slack < 0} {
  puts "Setup problem at the slow corner"
}
Pro tip

use eq and ne for strings and ==, != for numbers. Comparing strings with == sometimes works and sometimes surprises you; the dedicated string operators are unambiguous.