Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 21st, 2006, 6:28 PM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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.
Eric the Red is offline   Reply With Quote
Old May 21st, 2006, 6:35 PM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 311
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
	myBreakLabel:
		for (int x = 0; x < 10; x++)
		{
			for (int y = 0; y < 10; y++)
			{
				if (array[x][y].getState == true)
				{
					break myBreakLabel;
				}
			}
		}
andro is offline   Reply With Quote
Old May 21st, 2006, 6:37 PM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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
Jimbo is offline   Reply With Quote
Old May 21st, 2006, 6:40 PM   #4
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Well.. What would an expert java programmer do in this situation?
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old May 21st, 2006, 6:47 PM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
I can't say for sure what an "expert" would do, but I'd use the label
Jimbo is offline   Reply With Quote
Old May 21st, 2006, 6:59 PM   #6
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old May 21st, 2006, 7:07 PM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old May 21st, 2006, 9:33 PM   #8
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
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
Booooze is offline   Reply With Quote
Old May 21st, 2006, 10:32 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 21st, 2006, 10:47 PM   #10
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 311
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
A third solution:

x=10;
y=10;
andro is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC