Tcl Tutorial Lesson 10

Looping 102 - for and incr

Tcl supports a loop construct similar to the for loop in C. The for command in Tcl takes four arguments; an initialization, a test, an increment, and the body of code to evaluate on each pass through the loop. The syntax for the for command is:

   for start test next body

During evaluation of the for command, the start code is evaluated once, before any other arguments are evaluated. After the start code has been evaluated, the test is evaluated. If the test evaluates to true, then the body is evaluated, and finally, the next argument is evaluated. After evaluating the next argument, the interpreter loops back to the test, and repeats the process. If the test evaluates as false, then the loop will exit immediately.

start is the initialization portion of the command. It is usually used to initialize the iteration variable, but can contain any code that you wish to execute before the loop starts.

The test argument is evaluated as an expression, just as with the expr, while and if commands.

next is commonly an incrementing command, but may contain any command which the Tcl interpreter can evaluate.

body is the body of code to execute.

When braces are used for grouping, the newline is not treated as the end of a Tcl command. This makes it simpler to write multiple line commands. However, the opening brace must be on the line with the for command, or the Tcl interpreter will treat the close of the next brace as the end of the command, and you will get an error. This is different than other languages like C or Perl, where it doesn't matter where you place your braces.

Within the body code, the commands break and continue may be used just as they are used with the while command. When a break is encountered, the loop exits immediately. When a continue is encountered, evaluation of the body ceases, and the next iteration is started (if there is one left).

Because incrementing the iteration variable is so common, Tcl has a special command for this:

   incr varName ?increment?

This command adds the value in the second argument to the variable named in the first argument. If no value is given for the second argument, it defaults to 1.


Example

for {set i 0} {$i < 10} {incr i} {
    puts "I inside first loop: $i"
}

for {set i 3} {$i < 2} {incr i} {
    puts "I inside second loop: $i"
}

puts "Start"
set i 0
while {$i < 10} {
    puts "I inside third loop: $i"
    incr i
    puts "I after incr: $i"
}

set i 0
incr i
# This is equivalent to:
set i [expr {$i + 1}]

 Resulting output
I inside first loop: 0
I inside first loop: 1
I inside first loop: 2
I inside first loop: 3
I inside first loop: 4
I inside first loop: 5
I inside first loop: 6
I inside first loop: 7
I inside first loop: 8
I inside first loop: 9
Start
I inside third loop: 0
I after incr: 1
I inside third loop: 1
I after incr: 2
I inside third loop: 2
I after incr: 3
I inside third loop: 3
I after incr: 4
I inside third loop: 4
I after incr: 5
I inside third loop: 5
I after incr: 6
I inside third loop: 6
I after incr: 7
I inside third loop: 7
I after incr: 8
I inside third loop: 8
I after incr: 9
I inside third loop: 9
I after incr: 10