Page 1 of 1
[no need] For/Next: Set the countvariable to the last value
Posted: Sat Jul 10, 2010 10:40 pm
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]
Re: For/Next: Set the countvariable to the last value
Posted: Sat Jul 10, 2010 11:20 pm
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
Re: For/Next: Set the countvariable to the last value
Posted: Sat Jul 10, 2010 11:27 pm
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.
Re: For/Next: Set the countvariable to the last value
Posted: Sat Jul 10, 2010 11:40 pm
by Kurzer
Code: Select all
j = 10
For i = 10 To 0 Step - 1
j = i
Next
Debug "Last value of loop: " + Str(j)

Amazing!
Well now I understand. I never thought about a LCV at this level of detail.
Thank you Demivec and Stargate.
Re: For/Next: Set the countvariable to the last value
Posted: Sun Jul 11, 2010 3:36 am
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.