Page 2 of 2
Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 8:50 am
by threedslider
Ok I am talking this problem in my thread from Raytracing in one week more deep if you want even if it is different, sorry
Here is my link :
https://www.purebasic.fr/english/viewt ... 77#p622277
Hope you guys can help this tricky one
Thanks
Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 9:32 am
by PBJim
In my experience, all the BASIC dialects I've worked with, exit the For/Next loop with the counter + 1. It is normal, for compiled and interpreted BASICs and PureBasic is not extraordinary in this sense.
It has a useful purpose — it is possible to determine if the loop exited naturally, or through a BREAK, since if the count is within the bounds of the loop, it indicates a BREAK.
Code: Select all
For a = 1 To 10
If a = 5
Break
EndIf
Next a
Debug a; * 5
Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 10:21 am
by boddhi
Sorry but I don't really understand the discussion because for all BASIC (i.e. since the very first origins) the For...Next loop has always worked like this:
Code: Select all
For n=x to y step z ; (z=1 if step absent)
; 1) If 1st call of For: Initialization of variable n by assigning value x
; 2) Compare variable n to y : Continue loop processing if n is < to y (for a positive step) or n > y (for a negative step) otherwise end of loop process.
[...]
Next
; 1) Increment n with z (n = n + z)
; 2) Return to For instruction
In conclusion, with the exception of a premature exit, n will always have the value Int(y / z) + z at loop exit, and therefore strictly different from y.
Code: Select all
For n=1 to 2
[...] ; without Break instruction !important;
Next
If n=2
[...]
EndIf
makes no sense, since n will never have the value 2 at loop exit.
So, after exit, the main way to know if the loop has reached the end of its process is to test whether the value of n is higher (for a positive step) or lower (for a negative step) than y.
Note: We can just regret that PB can't manage one thing (a shame ?

) that even old BASIC could : accept decimal value, variable or expression as a step value.
This can be a real huge handicap in some situations.
Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 11:19 am
by boddhi
PBJim wrote:
In my experience, all the BASIC dialects I've worked with, exit the For/Next loop with the counter + 1. [...]
Sorry for the redundancy, PBJim posted a reply while I was writing mine and I didn't see it. (French to English is a pain for me!

)
Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 12:21 pm
by AZJIO
I sometimes use the last element of a loop as the beginning of the next one when creating a menu. I hope it won’t break, since it won’t occur to anyone to restore the counter after the loop is completed.
Code: Select all
iniCountStr = 2 ; the number of lines in the ini file is unknown in advance
iniCountStr2 = 2 ; - // -
For i = 1 To iniCountStr
Debug i
Next
Last = i - 1
Start2 = i
Last2 = Last + iniCountStr2
For i = Start2 To Last2
Debug i
Next
; Select EventMenu()
; Case 1 To Last
; Case Start2 To Last2
; EndSelect
Re: Loop ? The behavior not working
Posted: Fri Jun 07, 2024 12:26 am
by threedslider
Ok ! I say you thanks to all and now my problem was solved
Please to see my project for 3D Rendering if you want :
https://www.purebasic.fr/english/viewto ... 14#p622314
Re: Loop ? compiler weir implementation
Posted: Fri Jun 07, 2024 5:12 am
by moricode
for a human understanding , this was confuse me long time
Code: Select all
For n=0 To 2
n=10
Next
Debug n
; expected n = 10 , bcos it tell me what it's value above , and 10 is greater then 2 , so the loop ends , the value is 10
For n=0 To 2
...
Next
; after loop ends ,should retain the last n value , not increments it , implementation error
; Expected Implementation
;-----------------------------------------
n=0
x=n
loopagain:
if x>2 : goto endloop:
n=x
....
user code
....
x=n+1
goto loopagain:
endloop:
the logic is like this
Re: Loop ? compiler weir implementation
Posted: Fri Jun 07, 2024 11:18 pm
by boddhi
moricode wrote:
for a human understanding , this was confuse me long time
Code: Select all
For n=0 To 2
...
Next
; after loop ends ,should retain the last n value , not increments it , implementation error
Let's go back to your code: In case no modification of its value is made between For and Next, how could n be incremented?
With For? For n=0 would mean that n would start at 1... not good!
So, the only solution is than Next havs this role and perform the incrementation task.
It's why
Code: Select all
For n=0 To 2
n=10
Next
Debug n
; expected n = 10 , bcos it tell me what it's value above , and 10 is greater then 2 , so the loop ends , the value is 10
n can never be worth 10 but 11 because Next equals n+1 i.e. : For n=... : n=10 : Next (10+1) so 11
Code: Select all
; Expected Implementation
;-----------------------------------------
n=0
x=n
loopagain:
if x>2 : goto endloop:
n=x
....
user code
....
x=n+1
goto loopagain:
endloop:
It's for this kind of loops than Repeat...Until (or While...Wend) have been created

Re: Loop ? compiler weir implementation
Posted: Sat Jun 08, 2024 3:25 am
by moricode
boddhi wrote: Fri Jun 07, 2024 11:18 pm
Code: Select all
; Expected Implementation
;-----------------------------------------
n=0
x=n
loopagain:
if x>2 : goto endloop:
n=x
....
user code
....
x=n+1
goto loopagain:
endloop:
It's for this kind of loops than Repeat...Until (or While...Wend) have been created
This is the compiler implementation and output in ASM and binary Exe in internal , means implement the For / Next work like this internally, and no need to increment n when not necessary .
so use For/Next will resulting n remain last value