Page 1 of 2
Loop ? The behavior not working
Posted: Wed Jun 05, 2024 4:53 pm
by threedslider
Hi
I do the test for loop and the behavior not working thought, for example :
Code: Select all
; Is it a bug ?
n.i = 0
For y=0 To 5
For x = 0 To 5
For n=0 To 2
Debug n
Next
If n = 2
Break
EndIf
Next
If n = 2
Break
EndIf
Next
So you can confirm it is a bug or not ?
I do same in C language and it works fine for the function "break".
Thanks.
PS : I use the compiler from PB 6.10 x64
Re: Loop ? The behavior not working
Posted: Wed Jun 05, 2024 5:16 pm
by Paul
Code: Select all
; Is it a bug ?
n.i = 0
For y=0 To 5
For x = 0 To 5
For n=0 To 2
Debug n
Next
Debug "***" + n ;<<<< check with this
If n = 2
Break
EndIf
Next
If n = 2
Break
EndIf
Next
According to the value of "n" after you leave the For/Next loop, the condition "If n=2" will never be met so you won't get your "Break"
Re: Loop ? The behavior not working
Posted: Wed Jun 05, 2024 5:25 pm
by Caronte3D
I don't know if it's a bug, but when the for loop ends, its index is incremented by 1.
Re: Loop ? The behavior not working
Posted: Wed Jun 05, 2024 5:42 pm
by threedslider
@Paul : Strange or is it C language like bug !? Or it is differently from PB but work it

Because n is global so the conditional can "see" the number 2 and then break as do in C, right ?
@Caronte3D :

Re: Loop ? The behavior not working
Posted: Wed Jun 05, 2024 7:22 pm
by Caronte3D
Seriously... I think the loop variable should have the maximum value of the Next loop when it finishes.
Thinking logically
Re: Loop ? The behavior not working
Posted: Wed Jun 05, 2024 7:51 pm
by Comfort
It's not broken... it's just quirky.
Re: Loop ? The behavior not working
Posted: Wed Jun 05, 2024 8:01 pm
by Demivec
The value of the loop control variable is not defined outside of the loop itself (i.e. after exiting).
Re: Loop ? The behavior not working
Posted: Wed Jun 05, 2024 9:20 pm
by STARGÅTE
The For-loop is a head controlled loop.
That means, n is increase by 1 as long as n <= 2, so the last iteration is when n = 2, increased by n + 1, and n (3) <= 2 fails.
Re: Loop ? The behavior not working
Posted: Wed Jun 05, 2024 11:24 pm
by normeus
Maybe the fact that 2 is used to end the loop in C is what's confusing:
Code: Select all
#include <stdio.h>
int main()
{
int i = 0;
printf("Hello World of loops\n");
for(i=0;i<2;i++){
printf("in loop i-> %i \n",i);
}
printf("loop done i-> %i \n",i);
return 0;
}
C code only loops 2 times then exits when it hits 2 ( prints 0 and 1)
your PB code loops 3 times ( prints 0, 1, 2 )
Norm
Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 1:31 am
by mk-soft
Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 7:18 am
by threedslider
So I do this right ?
Code: Select all
n.i = 0
For y=0 To 5
For x = 0 To 5
For n=0 To 1
Debug n
Next
If n = 2
Break
EndIf
Next
If n = 2
Break
EndIf
Next
It seems work but strange after all

Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 7:20 am
by threedslider
Comfort wrote: Wed Jun 05, 2024 7:51 pm
It's not broken... it's just quirky.
Yes it is very quirky

Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 7:22 am
by threedslider
STARGÅTE wrote: Wed Jun 05, 2024 9:20 pm
The For-loop is a head controlled loop.
That means, n is increase by 1 as long as n <= 2, so the last iteration is when n = 2, increased by n + 1, and n (3) <= 2 fails.
Ok thanks, see above my code

Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 7:55 am
by Demivec
@threedslider: The important question is, why would you need the value of the loop control variable after the loop? If you are using the variable as a check to see whether a condition was met during the loop you could set a status variable to either #True or #False.
Your code examples don't really provide enough detail to recommend a more detailed solution. At the very least, pseudo-code outlining the purpose of the loops and the basis of their range of values would be very helpful.
A simple recommendation with your last approach is that if there were no commands before breaking out of the outer loop you could then just use 'Break 2' to leave both the inner loop and outer loop if n equals 2.
@Edit: corrected the spelling of threedslider's alias
Re: Loop ? The behavior not working
Posted: Thu Jun 06, 2024 8:23 am
by threedslider
Demivec wrote: Thu Jun 06, 2024 7:55 am
@threeslider: The important question is, why would you need the value of the loop control variable after the loop? If you are using the variable as a check to see whether a condition was met during the loop you could set a status variable to either #True or #False.
Your code examples don't really provide enough detail to recommend a more detailed solution. At the very least, pseudo-code outlining the purpose of the loops and the basis of their range of values would be very helpful.
A simple recommendation with your last approach is that if there were no commands before breaking out of the outer loop you could then just use 'Break 2' to leave both the inner loop and outer loop if n equals 2.
Sorry if it is not useful but...
Well I do for small test to understand with loop, and it is somewhat confusing for me because I do my project for Raytracing in one week end, I don't want to put the whole source code here

So to simplify I have a quirky behavior in my code from Raytracing, dunno if it is me or PB

Can we have both the behavior to "<" and "<=" in PB ?
It seems for loop in PB with "To" has a behavior to "<=", right ? And when I do as my code in Raytracing is another story (now it is "If" and "ElseIf" problem ... I want to the same in loop for similar behavior but not working)