Sign in to follow this  

what's my problem?

This topic is 4742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i] != 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

Share this post


Link to post
Share on other sites
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[i] != null)
return this.coordsMatch(blocks[n].getCoords())
}
return false;
}

Share this post


Link to post
Share on other sites
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[i] != 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[i] != null)
{
if (this.coordsMatch(blocks[n].getCoords()))
{
return true;
}
}
}
return false;
}


Jim.

Share this post


Link to post
Share on other sites
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[i] != 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]

Share this post


Link to post
Share on other sites
In stormrunner's code, you want to pull the 'return false;' outside of the loop, of course.

Oh, I assume the "blocks[i]" 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 class
public boolean isAt(int x, int y) {
// something like this...
return this.coords.equals(new Point(x,y));
}

// Checking code
public 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
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this