Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:37 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMBasic: Is this the expected behaviour for FOR/NEXT

Author Message
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 09:03am 05 Jul 2021
Copy link to clipboard 
Print this post

Hi folks,

Not a complaint, just an enquiry, on the CMM2:
> list "loop_test.bas"
Dim num% = 5
Dim i%

For i% = 1 To num%
 Print i%
 Inc num%
Next

> run "loop_test.bas"
1
2
3
4
5


In C and other languages with which I am most familiar:
[thwill@anon]$ cat loop_test.c
#include <stdio.h>

int main() {
   int num = 5;
   for (int i = 1; i <= num; ++i) {
       printf("%d\n", i);
       num++;
   }
   return 0;
}

[thwill@anon]$ gcc loop_test.c
[thwill@anon]$ ./a.out
1
2
3
4
5
6
7
8
9
10
etc.


Is the MMBasic behaviour of evaluating the FOR loop's end condition only on entry expected/intended ?

Best wishes,

Tom
Edited 2021-07-05 19:04 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
twofingers

Guru

Joined: 02/06/2014
Location: Germany
Posts: 1593
Posted: 09:30am 05 Jul 2021
Copy link to clipboard 
Print this post

Hi Tom,

I would say it works as expected ... for an interpreter language.

Regards
Michael

PS:
I think changing the end conditions is against the for/next-loop philosophy.
Edited 2021-07-05 19:41 by twofingers
causality ≠ correlation ≠ coincidence
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 442
Posted: 09:42am 05 Jul 2021
Copy link to clipboard 
Print this post

  thwill said  Hi folks,

Not a complaint, just an enquiry, on the CMM2:
> list "loop_test.bas"
Dim num% = 5
Dim i%

For i% = 1 To num%
 Print i%
 Inc num%
Next

> run "loop_test.bas"
1
2
3
4
5



Is the MMBasic behaviour of evaluating the FOR loop's end condition only on entry expected/intended ?

Best wishes,

Tom


Yes, most BASIC implementations, both interpreted and compiled, work that way. We professional BASIC programmers (there are more of us than you may think!) are used to it and some even rely on it.
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 09:48am 05 Jul 2021
Copy link to clipboard 
Print this post

  toml_12953 said  Yes, most BASIC implementations, both interpreted and compiled, work that way. We professional BASIC programmers (there are more of us than you may think!) are used to it and some even rely on it.


Thanks, I wasn't passing judgement, it just caught me out so I wanted to check it wasn't an unspecified behaviour. I'll add it to my list of BASIC idiosyncrasies alongside "non short-circuit evaluation of boolean conditions".

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 09:48am 05 Jul 2021
Copy link to clipboard 
Print this post

It's what I would expect too.
When the interpreter reads
For i% = 1 to num%
it copies num% into a termination variable. i% is a true variable so changes to that within the loop will affect how the loop runs, but changes to num% are ignored as that value is already stored.

In low level languages num% wouldn't normally be stored, just referred to, so you'd get a continuous increment in C or assembler.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 442
Posted: 10:00am 05 Jul 2021
Copy link to clipboard 
Print this post

  thwill said  
  toml_12953 said  Yes, most BASIC implementations, both interpreted and compiled, work that way. We professional BASIC programmers (there are more of us than you may think!) are used to it and some even rely on it.


Thanks, I wasn't passing judgement, it just caught me out so I wanted to check it wasn't an unspecified behaviour. I'll add it to my list of BASIC idiosyncrasies alongside "non short-circuit evaluation of boolean conditions".

Best wishes,

Tom

That behavior of ignoring changes to the terminating variable in a FOR-NEXT loop is actually in the ANSI/ISO standard for BASIC so it's well established. I wouldn't call it an idiosyncrasy, exactly. FORTRAN (which played a big part in BASIC's development) seems to work that way as well.

"non short-circuit evaluation of boolean conditions" is NOT a usual feature of BASIC. In standard BASIC, this will NOT give an error:

DIM a(3)
IF a(2)>0 AND a(5)=0 THEN
  PRINT"OK"
END IF
END

Edited 2021-07-05 20:02 by toml_12953
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 10:12am 05 Jul 2021
Copy link to clipboard 
Print this post

  toml_12953 said  That behavior of ignoring changes to the terminating variable in a FOR-NEXT loop is actually in the ANSI/ISO standard for BASIC so it's well established. I wouldn't call it an idiosyncrasy, exactly. FORTRAN (which played a big part in BASIC's development) seems to work that way as well.


Not judging, but convenient since I am contemplating a transpiler from Ratfor.

  Quote  "non short-circuit evaluation of boolean conditions" is NOT a usual feature of BASIC. In standard BASIC, this will NOT give an error:

DIM a(3)
IF a(2)>0 AND a(5)=0 THEN
  PRINT"OK"
END IF
END


OK, I think we may have been down this rabbit hole before

It is my understanding that MMBasic does not short-circuit evaluate, and nor I think does Visual Basic (hence the existence of AndAlso for short-circuit evaluation). Is non short-circuit evaluation standard for Microsoft BASICs ?

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 442
Posted: 10:18am 05 Jul 2021
Copy link to clipboard 
Print this post

  thwill said  
  toml_12953 said  That behavior of ignoring changes to the terminating variable in a FOR-NEXT loop is actually in the ANSI/ISO standard for BASIC so it's well established. I wouldn't call it an idiosyncrasy, exactly. FORTRAN (which played a big part in BASIC's development) seems to work that way as well.


Not judging, but convenient since I am contemplating a transpiler from Ratfor.

  Quote  "non short-circuit evaluation of boolean conditions" is NOT a usual feature of BASIC. In standard BASIC, this will NOT give an error:

DIM a(3)
IF a(2)>0 AND a(5)=0 THEN
  PRINT"OK"
END IF
END


OK, I think we may have been down this rabbit hole before

It is my understanding that MMBasic does not short-circuit evaluate, and nor I think does Visual Basic (hence the existence of AndAlso for short-circuit evaluation). Is non short-circuit evaluation standard for Microsoft BASICs ?

Best wishes,

Tom

True. MS BASICs don't follow the de jure standard which requires BASIC to short circuit boolean evaluation. They follow the de facto standard.
Edited 2021-07-05 20:18 by toml_12953
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 01:17pm 05 Jul 2021
Copy link to clipboard 
Print this post

  toml_12953 said  True. MS BASICs don't follow the de jure standard which requires BASIC to short circuit boolean evaluation. They follow the de facto standard.


Thanks for the confirmation ... again it it convenient because that is apparently the behaviour of most FORTRANs (excluding GNU) and thus of Ratfor, I was afraid I might have to some contortions in the transpiler.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025