Version 4 of incr

Updated 2002-03-20 13:36:08

increment a variable http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/incr.html

incr variable ?increment?

The variable must exist before the incr call, and must have an integer value. The increment must also be an integer and defaults to 1 if not specified, with the result being that

 incr i

and

 set i [expr {$i + 1}]

are nearly equivalent. The difference of course has to do when the assumptions are violated - if i isn't an integer, then the second expression works whereas the first raises an error that you have to catch unless you want the program to terminate.

Note that the increment can be a negative number - allowing you to 'increment' decrementally ;-).


Work is underway to add more 64 bit support into Tcl. Even when this happens, the increment value is supposedly going to continue to be limited to 32 bits due to the far reaching ramifications of changes needed.


RS had a half-baked idea these days - extend incr to also work on characters (like in C), so we could write:

 for {set letter "A"} {$letter<="Z"} {incr letter} {...} 

Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command from Tcl