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.

Working out distance between places, using OS grid references and a program in TclIn Great Britain, locations have Ordnance Survey grid reference positions which place them to within 100 metres. Our training centre / hotel is at ST907633 - that's
100 km square ST
90.7 km into that square from the west side
63.3 km into that squate from the south ...

Learning to program - Loop statements such as whileIf your program always ran each statement just once (indeed skipping over statements which were in blocks in false conditions) it would run very quickly and would have little use. You couldn't (for example) run a program which went through a whole series of results from a database query and displayed ...

Learning to Program - the conditional statement (if)Every language has some sort of conditional statement. That's a way of looking at some sort of setting or status in the program and performing some sort of action based on that setting or status.
such statements take the form ...
if {some sort of condition is true} then {run a group of ...

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 ...

Trapping errors in Tcl - the safety net that catch providesWhat 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 ...

Comparing loop commands in TclTcl 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 ...

Tcl - the danger of square brackets in a while commandIn 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) ...

Tcl - a great engineering languageIt 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 ...

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 ...

A short form of if ... then ... elseThere 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, ...

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. ! ...

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 ...

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 ...