We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Noob: How is this an infinite loop
Page Index Toggle Pages: 1
Noob: How is this an infinite loop? (Read 857 times)
Noob: How is this an infinite loop?
Aug 17th, 2009, 9:58pm
 
I commented out the part that makes this an infinite loop and causes processing to hang.

I can see how if I move my mouse to x=0 an infinite loop will be created. But as soon as I move my mouse to the right I think the exit condition will soon be met.

The book I am reading says no.

mouseX and mouseY are updated with new values at the beginning of each cycle through draw().

What don't I understand? Thanks.

int y = 80;        
int x = 0;        
int spacing = 10;  
int len = 20;      
int endLegs = 150;

void setup() {
 size(200,200);
}

void draw() {
 background(0);
 stroke(255);
 x = 0;

//spacing = mouseX / 2;
 while (x <= endLegs) {
   line(x,y,x,y + len);

   x = x + spacing;
 }
}


Re: Noob: How is this an infinite loop?
Reply #1 - Aug 17th, 2009, 10:14pm
 
When the sketch starts up, mouseX == 0.

That means that on the first call to draw(), zero is added to x each time through the loop and will never finish.

Perhaps add a condition that will only loop if mouseX is greater than zero?
Re: Noob: How is this an infinite loop?
Reply #2 - Aug 17th, 2009, 10:32pm
 
Use this spacing formula instead.

Code:
spacing = (mouseX / 2)+1; 



Re: Noob: How is this an infinite loop?
Reply #3 - Aug 17th, 2009, 10:58pm
 
If I understand you correctly; Even when I move the mouse to the right, those numbers just are not recognized so I have to manually keep it from the edge from the very start.

Thanks for the code suggestion.
spacing = (mouseX / 2)+1;
Re: Noob: How is this an infinite loop?
Reply #4 - Aug 18th, 2009, 5:41am
 
Quote:
Even when I move the mouse to the right, those numbers just are not recognized


That's not the point.

The first time draw() is called, at the very beginning of the sketch, x==0 and mouseX==0. As a consequence spacing == mouseX/2 == 0. Moreover (x <= endLegs) is true, and you get stuck in the while loop, because "x = x + spacing" is equivalent to "x = 0 + 0" and (x <= endLegs) will never be false.

Processing won't go further : nothing will be drawn on screen, and draw() will never be called again. You're just stuck in the while loop.
Re: Noob: How is this an infinite loop?
Reply #5 - Aug 18th, 2009, 12:00pm
 
If I understand this correctly; Processing is designed so the first frame drawn has to have something to draw on the window otherwise processing dose not draw the second frame or any frame there after. It just goes into an endless loop with a blank screen.

Thanks for your help.
Re: Noob: How is this an infinite loop?
Reply #6 - Aug 18th, 2009, 12:32pm
 
No. Sorry if I haven't explained it well. This is what Processing does :

step 1 : check if some variables have been declared
step 2 : call setup()
step 3 : call draw()
step 4 : wait a bit and go to step 3

In your case, the problem is that you'll never reach step 4, because you're stuck in the while() loop (which is infinite because x cannot get over endLegs). Since you're stuck, you cannot reach the end of the draw() function, and cannot go to step 4.
Re: Noob: How is this an infinite loop?
Reply #7 - Aug 18th, 2009, 3:41pm
 
If I understand this correctly; On the first frame, x has to get over endLegs or processing will not go to step 4 which is to draw frame 2 of the 30 or so frames a second. Like the frames (or pictures of a movie or television that makes up the flickering images we see on the monitor).

Adding the + 1 to (spacing = (mouseX / 2) + 1;) makes the while loop complete itself all within the first frame which is step 3 : call draw() before going to step 4 which is to call draw() again for the next frame to paint or draw() on.
Re: Noob: How is this an infinite loop?
Reply #8 - Aug 19th, 2009, 6:30am
 
Lenticular wrote on Aug 18th, 2009, 3:41pm:
If I understand this correctly; On the first frame, x has to get over endLegs or processing will not go to step 4 which is to draw frame 2 of the 30 or so frames a second. Like the frames (or pictures of a movie or television that makes up the flickering images we see on the monitor).


x has to get over endLegs on every frame, otherwise the program will be stuck in the while loop.  When you set up a loop you must ensure that the condition will get triggered at some point.


Lenticular wrote on Aug 18th, 2009, 3:41pm:
Adding the + 1 to (spacing = (mouseX / 2) + 1;) makes the while loop complete itself all within the first frame which is step 3 : call draw() before going to step 4 which is to call draw() again for the next frame to paint or draw() on.


If mouseX only updates once a frame then the draw loop has to complete at least once before mouseX updates from the starting value of 0.  For the draw loop to complete the first frame, the while loop has to complete.  In your original code the while loop would only complete if mouseX was greater than 0, which wouldn't happen because you couldn't get beyond the first frame...  i.e. it would never complete, you wouldn't pass Go and wouldn't collect £200...

By including the "+1" the condition in the while loop will be triggered, since each repetition of the while loop will add 1 to x, meaning it will eventually be greater than endLegs, even if mouseX=0.

And note that the while loop will complete each and every frame - it has to in order for Processing to move on to the next frame...


Re: Noob: How is this an infinite loop?
Reply #9 - Aug 19th, 2009, 2:25pm
 
Thanks for elaborating this intricate subject so I can understand.
Page Index Toggle Pages: 1