Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why do FOR/NEXT variables end up +1 more?
#2
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)

For t = 1 To 10.5 Step 1
Next
Print t ' What should t be?

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.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  _NEWIMAGE can't accept variables as dimensions? bobalooie 23 990 02-18-2026, 11:16 PM
Last Post: Unseen Machine
  Why are SINGLE variables faster than INTEGER variables ? Elzaimer 18 2,364 07-07-2025, 05:01 AM
Last Post: ahenry3068
  QB64's TYPE / END TYPE inquiry MystikShadows 2 904 06-30-2024, 02:34 PM
Last Post: MystikShadows
  Why does my Loop end after 11 Loops? Dimster 69 14,261 02-13-2023, 10:18 PM
Last Post: mnrvovrfc

Forum Jump:


Users browsing this thread: 1 Guest(s)