![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Break statement
Does anyone know how to break out of a double loop?
I understand that by using "break;" you can jump out of a loop. But what happens when i'm running through a 2-d array and i want to break out of a double loop? Is there any simple way like "break, break;" or "break 2x" LOL? I could assign a boolean variable "true" then test whether it is true in the outer loop. example: for (y= 0; y < 10;y++)
{
for (x = 0; x < 10; x++)
{
if (arrayTwoDimension[x][y].getState = true)
{
blnBreak = true;
break;
}
}
if (blnBreak = true) // tedious
break;
}Note the test in the 'y loop' (blnBreak = true). Anyone know a quick fix for this problem? What should I be doing in this situation?
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
myBreakLabel:
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
if (array[x][y].getState == true)
{
break myBreakLabel;
}
}
} |
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
you could use the idea you proposed, or you might consider using a label. This is pretty much the goto of Java, so some people might not like it. There's a blurb on labels here. Remember that the label comes before the outer loop, but will break you out of it.
[edit:] doggone you, andro... :p |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Well.. What would an expert java programmer do in this situation?
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#5 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
I can't say for sure what an "expert" would do, but I'd use the label
|
|
|
|
|
|
#6 |
|
Sexy Programmer
|
Sounds like the best solution. I forgot about labels myself.
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#7 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
I don't know what the best way to do it, or what the expert way to do it would be, but normally I just use a boolean value like you proposed.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#8 |
|
Expert Programmer
|
I would use a boolean value. Never used a label before, but looks just as effective. If I knew about labels, I would probably use em'. Don't do much java anymore :p
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I would go either way. When it comes to goto, as in C, you can put the goto label in strange, non-related out-of-the-way places and really do some spaghetti code screwups. In the situation you're looking at, the break terminates the labeled construct, rather than branching TO the label. I see nothing wrong with it. Of course, I'm not a high-priest looking for acolytes to kiss my ring. I'm an acolyte looking for a high-priest to kiss my butt.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Professional Programmer
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|