[no need] For/Next: Set the countvariable to the last value

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 670
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

[no need] For/Next: Set the countvariable to the last value

Post by Kurzer »

The For / Next loop ist really strange in PB.

When the For/Next loop is completely pass through, the counting variable is set to the "to-value" + 1

If you write:

Code: Select all

For i = 0 To 10
  ; Do something
Next i
The value of variable i will be 11 after exiting the For/Next loop.

Constructs like this one will beginners drive crazy

Code: Select all

  For i = 0 To 10
    Debug Str(i) + " / " + Str(j)
    For j = 0 To 10
    	; Do something other
    Next j
  Next i
For my understanding For/Next should set the counting value to the "to-value" after leaving the loop - in this example set to 10 instead of 11.

Edit: Edited the subject line to [no need]
Last edited by Kurzer on Mon Jul 12, 2010 9:15 pm, edited 2 times in total.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: For/Next: Set the countvariable to the last value

Post by STARGÅTE »

-1

The loop current work properly!
For: Next is a head-controlled loop

And it works like any head-controlled loop

Code: Select all

Define i.i = 0
While i <= 10
 i + 1
Wend
Debug i

If you want it otherwise, use Repeat: Unitl, or a break!

Code: Select all

Define i.i = 0
Repeat
 i + 1
Until i >= 10
Debug i

Code: Select all

Define i.i
For i = 0 To 10
 If i = 10 : Break : EndIf
Next
Debug i
the loop may not at all accept the to-value, because this would not always be reached!

Code: Select all

Define i.i
For i = 0 To 10 Step 50
Next
Debug i
10 here would be to issue false


AND, all other languages are work same like PB, here JS:

Code: Select all

<script language="JavaScript">
for(n=0;n<=10;n++) ;
alert(n);
</script>
11
Last edited by STARGÅTE on Sat Jul 10, 2010 11:27 pm, edited 1 time in total.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: For/Next: Set the countvariable to the last value

Post by Demivec »

kurzer wrote:For my understanding For/Next should set the counting value to the "to-value" after leaving the loop - in this example set to 10 instead of 11.
From my experience almost all programming languages do not define the value that the loop-counter-variable (LCV) should have after a loop exits. A beginner should never depend on the LCV being set to any particular value and an expert should likewise avoid doing so.

Just to show you what I mean, you say the LCV will equal the "to-value" + 1 after the loop exits, but look at this:

Code: Select all

For i = 10 To 0 Step - 1
  j = i
Next
Debug "Last value of loop: " + Str(j)
Debug "Value of LCV after loop: " + Str(i)
And if you have that one figured out what about this one:

Code: Select all

For i = 10 To 0 Step - 3
  j = i
Next
Debug "Last value of loop: " + Str(j)
Debug "Value of LCV after loop: " + Str(i)
As STARGÅTE pointed out the loop may not even be executed.

The LCV is for the loop only and not for use after the loop without setting its value explicitly. Of course beginners and non-beginners alike will still attempt to do so but it's not a good idea.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 670
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: For/Next: Set the countvariable to the last value

Post by Kurzer »

Code: Select all

j = 10
For i = 10 To 0 Step - 1
  j = i
Next
Debug "Last value of loop: " + Str(j)
:shock: Amazing!

Well now I understand. I never thought about a LCV at this level of detail.
Thank you Demivec and Stargate.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: For/Next: Set the countvariable to the last value

Post by PB »

> When the For/Next loop is completely pass through, the counting variable is set to the "to-value" + 1

Yep, just like in every other Basic that I've used. PureBasic is no different.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply