• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

labelled break

 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then do it starts with i==0, all over again? Or just ++i like continue?
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I really need to know this. Somebody please help?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic