Control Structures in Tcl

Advertisement

Advertisement

Control structures like if/else statements, for loops, and switch cases are vital to writing program logic. These code snippets will serve as easy references for these control structures when writing with the Tcl programming language

# If, elseif, else
if {$x > 3} {
    puts "x is greater than 3"
} elseif {$x < 3} {
    puts "x is less than 3"
} else {
    puts "x must equal 3 since it is the only option left"
}

# Foreach
foreach name $customerList {
    puts name
    #continue can be used to jump to the next iteration
}

# While
while {$x < 100} {
   puts $x
   incr x # Increment $x
   #break can be used to exit loop
}

# Switch
puts "Enter your name: "
gets stdin name

switch $name {
    "NanoDano" {
        puts "Hello master."
    }
    "Jim Raynor" {
        puts "Welcome, Commander!"
    }
    default {
        puts "You must not be important."
    }
}

Advertisement

Advertisement