Simple shorthand 'for' loop

dusthillresident Sun 24 May 01:56:37 BST 2026

I started out programming in BASIC years ago. I quickly moved to C and became comfortable there before I discovered Tcl, so Tcl's C-style 'for' loop has always felt familiar to me.

But when I'm writing quick scripts, I find myself avoiding the for loop, simply because it feels like a handful to type all those braces. When I used to program in BASIC, I used the 'for' loop all the time. I find myself missing the simplicity of BASIC's for loop, it was very quick and easy to type.

So today, when I was writing another short script and found myself once again instinctively avoiding writing a 'for' loop even where it made the most sense as the loop construct to use, I decided to re-create a vaguely BASIC-style 'for' loop in pure Tcl (with behaviour slightly different from BASIC that makes it more natural for use with Tcl's lists, where the indexing starts at 0)

proc FOR {var args} {
 switch -- [llength $args] {
  3 {
   # FOR x start end {}
   lassign $args start end script
   set step [expr { $start>$end? -1 : 1 }]
  }
  4 {
   # FOR x start end step {}
   lassign $args start end step script
  }
  default {
   error "FOR: wrong # args, should be: FOR varName startValue endValue ?stepValue? script"
  }
 }
 upvar $var loopVar
 set loopVar $start
 while { $start<$end? ($loopVar<$end && $step>0) : ($loopVar>$end && $step<0 ) } {
  uplevel $script
  set loopVar [expr { $loopVar + $step }]
 }
}


gold 5/24/2026. Added categories, so can find this Wiki page in Wiki.





cna - 2026-05-24 12:13:31

It seems okay in theory, but not in practice, meaning the performance.

The foreach cycle is better than for in most use cases, and the FOR wrapper above for is worst of all.



Maybe cna could post some data to demonstrate instead of just posting claims with no proof or explanation?



here is a new version with support for 'break' and 'continue':

proc FOR {var args} {
 switch -- [llength $args] {
  3 {
   # FOR x start end {}
   lassign $args start end script
   set step [expr { $start>$end? -1 : 1 }]
  }
  4 {
   # FOR x start end step {}
   lassign $args start end step script
  }
  default {
   error "FOR: wrong # args, should be: FOR varName startValue endValue ?stepValue? script"
  }
 }
 upvar $var loopVar
 set loopVar [expr { $start - $step }]
 while {
  ([set loopVar [expr {$loopVar + $step}]] != $end)
   &&
  ($start<$end? ($loopVar<$end && $step>0) : ($loopVar>$end && $step<0))
 } {
  uplevel $script
 }
}


cna is actually wrong, as shown by this test script:

# Generate a long script
set script ""
FOR i 0 444 {
 append script "set v[expr {int(rand()*1000)}] [expr {rand()*1000}]\n"
}

# Spin briefly to provoke dynamic CPU frequency scaling to ramp up
FOR i 0 20000 {}

# Test performance of standard Tcl 'for' vs 'FOR'
foreach x {1 2 3 4} {
 puts "---- for ----"
 puts [time {for {set i 0} {$i<1000} {incr i} $script}]
 puts "---- FOR ----"
 puts [time {FOR i 0 1000 $script}]
}

console output:

---- FOR ----
29098 microseconds per iteration
---- for ----
28155 microseconds per iteration
---- FOR ----
28137 microseconds per iteration
---- for ----
27936 microseconds per iteration
---- FOR ----
28066 microseconds per iteration
---- for ----
27909 microseconds per iteration
---- FOR ----
28154 microseconds per iteration
---- for ----
28014 microseconds per iteration

cna - 2026-05-24 17:16:44

Hm, it's obvious, the testing to do while Tcl scripting, not? Though, after all, i'm idiot being russian, how can i gain you over? this would be even impossible.


Sorry. I don't mean to say or suggest that you're an idiot, and your nationality shouldn't matter.

It's just frustrating when someone shows up just to say "this is bad because (claim x)" without providing any substance to back that up at all, or any kind of constructive input (eg. how to improve or fix the issue), and especially frustrating when the claim x turns out to be false - the test above shows that the difference is ~200 microseconds, negligible. And if you care that much about performance, you would probably not be using Tcl, one of the slowest interpreted languages.

I would urge you to make better contributions than just showing up to say "this performs badly" without explaining why or how or offering any remedy or improvement.



cna - 2026-05-24 18:43:16

Большинство погибших в Старобельске студенток похоронят в подвенечных платьях.


I'm sorry about how I wrote earlier. I overreacted. I don't want to be rude to people.