Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Comparing loop commands in Tcl

Tcl offers you three different loop commands - here they are generating the same output:

while A condition is tested, and if true the commands in a loop are performed. The condition is then retested (provided that you have passed the test into while as a deferred block)

  set month 1
  while {$month <= 12} {
    puts "2. This is for month $month"
    incr month
    }


for A prettified version of while - it takes two extra parameters which are normally used to initialise a variable, and to increment that variable before the test is redone.

  for {set month 1} {$month <= 12} {incr month} {
    puts "1. This is for month $month"
    }


foreach Run a block of code (a loop) for each value in a list.

  foreach month {1 2 3 4 5 6 7 8 9 10 11 12} {
    puts "3. This is for month $month"
    }


The latter is significantly different in its use and intent.

See full program [here].

All loops taught / covered / compared on our Tcl and Tcl/Tk courses.
(written 2012-01-06)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T203 - Tcl/Tk - Conditionals and Loops
  [4455] Working out distance between places, using OS grid references and a program in Tcl - (2015-03-11)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [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)
  [1401] Tcl - using [] or {} for conditions in an if (and while) - (2007-10-23)
  [210] Joining lists in Tcl. Indirect variables in Tcl. - (2005-02-12)


Back to
Trapping errors in Tcl - the safety net that catch provides
Previous and next
or
Horse's mouth home
Forward to
Adding Expect on top of Tcl - what is it and where can I get a training course to learn about it?
Some other Articles
Multiple buttons calling the same proc in wish (tcl/tk)
Perl functions such as chop change their input parameters
New in Java 7 - and why we are not running public Java 7 courses
Adding Expect on top of Tcl - what is it and where can I get a training course to learn about it?
Comparing loop commands in Tcl
Images of the new year in Melksham
Telling which ServerAlias your visitor used - useful during merging domains
First of the year
Initial thoughts - response to GW Rail Franchise Consultation for Wiltshire
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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3571_Com ... n-Tcl.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat