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

Loop ? The behavior not working

Post 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
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Loop ? The behavior not working

Post 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"
Image Image
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Loop ? The behavior not working

Post by Caronte3D »

I don't know if it's a bug, but when the for loop ends, its index is incremented by 1.
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Loop ? The behavior not working

Post by threedslider »

@Paul : Strange or is it C language like bug !? Or it is differently from PB but work it :lol: Because n is global so the conditional can "see" the number 2 and then break as do in C, right ?

@Caronte3D : :lol:
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Loop ? The behavior not working

Post by Caronte3D »

Seriously... I think the loop variable should have the maximum value of the Next loop when it finishes.
Thinking logically
Comfort
User
User
Posts: 32
Joined: Thu Jul 05, 2018 11:52 pm

Re: Loop ? The behavior not working

Post by Comfort »

Code: Select all

For n=0 To 2
  n=10
Next
Debug n
It's not broken... it's just quirky.
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Loop ? The behavior not working

Post by Demivec »

The value of the loop control variable is not defined outside of the loop itself (i.e. after exiting).
User avatar
STARGÅTE
Addict
Addict
Posts: 2260
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Loop ? The behavior not working

Post 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.
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
normeus
Enthusiast
Enthusiast
Posts: 475
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Loop ? The behavior not working

Post 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
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Loop ? The behavior not working

Post by mk-soft »

Code: Select all

for(i=0;i<=2;i++){
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Loop ? The behavior not working

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

Re: Loop ? The behavior not working

Post by threedslider »

Comfort wrote: Wed Jun 05, 2024 7:51 pm

Code: Select all

For n=0 To 2
  n=10
Next
Debug n
It's not broken... it's just quirky.
Yes it is very quirky :shock:
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Loop ? The behavior not working

Post 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 :)
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Loop ? The behavior not working

Post 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
Last edited by Demivec on Thu Jun 06, 2024 9:52 am, edited 1 time in total.
threedslider
Enthusiast
Enthusiast
Posts: 434
Joined: Sat Feb 12, 2022 7:15 pm

Re: Loop ? The behavior not working

Post 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 :shock: 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)
Post Reply