The upper bound of the for loop

For i% = 0 To 32767 crashes. The program stops with an error ar the Next statement.
For i% = 0 to 32766 is OK.
This malfunction surprises those wishing to use the full range of long.
Can it be corrected?

The one step after the last is always executed and the for…next loop is finished when the upper limit is exceeded.

for i = 1 to 32766
   if some_condition() = True then exit for
next
if i = 32767 then
   Msgbox "Condition not met"
else
  Msgbox "Condition met at step "& i
endif

Run this in step mode:

for i = 1 to 2
next i

At the end (next i) of the first cycle i is 1, then 2 and then it goes back to the for line, counts up to 3 and exits the loop.

I have corrected my post. Thank you.

A (prinitive but clear) way to loop through the full Range of type Integer is demonstrated by

Sub Main
i% = - 32768
loopNext:
  If (i% Mod 1000)=0 Then MsgBox(i%) REM Just an example statement!
  If i%>=32767 Then Goto afterLoop
  i% = i% + 1
Goto loopNext
afterLoop:
msgBox(i%)
End Sub

All right. Your construction is used and it works in this Basic. It is used by many.
My “academic” script below, semantically correct, will not work.

Dim i as Integer
for i = 1 to 32767
   b = some_condition() 
   if b then exit for
next
if Not b then
   Msgbox "Condition not met"
else
  Msgbox "Condition met at step "& i
endif

"Inadmissible value or data type. Overflow."?
Don’t use variables of type Integer. Use instead type Long.

1 Like

My “academic” script below, semantically correct, will not work.

Syntactically correct, but semantically dubious.

As explained elsewhere, the for loop is semantically equivalent to “repeat … until variable is larger than limit”.

Syntax requires that the limit be a numeric value (number, or expression evaluating to a number). This (syntactical) condition is satisfied.

A value of type integer can never be larger than the limiting number you have set. Thus, your test for <integer variable> larger than <maxint> makes no sense. This, in my opinion, counts as a semantical error. The human language semantics make sense, but the mentioned implication made by the computer language breaks semantically.

Asking a computer to assess a larger number than what it supports in a given context is akin to assessing the magnitude “larger than infinite” in a real world context.

No, what you explain is how it’s implemented, not what is is intended to mean in the language. It is intended to mean “for each value in the stated range, taking defined steps”, and if an implementation detail prevents it from working correctly, it should be fixed :slight_smile:

OTOH, if the upper bound were outside of the type’s possible values, that would be a user error.

No way! The lower bound is missing.

And: the old Basic standard of typing variables by a one-character-suffix is outdated, imo, and should be abandoned. (Alas, there are the file handles.).
Dim i As Long is what you need.
LibO Basic has integer types Byte, Integer, Long.
Dim i% is “shorthand” for Dim i As Integer. The range then is
-32768(=-2^15) .. 32767(=2^15 - 1).
Range for Long is -2147483648(-2^31) .. 2147483647(=2^31 - 1)
That you can’t use 32767 as the upper bound of an Integer For-Next loop is due to the way LibO Basic handles such Loops (badly): The Next i% used after the statements for i% = 32767 woud first try to increment i% wich causes the error.

1 Like

Hmm. But OTOH, Next means “do the next increment”, and indeed, it should be executed at the 32767’s pass, too… And VBA also errors out.

Hmm.

Yes, this is not a bug. What’s important is the expected value of the counter variable after the loop. So any Basic code is OK to expect that

for i = 0 to 5
next i
MsgBox i

would output 6.

Implementation governs the interpretation. According to human interpretation the given construct is semantically sound. According to general behavior of FOR loops (in C as well as in BASIC) it is not. The stepping takes place until the “repeat condition” fails. Whether the interpretation on the receiving end is a defining component for semantics, well I guess that is a semantical conundrum…

According to human interpretation of language the given construct is semantically sound. According to general behavior of FOR loops (in C as well as in BASIC) it is not. The stepping takes place until the “repeat condition” fails.

The FOR … IN operation used on lists handling, available in many languages, works closer to human interpretation.

So, if “semantics” should apply as “sensible in everyday language”, you @mikekaganski and @antekg0 are of course right. If it should apply to implementation context, I am. Whether the implementation is a good one is a different matter.

Anyway, we are splitting hairs here. I guess I started it. I apologize! In either case, I guess for practical purposes the message has gotten across.

According to which specification?

Any bug may only be corrected, if it is reported correctly. What you describe is a bug. But this is not a proper place to report it.

Or not. :slightly_smiling_face:
MS Office VBA has the same behavior (and many other languages).
The programmer must know the algorithms for executing cycles.

2 Likes