Tcl break Command
last modified October 20, 2026
The Tcl break command is used to exit from loops prematurely. It
terminates the innermost enclosing loop and continues execution after the loop.
The command is essential for controlling loop execution flow.
Basic Definition
The break command immediately terminates the current loop's
execution. It works with while, for, and foreach
loops. The program continues with the next command after the loop.
Syntax: break. The command takes no arguments and has no return
value. It simply transfers control out of the current loop structure.
Basic While Loop Break
This example demonstrates using break to exit a while
loop when a condition is met.
set count 0
while {1} {
puts "Count is $count"
incr count
if {$count > 5} {
break
}
}
puts "Loop exited"
This creates an infinite loop with while {1} that would run forever.
The break command exits the loop when count exceeds 5. The message
after the loop confirms exit.
For Loop with Break
This example shows break used within a for loop to
exit when finding a specific value.
for {set i 0} {$i < 10} {incr i} {
if {$i == 7} {
puts "Found 7, breaking loop"
break
}
puts "Current value: $i"
}
puts "After loop"
The loop normally runs from 0 to 9, but exits early when i equals 7. The
break command stops the loop immediately, skipping remaining
iterations. Control passes to the statement after the loop.
Nested Loops with Break
This demonstrates how break only affects the innermost loop when
used in nested loop structures.
for {set i 1} {$i <= 3} {incr i} {
puts "Outer loop: $i"
for {set j 1} {$j <= 3} {incr j} {
if {$j == 2} {
puts " Breaking inner loop at j=2"
break
}
puts " Inner loop: $j"
}
}
The outer loop runs completely, but the inner loop breaks when j equals 2.
The break only affects the inner for loop. The outer
loop continues its normal execution.
Break in Foreach Loop
This example shows break used with a foreach loop to
stop processing a list when a condition is met.
set colors {red green blue yellow orange}
foreach color $colors {
if {$color eq "blue"} {
puts "Found blue, stopping"
break
}
puts "Processing $color"
}
puts "Done"
The loop processes each color in the list until it finds "blue". At that point,
break exits the loop. Colors after "blue" in the list are not
processed. The message after confirms loop exit.
Break with Conditional Logic
This example combines break with more complex conditional logic to
exit a loop based on multiple factors.
set total 0
while {1} {
puts -nonewline "Enter a number (0 to exit): "
flush stdout
if {[gets stdin num] < 0} {
break
}
if {$num == 0} {
break
}
if {$num < 0} {
puts "Negative numbers not allowed"
continue
}
set total [expr {$total + $num}]
puts "Current total: $total"
}
puts "Final total: $total"
This interactive example sums numbers until the user enters 0. The break
exits the loop when 0 is entered or when the input ends (gets
returns -1). Negative numbers trigger continue
to skip that iteration. The final total displays after loop exit.
Break in Switch Statement
Unlike in C, the bodies of a switch do not fall through to the next
case. This example shows what break does when it is used inside a
switch that is part of a loop.
foreach value {1 2 3 4} {
switch $value {
3 {
puts "Found 3, leaving the loop"
break
}
default {
puts "Value: $value"
}
}
}
puts "After loop"
A switch runs only the body of the matching case, so no
break is needed to prevent fall-through. When break
is executed inside a case body, it terminates the enclosing
foreach loop, not just the switch. Here the loop ends
when the value 3 is found, so 4 is never processed. Outside of a loop, such a
break is an error. To share a body between cases, use the
- body.
Best Practices
- Use Sparingly: Minimize break usage for clearer code flow.
- Clear Conditions: Make break conditions obvious and documented.
- Avoid Deep Nesting: Consider restructuring deeply nested loops.
- Alternative Logic: Sometimes loop conditions can replace breaks.
- Comments: Add comments explaining why break is necessary.
This tutorial covered the Tcl break command with practical
examples showing its usage in different loop structures and scenarios.
Author
List all Tcl Tutorials.