Why do FOR/NEXT variables end up +1 more? - 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: Why do FOR/NEXT variables end up +1 more? (/showthread.php?tid=1931) Pages:
1
2
|
Why do FOR/NEXT variables end up +1 more? - Dav - 08-24-2023 I vaguely remember something about this at the old .com forum, but I forget the reason, so I thought I'd just ask about it here. Why are variable used with FOR/NEXT +1 more at the end? I was doing a count using a FOR/NEXT variable and couldn't figure out why afterwards the variable end up +1 number higher when over. In this example, the FOR/NEXT prints all 10 t's, but when printing the last t value over, it's at 11, and the FOR/NEXT never went to 11. Why is that? - Dav Code: (Select All)
RE: Why do FOR/NEXT variables end up +1 more? - DSMan195276 - 08-24-2023 It's because `FOR` works by first incrementing the loop variable and then checking if it is greater than the end value you gave. For your loop, the first time `t` is greater is when it is incremented to 11. The reason it works this way is because depending on your `STEP` and what end number you use, you might not hit the last number at all and instead skip right over it. Imagine you did this: Code: (Select All)
Obviously `t` cannot be 10.5 at the end because 10.5 will never be hit by the loop. Instead it works identical to your loop,`t` gets incremented to 11 and since that's greater than 10.5 the loop stops there. RE: Why do FOR/NEXT variables end up +1 more? - Dav - 08-24-2023 Oh I see. Thank you for the explanation. - Dav RE: Why do FOR/NEXT variables end up +1 more? - mnrvovrfc - 08-24-2023 BASIC always behaved this way about FOR... NEXT. Since Dartmouth. Many programs depend on this "quirk". This could cause problems with an _UNSIGNED variable used like this: Code: (Select All) DIM u AS _UNSIGNED INTEGER Expect "u" to be -1? Read the code again. Or if it were possible in QuickBASIC to compile in debug mode it would return a runtime error. This is something that never occurred to me while using QuickBASIC or QBasic. (scratch head) RE: Why do FOR/NEXT variables end up +1 more? - Dav - 08-24-2023 (08-24-2023, 01:02 PM)mnrvovrfc Wrote: BASIC always behaved this way about FOR... NEXT. Since Dartmouth. Many programs depend on this "quirk". Now I wasn't expecting that ... u ends up with a value of 65535, the max value. Interesting. EDIT: u ends up as -1 in QBJS however. - Dav RE: Why do FOR/NEXT variables end up +1 more? - grymmjack - 08-24-2023 @Dav I ran across something that might be useful aside from @mnrvovrfc comments above. From the Wiki in turn... https://qb64phoenix.com/qb64wiki/index.php/DO...LOOP Quote: https://qb64phoenix.com/qb64wiki/index.php/FOR...NEXT Quote: https://qb64phoenix.com/qb64wiki/index.php/WHILE...WEND Quote: https://qb64phoenix.com/qb64wiki/index.php/UNTIL Quote: I ran into a unique situation where I needed to switch from a `FOR..NEXT` to a `DO: LOOP UNTIL` last night. It was regarding this `str_shuffle$` function I wrote for my library: I was using a `FOR..NEXT` but the issue was after exiting, I was doing something in an older version of this which was attempting to use the FINAL value of `i%` (my goto var name for iterator) using `ASC(s$, i%)` which exceeded the length of the string by 1. I dogmatically reach for `FOR..NEXT` but will no longer. I wanted to share the findings with you Also what do you think this word is using code from above? (@a740g can't play he cheats! (I shared this with him already on discord )) Code: (Select All) EMUCPSTOR I'm happy to be wrong and corrected and scolded about my code, BTW! If you guys know of a better way to shuffle a string than this! @a740g already told me I shouldn't modify the control variable. But @a740g I found this: Quote:Avoid changing the FOR counterVariable's value inside of the loop. This obfuscates code and is a poor programming practice. And actually I'm not even using a `FOR..NEXT` any more. ... But not to hijack the thread. It's relevant share in context, and wanted to make sure to share with others. @Dav it's a serendipitous coincidence so sorry for the intrusion if you feel slighted by this slightly off-topic but-similar share. RE: Why do FOR/NEXT variables end up +1 more? - Kernelpanic - 08-24-2023 If it is important that no last value is assigned, one can cancel it with "If ... Exit ... End If". Code: (Select All)
RE: Why do FOR/NEXT variables end up +1 more? - Kernelpanic - 08-24-2023 Addition with While ... Wend. Code: (Select All)
RE: Why do FOR/NEXT variables end up +1 more? - Dav - 08-25-2023 Thanks for the info, @grymmjack & @kernelpanic. About the word scramble puzzle thing grymmjack, hmm... I can't see it yet but will take a closer look... I like puzzles. - Dav RE: Why do FOR/NEXT variables end up +1 more? - grymmjack - 08-25-2023 (08-24-2023, 12:51 PM)DSMan195276 Wrote: It's because `FOR` works by first incrementing the loop variable and then checking if it is greater than the end value you gave. For your loop, the first time `t` is greater is when it is incremented to 11.Hey @DSMan195276 Since you used backticks, they work everywhere now to show as code inline like in discord. Also you can use 3 backticks for a regular qb codeblock. Trying the reply to you since it's quoting the backticks to see what will happen Thanks |