for(3tcl)
π for β the 'For' loop, bestie π
so this is a page from the Tcl Built-In Commands manual, and it's about for, and honestly? it's giving C's for statement but make it Tcl. π come sit with me.
β¨ NAME β¨
for β the 'For' loop π
πΈ SYNOPSIS πΈ
for start test next body
π¦ DESCRIPTION π¦
for is a looping command, and its whole structure is basically the C for statement's twin. π«Ά
So here's the lineup: start, next, and body must be Tcl command strings, and test is an expression string. That's the vibe check on the arguments. β
What happens, in order:
- First,
forinvokes the Tcl interpreter to executestart. π« - Then it repeatedly evaluates
testas an expression. - If that result is non-zero, it invokes the Tcl interpreter on
body, then invokes the Tcl interpreter onnext, then repeats the loop. π - The command terminates when
testevaluates to0.
Now the plot twists:
- If a
continuecommand is invoked withinbody, then any remaining commands in the current execution ofbodyare skipped β no cap. Processing continues by invoking the Tcl interpreter onnext, then evaluatingtest, and so on. π - If a
breakcommand is invoked withinbodyornext, then theforcommand will return immediately. π
The operation of break and continue are similar to the corresponding statements in C. And for returns an empty string. that's it, that's the return value, iconic.
β οΈ OKAY THIS PART. LISTEN. Note thattestshould almost always be enclosed in braces. If it's not, variable substitutions will be made before theforcommand starts executing, which means that variable changes made by the loop body will not be considered in the expression. This is likely to result in an infinite loop. ππ Iftestis enclosed in braces, variable substitutions are delayed until the expression is evaluated (before each loop iteration), so changes in the variables will be visible. β¨ See below for an example: π
π EXAMPLES π
π Print a line for each of the integers from 0 to 9:
for {set x 0} {$x<10} {incr x} {
puts "x is $x"
}
π Either loop infinitely or not at all β because the expression being evaluated is actually the constant. or even generate an error! π
The actual behaviour will depend on whether the variable x exists before the for command is run, and whether its value is a value that is less than or greater than/equal to ten. And this is because the expression will be substituted before the for command is executed. π«
for {set x 0} $x<10 {incr x} {
puts "x is $x"
}
π Print out the powers of two from 1 to 1024:
for {set x 1} {$x<=1024} {set x [expr {$x * 2}]} {
puts "x is $x"
}
π SEE ALSO π
π KEYWORDS π
boolean, for, iteration, loop
okay bestie that's the whole page, you ate it, go write some loops π
this was Tcl, and the page is for(3tcl) β mwah, ily, go forth and iterate. π¦β¨πΈ
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms