Exercises, examples and other material relating to training module T203. This topic is presented on public courses
Learning to program in Tcl,
Tcl Basics
Every programming language needs conditional commands such as "if" and the ability to repeat sections of code using commands such as "while". We cover these commands in this module, together with "for", "foreach", "switch" and conditional operators.
| Articles and tips on this subject | updated |
| 3570 | Trapping errors in Tcl - the safety net that catch provides What could possibly go wrong when you open a file in a program? Lots of things, actually.
* You could try to open a file in directory you cannot access
* You could try to read from a file that does not exist
* You could try to read from something that's not a plain file - e.g. a directory
* You could ... | 2012-01-07 |
| 3571 | 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 ... | 2012-01-06 |
| 3397 | Does a for loop evaluate its end condition once, or on every iteration? All the languages that we teach have a for loop or the equivalent, which is a clean way of repeating a block of code with a rising or falling index number. It's used in many circumstances - for example in iterating through the months of the year (for m goes from 1 to 12) of in stepping through all ... | 2011-08-18 |
| 3189 | Tcl - the danger of square brackets in a while command In Tcl, every statement is a command - with the parameters separated by spaces. Elements which are written in square brackets are commands which are evaluated before the rest of the statement, and elements which are written in curly braces are deferred - held back - and are only evaluated (if at all) ... | 2011-03-04 |
| 2681 | Tcl - a great engineering language It might be a little old and out of fashion - but Tcl is still very much in use, we use it and we can offer you a learning to program in Tcl training course if you're new to programming, or a Tcl Programming course if you've programmed in another language before. We also offer a two day Tk course as ... | 2011-03-01 |
| 1401 | 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 ... | 2011-03-01 |
| 2471 | A short form of if ... then ... else There are so many times you want to say "if ... then ... else" to do no more than choose between the word "is" and "are", to say "child" or "children", or to say "may" or "may not" in your output. Using an if statement for that's a lot of code for a little job.
In most of the languages we teach (Perl, ... | 2009-10-23 |
| 2261 | Tcl - nice and nasty Tcl is a lovely engineers language ... but goodness you can do some nasty things with it, and write some unmaintainable code.
set count 1
foreach type {if while} {
$type {$count < 5} {
puts "yay $type $count"
incr count
}
}
Which ... | 2009-06-30 |
| 1696 | Saying NOT in Perl, PHP, Python, Lua ... "Isn't there one standard way to say NOT?" asked one of my delegates on today's course - and it's an excellent question. But the answer to a question about a negative subject is itself in the negative - no, there isn't just a single way!
In fact .. I can think of no fewer that 12 ways!
• 1. ! ... | 2008-07-05 |
| 1477 | Decisions - small ones, or big ones? When you're traveling, you'll sometimes come to a point at which you make a decision - to go one way or to go another. Sometimes the decision is a small one - for example, if I'm driving through Marlborough there's a choice of the town centre or the road around the back; whichever I take, they come ... | 2007-12-19 |
| 210 | Joining lists in Tcl. Indirect variables in Tcl. With all programming languages, there seem to be a few things that you end up saying "I'm SURE there's a way to do that" .... yet you can't find out how. Here are two quick solutions in Tcl - a specialist subject, I know, but one of the busiest on our forum.
# Two quick solutions in Tcl ...
# How ... | 2006-06-05 |
Examples from our training material
| diy | The for command |
| diy2 | The foreach command |
| dthrow | The switch command |
| flt | for while and foreach loops compared |
| food | Alternative conditions - while loop |
| ftoc | F to C conversion - exercise |
| ftoc2 | Is water a solid, a liquid or a gas? |
| globber | Switch - exact, glob and regex |
| group | The if command in Tcl |
| group4 | deferred execution blocks with { and } |
| group5 | if, elseif and else in Tcl |
| lazy | The ? : conditional operator |
| lunches | Lunches for each day of the week |
| minimax | Answer to practical - minimum and maximum |
| morelunches | Lunches - different places to go |
| older | Conditional Operator ? and : |
| validlunches | Lunches - validating user input within range |
| wd | The while command in Tcl |
| wipats | More flexible alternative to a switch |
| wtot | breaking out of a Tcl loop |
Pictures
The Tcl if command
Tcl course group
A private Tcl Course in Dublin
Background information
Some modules are
available for download as a sample of our material or under an
Open Training Notes License for free download from
http://www.training-notes.co.uk.
Topics covered in this module
The if command.
deferred substitution.
The else and elseif parameters.
The while command.
Getting out of loops.
The for command.
The foreach command.
The switch command.
Conditional operators.
Combining conditions.
fconfigure.
Locating errors in your Tcl program.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Tcl/Tk, Expect,
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.