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 

Comments

rauphel79 at 21 Sep, 2007 12:45
Hi,
I have question to ask about if statement in term of condition expression

if [ -n $a ]

what is the equivalent in tcl ?

Regards,
Reply to this.
Anonymous at 05 Sep, 2008 06:43
if {exists $n}
Reply to this.
Anonymous at 24 Apr, 2008 07:39
I believe this [ -n $a ] would be referring to shell-scripting ? (test whether variable a is NOT set)
Reply to this.
sandeep at 18 Jun, 2008 01:52
Error: syntax error in expression " $i != "" && $i == Exported "
//when used in if loop
if { $i != "" && $i == Exported } {
puts -nonewline $fpw "$i "
}
Reply to this.
Gaurav Thakur at 22 Sep, 2009 01:27
shouldn't it be "Exported" and not Exported ???
Reply to this.
khajaahmed at 28 Sep, 2009 07:11
Hi,
how to take input from the keyboard like scanf in C, is there any function like scanf in TCl also.
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