![]() |
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 KingdomPosts: 4311 |
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: GermanyPosts: 1593 |
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 StatesPosts: 442 |
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 KingdomPosts: 4311 |
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 KingdomPosts: 7937 |
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 StatesPosts: 442 |
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 KingdomPosts: 4311 |
Not judging, but convenient since I am contemplating a transpiler from Ratfor. 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 StatesPosts: 442 |
Not judging, but convenient since I am contemplating a transpiler from Ratfor. 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 KingdomPosts: 4311 |
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 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |