aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes labelled break Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "labelled break" Watch "labelled break" New topic
Author

labelled break

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I have a question about labelled break.
mainloop: for(int i = 0; i < array.length; i++){
for(int j = 0; j < array[i].length; j++){
if(array[i].secondArray[j]=='\u0000'){
break mainlooop;
}
}
}
does the flow of the program leaves the "j for loop" and go to the "i for loop" with i incremented? If that is the case, what's the difference between a labelled continue and a labelled break?

mainloop: for(int i = 0; i < array.length; i++){
for(int j = 0; j < array[i].length; j++){
if(array[i].secondArray[j]=='\u0000'){
continue mainlooop;
}
}
}
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
A break with a label breaks the entire loop. So it breaks out of the i and j loop. continue label; begins in the mainloop again:

[This message has been edited by Mathias P.W Nilsson (edited August 18, 2001).]
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Then do it starts with i==0, all over again? Or just ++i like continue?
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I really need to know this. Somebody please help?
INDU, BALA
Ranch Hand

Joined: Jun 07, 2001
Posts: 48
Hi Cameron,
Just I have changed Nilsson's code little bit and executed,
go thru this code and its output
code 1:



The output is
j: 0 i: 0
j: 1 i: 0
breaking mainloop

If you replace break mainloop with continue mainloop the output will be like this
j: 0 i: 0
j: 1 i: 0
continuing mainloop
j: 0 i: 1
j: 1 i: 1
continuing mainloop
j: 0 i: 2
j: 1 i: 2
continuing mainloop

Hope this helps
Indu

Ernest Lee
Ranch Hand

Joined: Aug 20, 2001
Posts: 145
break label works like this:
here:
for (;
for (;
break here; //line A
//line B
when program reach line A, it will first go to "here: " and then go the line B.. (skip things in between). Try to put "continue here;" instead of "break here;" and u will enter an infinite loop! haha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: labelled break