Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is wrong with this for/next loop
#1
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.
Reply
#2
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.
Reply
#3
(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
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.

Agreed. I first saw this happen with Commodore v 2.0. BASIC. This is how the FOR loop operates in BASIC
______________________________
I'm with you fellers
Reply
#4
Quote:This is how the FOR loop operates in BASIC
IMHO 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)

Rem this demo showes how FOR  works
Screen 0
While 1
    Cls , (Rnd * 6) + 1
    Locate 1, 1
    Print "Press 1 for Original FOR-NEXT "
    Print "Press 2 for GOTO version of FOR-NEXT"
    Print "Press 0 to quit program"
    a$ = Input$(1)
    If a$ = "0" Then
        End
    ElseIf a$ = "1" Then
        OriginalFORNEXT
    ElseIf a$ = "2" Then
        GOTODemoFORNEXT
    End If
    Color 31, 9: Print "Press a key for continue": Sleep: _KeyClear: Color 15, 0
Wend
End
Sub OriginalFORNEXT ()
    Dim counter As Integer
    counter = 0
    Print "Counter before FOR"; counter
    Print " Counter in the loop of FOR"
    For counter = 1 To 5 Step 1
        Print counter
    Next counter
    Print " Counter after loop FOR"; counter
End Sub
Sub GOTODemoFORNEXT ()
    Dim counter As Integer
    counter = 0
    Print "Counter before FOR loop"; counter
    Print "Counter in the loop of FOR"
    nextTest:
    counter = counter + 1 ' the STEP of loop FOR-NEXT is set to 1
    If counter <= 5 Then
        Print counter
        GoTo nextTest
    End If
    Print "Counter after loop FOR"; counter
End Sub

here a screenshot of output using VScode
[Image: FOR-loop-demo.jpg]

and here a example in C
compiled with g++


[Image: FOR-Loop-in-C.jpg]
Reply
#5
(04-14-2025, 01:00 PM)Helium5793 Wrote: 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.
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."

[Image: For-Schleife-QB64-C.jpg]
Reply
#6
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.  Wink
Reply
#7
This can be prevented, for example, in this way:

Code: (Select All)

'For-Schleife und Ausgabe des letzten Wertes - 15. April 2025

Option _Explicit

Dim As Integer i, j

Print
For i = 1 To 5
  Print i
Next

Print
Print i


Print: Print "------------------------"
Print
For j = 1 To 5
  Print j
  If j = 5 Then
    'Keine Zuweisung mehr
    Exit For
  End If
Next

Print
Print j

End
Reply




Users browsing this thread: 1 Guest(s)