what's my problem?

Started by
7 comments, last by Zahlman 19 years, 4 months ago
I THINK my question is: how do you break from a 'for' loop? But here is an explanation of the problem. Avoiding overly-detailed explanation of my program, what i essentially want to do in this particular method is go through an array of blocks and check if the coordinates match the given ones. If they do, I want to break from the 'for' loop and return the current answer, but there is an obvious problem and maybe i'm just too brain-dead to see it, but help me out here:

public boolean blockMade(int x, int y) {
	Point loc = new Point(x, y);
	boolean needBlock = false;
	for (int n = 0; n < blocks.length; n++) {
	    if (blocks != null) {
		if (this.coordsMatch(blocks[n].getCoords())) {
		    needBlock = true;
		} else {
		    needBlock = false;
		}
	    }
	}
	return needBlock;
    }
/** see what i'm TRYING to do at least? While looking through the array of blocks, as soon as the coordinates match, I want to break from the loop and have it return true. How should I do this? Or is there a different method entirely? thanks much guys, it's great to have a place like this where i can just throw out questions that concern me. Gbizzle
Advertisement
eek, that code lost all its spacing....btw, how do you get the code to space out right? can i attach a text file or something?
Quote:Original post by gbizzle
eek, that code lost all its spacing....btw, how do you get the code to space out right? can i attach a text file or something?


GDNet Forums FAQ
I fixed your post for you.

Quote:I THINK my question is: how do you break from a 'for' loop?

What language are you using?

Would this suit you?

public boolean blockMade(int x, int y) {	Point loc = new Point(x, y);	for (int n = 0; n < blocks.length; n++) {	    if (blocks != null)                 return this.coordsMatch(blocks[n].getCoords())	}	return false;    }
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You want the break statement, like so (I'm assuming C++, but this actually looks like Java, in which case I might be wrong):

public boolean blockMade(int x, int y) { Point loc = new Point(x, y); boolean needBlock = false; for (int n = 0; n < blocks.length; n++) {  if (blocks != null) {   if (this.coordsMatch(blocks[n].getCoords())) {    needBlock = true;break;    } else {    needBlock = false;   }  } }return needBlock;}



Alternatively, if I'm interpreting what you're doing in the right way, you could use (at least in C++ - can you use multiple returns in Java?):
public boolean blockMade(int x, int y) { Point loc = new Point(x, y); for (int n = 0; n < blocks.length; n++)  {  if (blocks != null)   {   if (this.coordsMatch(blocks[n].getCoords()))    {    return true;   }   } }return false;}

Jim.
Quote:Original post by Fruny
What language are you using?

java, unless i'm much mistaken.
gbizzle, you would simply use a return statement, e.g.
public boolean blockMade(int x, int y) {	Point loc = new Point(x, y);	for (int n = 0; n < blocks.length; n++) {	    if (blocks != null) {		if (this.coordsMatch(blocks[n].getCoords())) {		    return true		}	    }            return false;	}

that way you don't need a boolean variable; you return true if the blocks match, otherwise you return false at the end of the function. please keep in mind i'm assuming the statement
if(this.coordsMatch(blocks[n].getCoords()))

being true is the condition that will exit the loop. forgive me if thats incorrect.

cheers.

<edit:: arg, beaten by JimPrice [smile]
- stormrunner
In stormrunner's code, you want to pull the 'return false;' outside of the loop, of course.

Oh, I assume the "blocks" should be "blocks[n]", and you don't seem to be using the Point object anywhere (should that "this" be "loc"?).

Oh, consider moving the checking into the block class itself, so you don't just use a raw accessor:

// in Block classpublic boolean isAt(int x, int y) {  // something like this...  return this.coords.equals(new Point(x,y));}// Checking codepublic boolean anyBlockAt(int x, int y) {  for (int n = 0; n < blocks.length; n++) {    if (blocks[n] != null) {      // Having this check is ugly too. Consider the Null Object pattern      if blocks[n].isAt(x, y) return true;    }  }  return false; // nothing matched}
Doesn't java also have a break label feature?

Something like

....
for(...) {
if(....) break doneloop;
}
doneloop:
...
thanks to all you guys for your help, and it was in Java by the way, i forgot to say at the beginning.

Anyway I also forgot to ask one more thing, once the block that matches the coordinates is found, I want to somehow return that block as well....how should i go about doing that? Just store it in a global variable and return it in another method?

thanks again

[Edited by - gbizzle on December 17, 2004 3:18:16 PM]
Quote:Original post by gbizzle
thanks to all you guys for your help, and it was in Java by the way, i forgot to say at the beginning.

Anyway I also forgot to ask one more thing, once the block that matches the coordinates is found, I want to somehow return that block as well....how should i go about doing that? Just store it in a global variable and return it in another method?

thanks again


Just return it. As the return value. If no block is found, return null.

This topic is closed to new replies.

Advertisement