![]() |
|
What is wrong with this for/next loop - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: What is wrong with this for/next loop (/showthread.php?tid=3614) |
What is wrong with this for/next loop - Helium5793 - 04-14-2025 When I type this in For i% = 1 To 5 Print i% Next Print i% I get when I run it: 1 2 3 4 5 6 Seems like it should be 5 not 6. Why is i% 6 after it comes out of the for/next loop? I am running QB64e under fedora linux. Not sure how to find the version number, but the installation files all have 12/13/2024 as the date modified on them so it isn't an old version. RE: What is wrong with this for/next loop - ahenry3068 - 04-14-2025 This is normally how BASIC works. The Index variable gets changed by STEP (or just +1 if no STEP is specified) at the NEXT. It happens one final time when the for loop exits. Actually by formal definition the variable value is UNDEFINED except during the loop. But what you found is how every BASiC I've dealt with has actually behaved. RE: What is wrong with this for/next loop - Jack002 - 04-14-2025 (04-14-2025, 01:03 PM)ahenry3068 Wrote: This is normally how BASIC works. The Index variable gets changed by STEP (or just +1 if no STEP is specified) at the NEXT. It happens Agreed. I first saw this happen with Commodore v 2.0. BASIC. This is how the FOR loop operates in BASIC RE: What is wrong with this for/next loop - TempodiBasic - 04-14-2025 Quote:This is how the FOR loop operates in BASICIMHO it seems how FOR loop operates... when you exit from the loop the counter of loop will be max + 1 because until the counter is max it must run the loop! Code: (Select All)
here a screenshot of output using VScode ![]() and here a example in C compiled with g++
RE: What is wrong with this for/next loop - Kernelpanic - 04-15-2025 (04-14-2025, 01:00 PM)Helium5793 Wrote: Why is i% 6 after it comes out of the for/next loop?This has nothing to do with Basic; it's the same in C. The reason is simple: The loop is executed until it detects that the final count will be exceeded on a subsequent iteration. However, the counter variable "i" contains the last value, which in this case is "6."
RE: What is wrong with this for/next loop - SMcNeill - 04-15-2025 Lots of people have answered this, but let me go ahead and add my two cents worth on WHY it works this way. FOR i = 1 TO 5 NEXT PRINT i For the above, it seems reasonable to assume that i *could* be 5. We're running a loop and counting from 1 to 5, so *WHY* is the value 6 after that loop? That's easily explained with a second set of code: FOR i = 1 TO 10 STEP 2 NEXT PRINT i Now, with the above, I can never *be* to 10. It counts by 2, starting at 1, so I will be: 1 -- first loop 3 -- second loop 5 -- third loop 7 -- fourth loop 9 -- fifth loop 11 -- greater than 10, so EXIT loop As you can see, I can *never* be 10 as we're counting odd numbers... So how would this loop ever terminate IF it had to match the target number exactly? Obviously, it couldn't. Instead, the loop runs until the variable EXCEEDS the target value. In this case, it'd be 11. We set the FOR loop to run from 1 TO 10, and we count by 2, so 11 would be when it first exceeds that value. In the FOR i = 1 TO 5, it works the same way. The process starts counting at 1, adds 1 each loop, and only stops once it EXCEEDS 5. 1... start loop and add 1 each pass 2...add one 3...add one 4 ...add one 5 ...add one (5 still doesn't EXCEED 5, so we continue on) 6... Exceeds 5, so EXIT loop And that's basically the *WHY* it behaves the way it does. A FOR loop starts at the first number, counts by the STEP, and repeats until it EXCEEDS the second number. ^ It's just that simple of a process.
RE: What is wrong with this for/next loop - Kernelpanic - 04-15-2025 This can be prevented, for example, in this way: Code: (Select All)
|