Skip to content

for

C-style loop with initialization, test, and increment.

Syntax

tcl
for start test next body

Parameters

  • start: Initialization script, executed once before the loop
  • test: Loop condition expression, evaluated with expr before 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

  1. Executes the start script once at the beginning
  2. Evaluates the test expression before each iteration
  3. If test is true (non-zero), executes body
  4. After body, executes the next script
  5. Repeats from step 2 until test evaluates to false (zero)

Loop Control

  • break: Exits the loop immediately when invoked in body or next
  • continue: Skips remaining commands in body, but still executes next before 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

See Also