:TITLE: Looping 102 - For and incr ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl10.lsn,v 1.1 2004/11/04 16:01:13 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: Syntax: forstart test next body

The man page entry for for:

For is a looping command, similar in structure to the C for statement. The start, next, and body arguments must be Tcl command strings, and test is an expression string.

The for command first invokes the Tcl interpreter to execute start. Then it repeatedly evaluates test as an expression; if the result is non-zero it invokes the Tcl interpreter on body, then invokes the Tcl interpreter on next, then repeats the loop. The command terminates when test evaluates to 0.

If a continue command is invoked within body then any remaining commands in the current execution of body are skipped; processing continues by invoking the Tcl interpreter on next, then evaluating test, and so on.

If a break command is invoked within body or next, then the for command will return immediately. The operation of break and continue are similar to the corresponding statements in C.

For returns an empty string.

As with the while command, the test should be placed within braces to keep the substitution phase of the interpreter from replacing the iterating variable with its start value before evaluating the for command.

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. :TEXT_END: :LESSON_TEXT_START_LEVEL 1: Tcl supports an iterated loop construct similar to the for in C, and the do loop in Fortran. 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 commonly 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 numerically, just as with the expr command, and as done in the while command. This can contain any expression that you would pass to expr.

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.

Since you commonly do not want the Tcl interpreter's substitution phase to change variables to their current values before passing control to the for command, it is common to group the arguments with braces. 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 there will not be enough arguments for the for command.

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 test is re-evaluated.

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. :TEXT_END: :LESSON_TEXT_START_LEVEL 2: The while command is very powerful, and can be used anyplace you need to perform a loop. However, there are many times when you want a simpler method of constructing a loop.

Tcl supports an iterated loop construct called the for command. The for command takes four arguments; an initialization, a test, an increment, and the body of code to evaluate on each pass through the loop. These are the same four pieces of code you'd have to use with a while command, but they can be expressed more compactly with the for command.

The syntax for the for command is:

for start test next body

The evaluation of the for goes through these steps:

  1. the start code is evaluated once, before any other arguments are evaluated.
  2. the test is evaluated.
  3. the next argument is evaluated
  4. loop back to step 2, and repeat process.

Start is the initialization portion of the command. It is commonly 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 numerically, just as with the expr command, and as done in the while command. This can contain any expression that you would pass to expr.

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.

Since you commonly do not want the Tcl interpreter's substitution phase to change variables to their current values before passing control to the for command, it is common to group the arguments with braces. 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 there will not be enough arguments for the for command.

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 test is re-evaluated.

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. The second argument (increment) can be a negative value if you want to count down, instead of up. :TEXT_END: :CODE_START: for {puts "Start"; set i 0} {$i < 2} {incr i; puts "I after incr: $i"; } { puts "I inside first loop: $i" } ;# Because the test is evaluated before the body, ;# this loop won't execute the body of the code. for {puts "Start"; set i 3} {$i < 2} {incr i; puts "I after incr: $i"; } { puts "I inside second loop: $i" } ;# A while loop equivalent to the first for loop: puts "Start"; set i 0; while {$i < 2} { puts "I inside first loop: $i" incr i; puts "I after incr: $i"; } :TEXT_END: