Previous
While Loop 
Next
Procedures 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Switch Loop

The Control Flow Loops

Switch Loop

Evaluate one of several scripts, depending on a given value.

Syntax:
switch ?options? string { pattern body ?pattern body ...? }

I like this syntax better...
switch ?options? { string } {
pattern { body }
?pattern { body }
...?
}

?options? can be any of the below -
-exactUse exact matching when comparing string to a pattern. This is the default.
-glob When matching string to the patterns, use glob-style matching (i.e. the same as implemented by the string match command).
-regexpWhen matching string to the patterns, use regular expression matching
Switch is very similar to the if-else commands. So I will use the same example I used there.

#The marriage status script...
#Change the status please
set marital_status "After"

label .thesis -text "Marriage is a three ring circus..."

switch $marital_status {
"Before" { set ring "Engagement" }
"During" { set ring "Wedding" }
default { set ring "suffe -" }
}

label .proof -text "$marital_status Marriage: $ring ring"

pack .thesis
pack .proof

Run this script and give the 'marital_status' to "Before", "During" and "After" and see the different results.

Previous
While Loop 
Next
Procedures 
Subscribe to Feed