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 

Comments

Anonymous at 14 Oct, 2008 06:34
And what if we need to assign a numerical value to the variable "ring" after the comparison.
Reply to this.
saravanan at 18 Jan, 2009 09:20
thanks very much. I am a beginner and this was very helpful.
Reply to this.
Anonymous at 18 Feb, 2009 02:08
could you explain the programs written a lil more
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed