Why do FOR/NEXT variables end up +1 more?
#6
@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:
  • DO UNTIL or DO WHILE used with LOOP: The condition is evaluated before running the loop code.
    UNTIL checks if the condition is false each time before running code.
    WHILE checks if the condition is true each time before running code.
  • DO used with LOOP UNTIL or LOOP WHILE: The code block will run at least once:
    UNTIL checks if the condition is false before running loop code again.
    WHILE checks if the condition is true before running loop code again.

https://qb64phoenix.com/qb64wiki/index.php/FOR...NEXT
Quote:
  • NEXT ends the FOR loop code block and increments the counter to the next value even when it exceeds the stop limit.

https://qb64phoenix.com/qb64wiki/index.php/WHILE...WEND
Quote:
  • statements will execute repeatedly while condition is a non-zero value.
  • EXIT WHILE can be used for emergency exits from the loop in QB64 only.
  • A DO...LOOP can use the same DO WHILE condition to get the same results.
  • WHILE loops only run if the WHILE condition is True.

https://qb64phoenix.com/qb64wiki/index.php/UNTIL
Quote:
  • Only one conditional evaluation can be made at the start or the end of a DO...LOOP.
  • DO UNTIL evaluates a condition before and inside of the loop. The loop may not run at all.
  • LOOP UNTIL evaluates a condition inside of the loop. It has to loop once.
  • Skips the loop or loops until an evaluation becomes True.

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 Smile

Also what do you think this word is using code from above? Big Grin (@a740g can't play he cheats! (I shared this with him already on discord Big Grin))

Code: (Select All)
EMUCPSTOR
TSPURMCEO
MRCSTPOUE
TCUSEPRMO
MOTEUPRSC
CPSMEOTRU
OPECSUTRM
MEROUSCPT
TPROECSMU
RSETPOUMC
USECOMTPR
OCTMRPSEU
UCEORMTPS
SUPORCTME
RMPUCSTOE
MTERCSOPU
UPSMCOETR
CRPMTOUES
PEMRCOSTU
EOPCRUMST
CPSRMTOEU
UMRSOTEPC
POCSUTMER
URTMOESPC
UPCRTOSME
PREUOSTCM
TROEUSCPM
MOUPRSTEC
PCOETURMS
PUOSTERCM


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.
Once the loop has been started, changing the variables holding the startValue, stopValue or increment value will not affect loop execution.

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.
grymmjack (gj!)
GitHubYouTube | Soundcloud | 16colo.rs
Reply


Messages In This Thread
RE: Why do FOR/NEXT variables end up +1 more? - by grymmjack - 08-24-2023, 03:00 PM



Users browsing this thread: 6 Guest(s)