TSE for-loop confusion

14 views
Skip to first unread message

Tom Collins

unread,
Feb 12, 2026, 4:21:30 PM (8 days ago) Feb 12
to SemWare TSE Pro Text Editor
So I coded up a for loop in TSE today, quickly scanned the help, and wrote this:

INTEGER ii

for ii = 66 downto 1 by -1
  ...
endfor

The editor hung.  Upon more careful examination of the help, it appears that "downto" implies decrementing the loop by -1, so you don't need "by -1".  But I think what happens is that if you do include a negative decrement with a downto, the editor ends up incrementing the loop variable instead.  This feels confusing to me.

I'm not 100% sure what to suggest.  Perhaps if there's a for-loop and the compiler notices that you've done something that'll be an infinite loop, then that should be an error.

Thoughts?

Carlo Hogeveen

unread,
Feb 12, 2026, 4:50:08 PM (8 days ago) Feb 12
to sem...@googlegroups.com

A double negative is a positive.
Decreasing by -1 is increasing by 1.
The documentation implies this correctly, but could be more explicit.
It would not be as easy as it seems at first glance for a compiler to catch when this causes in infinite loop, because the break and return() statements can be used to intentionally abort a seemingly infinite loop.
My thought is that the issue is minor: We make this mistake once, and learn.

Carlo
--

---
You received this message because you are subscribed to the Google Groups "SemWare TSE Pro text editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mailto:semware+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/semware/CAODxuObb2pS8x6z0KqVXe%3DCiPC6Us%2BB_7E-7BatK7hFV-v_iFw%40mail.gmail.com?utm_medium=email&utm_source=footer.

knud van eeden

unread,
Feb 12, 2026, 4:54:51 PM (8 days ago) Feb 12
to SemWare TSE Pro Text Editor
PROC Main()
 INTEGER ii
  for ii = 66 downto 1 by -1
  endfor
END

1. Hanging reproduced here, latest TSE for Microsoft Windows version 4.50.19

2. According to the documenation this syntax should be valid in TSE:

The "for" statement syntax is:

    for integer_variable = numeric_expression to/downto
            numeric_expresssion2 [by numeric_expression3]

        statements
    endfor

Specify "to" or "downto" to indicate whether the value of numeric_expression
is to be incremented ("to") or decremented ("downto") until it reaches
(inclusive) the value of numeric_expression2.  The value will be
incremented/decremented by 1, unless you specify an alternate "by"
numeric_expression3 value.

3. Possible root cause:
    
   I assume what happens is that this '-1' simply is converted to 
   -( -1 ) internally, thus +1, so starting from 66 it simply should continue
   to add until it reaches MAXINT (=2^31 - 1)

   
   a large number.

   The 32 bits of TSE integer numbers are in general
   split in 2^31 - 1 positive numbers, 2^31 negative
   numbers and a zero.
    
4. Reaching that value takes a long waiting time assumed, so it appears
   to the outside world viewer that TSE is hanging.

To check that hypothesis, running this program should quickly stop:

PROC Main()
 INTEGER ii
  for ii = 66 downto 1 by -1000000
  endfor
  Warn( ii )
END

This stops very quickly and shows -214697230
which is close to -MAXINT or thus -( 2^32 - 1),
thus the maximum negative number in TSE.

5. Conclusion: From the last example: If negative numbers like -1 used in 'by' it counts 
down in the direction of the negative numbers.

So I believe formally it should stop at 1, when it starts at the larger number 66. 


with friendly greetings
Knud van Eeden


Inline image


--

---
You received this message because you are subscribed to the Google Groups "SemWare TSE Pro text editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to semware+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/semware/CAODxuObb2pS8x6z0KqVXe%3DCiPC6Us%2BB_7E-7BatK7hFV-v_iFw%40mail.gmail.com.

Tom Collins

unread,
Feb 12, 2026, 4:57:59 PM (8 days ago) Feb 12
to sem...@googlegroups.com
I agree in general that this is a mistake that you fix, and that's the way I'd approach it in a general-purpose language.  But for TSE, the macro language is a rarely-used feature (except by you :) so you should warn the user about something funny-looking.  And while I agree it's possible to break out of a loop, I'd have to ask: in practice, is it ever going to be useful to have a for-loop that's looping in the opposite direction of its range?

To unsubscribe from this group and stop receiving emails from it, send an email to semware+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/semware/000701dc9c69%248c5e6d50%24a51b47f0%24%40ecarlo.nl.

knud van eeden

unread,
Feb 12, 2026, 5:02:21 PM (8 days ago) Feb 12
to sem...@googlegroups.com
TSE thus for sure counts down (at least in some 
examples). See my other email.

Thus when starting at 66 and if -1 then checking
thus 65, 64, 63, ..., 2, 1

So when that does not work this is assumed by me
thus a TSE native source code error.

So -> Semware assumed can resolve this.

Knud van Eeden

unread,
Feb 12, 2026, 5:17:57 PM (8 days ago) Feb 12
to sem...@googlegroups.com
 

PROC Main()
 INTEGER ii
  for ii = 66 downto 1 by -1000000
  endfor
  Warn( ii )
END

This stops very quickly and shows -214697230
which is close to -MAXINT or thus -( 2^32 - 1),
thus the maximum negative number in TSE.

===

And that above is at least also a bug, as it should stop immediately after being less than this '1' above,
which is already after one step.



Carlo Hogeveen

unread,
Feb 12, 2026, 5:20:37 PM (8 days ago) Feb 12
to sem...@googlegroups.com

> …, I'd have to ask: in practice, is it ever going to be useful to have a for-loop that's looping in the opposite direction of its range?

No. That is an excellent point!
And in theory checkable at compile-time if all for-parameters are constants.

Carlo




S.E. Mitchell

unread,
Feb 13, 2026, 6:30:41 AM (7 days ago) Feb 13
to sem...@googlegroups.com
I can update the "for" logic to include specific compile-time checks
for potential infinite loops.

If a "by" expression is found and is a constant:

- If the value is 0: Issue a warning for a "possibly infinite loop
with 'by' expression of 0."
- If the value is less than 0 and used with "downto": Issue a warning
for a "possibly infinite loop with 'downto' and 'by' expression < 0."

I can also update the help documentation to be more explicit. Unless
there are other suggestions, I will add:

"When 'downto' is used, the 'by' value (explicit or implied) is
subtracted from the initial value at each iteration; therefore, the
'by' value should generally be positive. Only use a negative 'by'
value if you know what you are doing."
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "SemWare TSE Pro text editor" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to semware+u...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/semware/CAODxuObb2pS8x6z0KqVXe%3DCiPC6Us%2BB_7E-7BatK7hFV-v_iFw%40mail.gmail.com.

knud van eeden

unread,
Feb 13, 2026, 7:29:17 AM (7 days ago) Feb 13
to sem...@googlegroups.com
> Only use a negative 'by'
value if you know what you are doing."

===

for ii = 66 downto 1 by -1

If the current counter value is equal or lower than the lower bound 1 it should stop (always)

So here something like 66 steps expected.

===

for ii = 66 downto 1 by -1000000

If the current counter value is equal or lower than the lower bound 1 it should stop (always)

So here something like 2 steps expected.
The first one for 66 itself, the second one while far below 1.

===

By zero (0) would be lead to an infinite loop so warning or error.


with friendly greetings 
Knud van Eeden 



Sent from Yahoo Mail on Samsung Galaxy S24 Ultra / 1 terabyte / artificial intelligence

knud van eeden

unread,
Feb 13, 2026, 7:47:10 AM (7 days ago) Feb 13
to sem...@googlegroups.com
Maybe coding those loops in pure C and seeing how that behaves. 

Then implementing or mimicking it similarly in TSE.

Negative 'by' values should thus certainly be mathematically possible, even if one does not know what one is doing ;-)


Sent from Yahoo Mail on Samsung Galaxy S24 Ultra / 1 terabyte / artificial intelligence

Tom Collins

unread,
Feb 13, 2026, 10:47:14 AM (7 days ago) Feb 13
to sem...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages