Loop ? The behavior not working

Just starting out? Need help? Post your questions and find answers here.
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Loop ? The behavior not working

Post 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 :lol:

Thanks
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Loop ? The behavior not working

Post 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
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Loop ? The behavior not working

Post 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.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Loop ? The behavior not working

Post 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! :D )
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Loop ? The behavior not working

Post 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
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Loop ? The behavior not working

Post by threedslider »

Ok ! I say you thanks to all and now my problem was solved 8)

Please to see my project for 3D Rendering if you want : https://www.purebasic.fr/english/viewto ... 14#p622314
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

Re: Loop ? compiler weir implementation

Post 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


boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Loop ? compiler weir implementation

Post 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 :wink:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

Re: Loop ? compiler weir implementation

Post 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 :wink:

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