for
C-style loop with initialization, test, and increment.
Syntax
tcl
for start test next bodyParameters
- start: Initialization script, executed once before the loop
- test: Loop condition expression, evaluated with
exprbefore each iteration (0 = false, non-zero = true) - next: Increment script, executed after each iteration
- body: Loop body to execute
Return Value
Returns an empty string upon normal completion.
Behavior
- Executes the
startscript once at the beginning - Evaluates the
testexpression before each iteration - If
testis true (non-zero), executesbody - After
body, executes thenextscript - Repeats from step 2 until
testevaluates to false (zero)
Loop Control
break: Exits the loop immediately when invoked inbodyornextcontinue: Skips remaining commands inbody, but still executesnextbefore the next iteration
Note: Using continue within the next script produces an error ("invoked 'continue' outside of a loop").
Examples
Basic counting loop
Output
Counting by twos
Output
Nested loops
Output
Using break to exit early
Output
