Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

break vs continue

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading the RHE book right now.
I am a bit confused over the difference between a break and continue.
I understand that continue will cause you to break out of a loop (possibly to a place designated be a label).
Breaks seem to be essentially the same thing? Also, what is the scope of a break? For example:
for (int i=0; i<10; i++){
doSomething();
for (int j=0; j<i; j++){>
if (j=2){
break
}
doSomethingElse();
}
}
What happens with the break? Do you do the next iteration of i?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kris
Continue will not break you out of the loop, it will take you back to the loop test and try to run the next iteration. While break will take you completley out of the loop.
For a detailed explaination and samples check out the JLS section 14.14

hope that clears it up for you

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kris
The basic difference between continue and break is as follows:
continue takes the control to the first statement of the corresponding loop whereas the break statement exits from the loop.
Your programme is got many errors and so i have written my program modifying urs..which goes as follows
import java.io.*;
public class Test
{
public static void main(String args[])
{
for(int i=0;i<10;i++)
{
//doSomething();
System.out.println("Entered the outer loop");
for(int j=0;j<2;j++){ // for loop to be executed perfectly
if(j==0){ // requires boolean condition
break;
}
//doSomethingElse();
System.out.println("Entered the inner loop");
}
}
}
}
Here the if condition tests for value of j equal to o and then exits since it comes across break statement. Hope this example solves your doubt.
Regards
venu

 
Kris Decker
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for teh swift reply. I havent had the cnahce to read the JLS section yet, but I think I have a good grasp on teh difference between break and continue.
However, one question remains:
if there is a break within a nested loop, does it break out of ONLY the inner loop--or doest it break out of the inner and they outer loops (assuming there are two loops)?

Originally posted by venugopal askani:
Hi Kris
The basic difference between continue and break is as follows:
continue takes the control to the first statement of the corresponding loop whereas the break statement exits from the loop.
Your programme is got many errors and so i have written my program modifying urs..which goes as follows
import java.io.*;
public class Test
{
public static void main(String args[])
{
for(int i=0;i<10;i++)
{
//doSomething();
System.out.println("Entered the outer loop");
for(int j=0;j<2;j++){ // for loop to be executed perfectly
if(j==0){ // requires boolean condition
break;
}
//doSomethingElse();
System.out.println("Entered the inner loop");
}
}
}
}
Here the if condition tests for value of j equal to o and then exits since it comes across break statement. Hope this example solves your doubt.
Regards
venu


 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
break- it will break only from the loop in which this stmt is.
for(..)
{
for(..)
{
if(condition) break;
}
}
-----
If u hav two loop say out and in
out:for(..)
{
in: for(..)
{
if(condition) break out;
}
}
only now u can break outer loop.Even if u hav more than 2 loops
with lable u can break,whichever loop u want.
Hope this helps,
(sorry my explanation is not good,i am in hurry now)
 
Well THAT's new! Comfort me, reliable tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic