Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Tcl loops and conditionals

Posted by admin (Graham Ellis), 4 September 2002
There seem to be an incredible number of people out there searching for "Tcl" and "loops" or "conditionals" on the search engines, or perhaps it's just that all such people get sent to our web site!

When you're writing Tcl, you must remember that it's a command based language, and that even the conditionalsand loops are commands.  You must also remember that
     square brackets are always performed EARLY
     curly braces are usually perfored LATE

Taking an example, the if command in its simplest form takes two parameters; the first is something that has to return a true or false value, and the second is the thing to do if the first is true.  Thus:
     if  $abc  exit
will cause the exit command to be run if the varaible $abc contains a true value, but the code will drop through to the following line if $abc contains a false value.

I repeat - it's the if COMMAND.  Thus the space after the command word, and th space between the first and second parameters is vital.   And if you want to put a new line between the parameters, you need to protect it with a \ character.

Using blocks in curly braces does allow you to specify a whole lot of follow up commands without having to protect every new line, and using curly braces for the condition does make the syntax a bit easier there too as the block is deferred and passed into the if command itself, which wraps it up in an expr for you.   Thus:
     if {$abc > 10} {
           puts "\$abc is big ..."
           puts "in fact, it contains $abc"
           }
works fine.

Posted by raju (raju), 16 September 2004
Dear Sir,
I have two variables which are needed to to change diffuse color and ambient color and I need to use for loop, But I am having problem declaring and incrementing the variables.I am getting error in the for loop.So, Please help me with a small example.
Thank You,
Raju

Posted by admin (Graham Ellis), 17 September 2004
Hi, welcome Raju.  

You're very welcome to ask about your code - please post up a few lines to show us the problems that you're having and I'll then know how best to advise you.

Graham

P.S.  Please start a NEW TOPIC - that way, our question and answer sequence won't get mixed up with the more general advise in this thread.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

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