Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# labelled break

Cameron Park
Ranch Hand
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
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
Posts: 371
Then do it starts with i==0, all over again? Or just ++i like continue?

Cameron Park
Ranch Hand
Posts: 371

INDU, BALA
Ranch Hand
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
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