for(n)
π for(n) β the Tcl Built-In Commands girlies present: the for loop π
okay bestie, gather round, because we are about to talk about the single most iconic looping command in all of Tcl. it's called for, it lives in the Tcl Built-In Commands section, and honestly? she ate. β¨
πΈ NAME πΈ
for β the 'For' loop, your ride-or-die iteration bestie π
π¦ SYNOPSIS π¦
for start test next body
β¨ DESCRIPTION β¨
so for is a looping command, and structurally it's giving major C for statement energy β like, they're cousins fr fr. π
here's the tea on the arguments:
start,next, andbodyhave to be Tcl command strings.testis an expression string.
and here's how the whole thing goes down, step by step:
- first,
forinvokes the Tcl interpreter to executestart. one and done. π - then it repeatedly evaluates
testas an expression. if the result is non-zero, it:- invokes the Tcl interpreter on
body, - then invokes the Tcl interpreter on
next, - then loops back around and does it all again. π
- invokes the Tcl interpreter on
- the command terminates when
testevaluates to 0. periodt.
now, the drama π β if a continue command gets invoked inside body, then any remaining commands in the current execution of body are skipped. processing then continues by invoking the Tcl interpreter on next, then evaluating test, and so on. so it's not a full stop, it's more of a "not right now, but we're still going" vibe.
but if a break command gets invoked inside body or next, then the for command returns immediately. done. no more loop. π
the way break and continue operate is similar to the corresponding statements in C. and for returns an empty string. (yes, an empty string. she gives nothing and that's the point. π)
π«Ά the braces thing (this is important, bestie) π«Ά
note that test should almost always be enclosed in braces. do not skip this. i am literally begging.
here's why, no cap:
- if
testis not braced, variable substitutions will be made before theforcommand starts executing. which means variable changes made by the loop body will not be considered in the expression. this is likely to result in an infinite loop. πππ - if
testis enclosed in braces, variable substitutions are delayed until the expression is evaluated β which happens before each loop iteration β so changes in the variables will be visible. π
see below for an example! (scroll down, i gotchu π)
π 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"
}
the cursed one π
either loop infinitely or not at all, because the expression being evaluated is actually the constant β or, even worse, 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"
}
see? no braces, no peace. this is the cautionary tale, bestie. π¨
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 β the whole squad is here π«Ά
and that's the page, bestie! this one's a Tcl classic, straight from the Tcl Built-In Commands section, brought to you by the letter n in for(n) and the sheer audacity of looping forever because you forgot your braces. π
β¨ stay looped, stay slayed, and go brace your test. ππ¦
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- 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