Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
Tcl - using [] or {} for conditions in an if (and while)

In Tcl, the use of square brackets tells the interpreter to "do this first" whereas curly braces are a request to defer execution of a block - perhaps suppressing it completely as in the action on an if statement.

So the if statement's action will NEVER be written in square brackets - [ ] - as that would cause it to be performed irrespective of the actual condition and would defeat the whole purpose of the condition! But it's a different matter with the condition itself. Have a look at this code:

set cost [gets stdin]
 
if [expr $cost > 20] {
    set gpt [expr $cost * 0.9]
    puts "Group ticket at $gpt"
 }
if {$cost > 20} {
    set gpt [expr $cost * 0.9]
    puts "Group ticket at $gpt"
 }
if {[expr $cost > 20]} {
    set gpt [expr $cost * 0.9]
    puts "Group ticket at $gpt"
 }


All three statements produce the same end result, but they do it differently.

tcl course in March 2011The first - using [ ] - evaluates the expression before it ever runs the if comment, to which is passed just a true or false value and not the condition itself. This works well for an if but should never be used in a while, since the condition would never be rechecked and you would have an infinite loop.

The second - using { } - passes the string containing into if which assumes an expr command is to be used to evaluate it. You would usually want it to be expr-ed, so most of the time thats a good and fair assumption.

Using { } and the [ ] with it gives you the deferred block that you'll need when giving a condition for the while, but then also gives you the ability to embed any command - not JUST an expr - with it. And this is the structure you'll need to use for more complex while loops.

(written 2007-10-23, updated 2011-03-01)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T203 - Tcl/Tk - Conditionals and Loops
  [3571] Comparing loop commands in Tcl - (2012-01-06)
  [3570] Trapping errors in Tcl - the safety net that catch provides - (2012-01-06)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3189] Tcl - the danger of square brackets in a while command - (2011-03-02)
  [2681] Tcl - a great engineering language - (2010-03-17)
  [2471] A short form of if ... then ... else - (2009-10-23)
  [2261] Tcl - nice and nasty - (2009-06-29)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [210] Joining lists in Tcl. Indirect variables in Tcl. - (2005-02-12)


Back to
Dark Dawn
Previous and next
or
Horse's mouth home
Forward to
Tcl - append v lappend v concat
Some other Articles
Sorting in Tcl - lists and arrays
Tcl - global, upvar and uplevel.
Square Bracket protection in Tcl
Tcl - append v lappend v concat
Tcl - using [] or {} for conditions in an if (and while)
Dark Dawn
Pictures Framed
Someone else's wedding
Perl - progress bar, supressing ^C and coping with huge data flows
Using PHP to upload images / Store on MySQL database - security questions
4255 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1401_Tcl ... hile-.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat