Training, Open Source Programming Languages

This is page http://www.wellho.net/mouth/1401_Tcl ... hile-.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

The 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
  [210] Joining lists in Tcl. Indirect variables in Tcl. - (2005-02-12)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [2261] Tcl - nice and nasty - (2009-06-29)
  [2471] A short form of if ... then ... else - (2009-10-23)
  [2681] Tcl - a great engineering language - (2010-03-17)
  [3189] Tcl - the danger of square brackets in a while command - (2011-03-02)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3570] Trapping errors in Tcl - the safety net that catch provides - (2012-01-06)
  [3571] Comparing loop commands in Tcl - (2012-01-06)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4455] Working out distance between places, using OS grid references and a program in Tcl - (2015-03-11)


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
4759 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, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

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

PAGE: http://www.wellho.net/mouth/1401_Tcl ... hile-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb