FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   is it possible to break out a for() loop?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: is it possible to break out a for() loop?  (Read 531 times)
lunetta

7200475272004752 WWW Email
is it possible to break out a for() loop?
« on: May 8th, 2004, 10:55pm »

hello all
 
that's me, bothering the syntax forum again...
 
I was watching the squirrels mating in the park and wondered "Is it possible, once I reach a value that I want, to simply break out a for() loop?"
 
 example:
I'm scanning the screen searching a certain color, using two nested for() loops; if I find this certain color, I want to stop the scan, it's a waste of electricity to continue; I was setting a flag like "if this color is seem, raise the flag", and after the loop ended, I had my information. That's not an optimized solution...
 
Is it good to nest two while() loops to scan the screen and gather color information? What would be the fastest option?
 
thanks!
 
TomC

WWW
Re: is it possible to break out a for() loop?
« Reply #1 on: May 9th, 2004, 12:12am »

There is a keyword 'break' but I always get it wrong
 
One easy way is to add another condition in your loop.
 
Code:

boolean gotOne = false;
for (int i = 0; i < pixels.length && !gotOne; i++) {
  gotOne = (pixels[i] == #000000);
}
if (gotOne) println("at least one black pixel");
 
lunetta

7200475272004752 WWW Email
Re: is it possible to break out a for() loop?
« Reply #2 on: May 9th, 2004, 12:33am »

hey, that's smart! it works!
Thanks!
 
mKoser

WWW Email
Re: is it possible to break out a for() loop?
« Reply #3 on: May 9th, 2004, 1:38am »

well if you wanna use break to avoid running through the entire loop, you can do it like this:
 
Code:

void setup(){
}
 
void draw(){
  int r = (int)random(1000);
  for(int i=0; i<1000; i++){
    if(i == r){
 println("FLAG!..." + r);
 break;
    }
    println(i);
  }
}

 
+ mikkel
 

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
lunetta

7200475272004752 WWW Email
Re: is it possible to break out a for() loop?
« Reply #4 on: May 9th, 2004, 3:23am »

That's great; I couldn't find reference for the break command before, so I didn't know how to use properly.
Thanks too!
 
narain


Re: is it possible to break out a for() loop?
« Reply #5 on: May 9th, 2004, 6:26am »

If you have nested loops, break will only break out of the innermost one by default.
 
I'm guessing you'll need to break all the loops, right? For that, you need a labeled break.
 
For example:
Code:
boolean foundIt = false;
search:
for (int x = 0; x < width; x++) {
  for (int y = 0; y < height; y++) {
    // ... is this what you're looking for? ...
    if (thisIsIt) {
 foundIt = true;
 break search;
    }
  }
}
 
skloopy

WWW
Re: is it possible to break out a for() loop?
« Reply #6 on: May 9th, 2004, 6:43am »

The Java tutorial on sun's site is where I learned about all that stuff
 
here's the part on break and continue and loops and stuff
 
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/flow.html
 
Edit: Oops! didn't see that was linked before
« Last Edit: May 9th, 2004, 6:45am by skloopy »  
lunetta

7200475272004752 WWW Email
Re: is it possible to break out a for() loop?
« Reply #7 on: May 9th, 2004, 4:23pm »

oh yeah, labeled break, that's all I ever asked for. Thanks for all responses!
 
fry


WWW
Re: is it possible to break out a for() loop?
« Reply #8 on: May 9th, 2004, 4:32pm »

fwiw, using labels is a major programming no-no. you should almost always never ever use them.. for as long as i've been writing code in java (since ~1995) i've never had to use one.  
 
it's mostly because it makes the code really difficult to read, and is considered bad practice. you should go out of your way to stick with the regular structures like loops and whatnot. using 'break' is fine, and a necessity, it's just that the labels are a weird territory.
 
lunetta

7200475272004752 WWW Email
Re: is it possible to break out a for() loop?
« Reply #9 on: May 9th, 2004, 4:55pm »

oops; the labeled break is returning a "invalid type 22" ... what can this be?
 
Edit: I just read Fry response... the territory is so weird that Sun's examples are not working... let me get out of here!  
 
« Last Edit: May 9th, 2004, 4:57pm by lunetta »  
narain


Re: is it possible to break out a for() loop?
« Reply #10 on: May 10th, 2004, 8:02am »

Um, yeah, "Goto considered harmful" and all that. I agree it's important to write easily readable code.
 
But how do you break out of nested loops?
A simple label on the outermost loop seems more elegant and more readable to me than checking a boolean in each loop condition.
 
But if Processing deems it illegal, the point's rather moot; anyway, it's not a real big issue.
 
TomC

WWW
Re: is it possible to break out a for() loop?
« Reply #11 on: May 10th, 2004, 10:57am »

To avoid the break construct all together (as I do), you could write a small function and use the return to break out of the nested loops.
 
Here's what I'd do instead of your labelled break:
 
Code:

boolean isItThere() {
  for (int x = 0; x < width; x++) {  
    for (int y = 0; y < height; y++) {  
 // ... is this what you're looking for? ...  
 if (thisIsIt) {  
   return true;  
 }  
    }  
  }  
  return false;
}

 
You could of course return an int (e.g. the index of the first black pixel), or whatever, using this method.
 
« Last Edit: May 10th, 2004, 10:58am by TomC »  
Pages: 1 

« Previous topic | Next topic »