Previous
Lists 
Next
For Loop 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
If

The Control Flow

If Condition

The Syntax:
if { test1 } {
body1 
} elseif { test2 } {
body2 
} else {
bodyn
}

If you don't like the curly brackets, you can take them out. But then, you will have to give the full thing in one line. I know that after you had a look at that syntax, you are still wondering what in the world that was. Don't worry - an example will set things straight.


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

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

if { $marital_status=="Before" } {
set ring "Engagement"
} elseif { $marital_status=="During" } {
set ring "Wedding"
} else {
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.

Operators

You will need operators to do the checking. Fortunately, the operators in Tcl are same as in any other language. So a small experience with any language will guarantee that you know all them.

Tcl has two kinds of operators.
Relational operators: <, <=, >, >=, ==, != return 0 (false) or 1 (true).
Operator in Tcl Meaning Example
==is equal to5 == 6
!=is not equal to5 != 6
<is less than 5 < 6
<= is less than or equal to 5 <= 6
>is greater than 5 > 6
>=is greater than or equal to 5 >= 6

Logical Operators: && , ! and || operator - AND, NOT and OR Operators.

Operator Logical Equivalent Example
! expressionNOT!$a
expression1 && expression2AND$a > 6 && $a < 10
expression1 || expression2OR$a != 6 || $a!=5
Previous
Lists 
Next
For Loop 
blog comments powered by Disqus
Subscribe to Feed