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, and body have to be Tcl command strings.
  • test is an expression string.

and here's how the whole thing goes down, step by step:

  1. first, for invokes the Tcl interpreter to execute start. one and done. 🌟
  2. 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 loops back around and does it all again. πŸ”
  3. the command terminates when test evaluates 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 test is not braced, variable substitutions will be made before the for command 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 test is 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 🌈

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. 🚨
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