• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

difference between continue and break

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody helps me with that with an example which has both continue and break keywords.
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

A break example,

for ( int i = 0; i < 10; i++ )
{
if ( i == 5 )
{
System.out.println( "Will get out of loop" );
break;
}

System.out.println( "Still In loop" );
}

A continue example,

for ( int i = 0; i < 10; i++ )
{
if ( i <= 5 )
{
continue;
}

System.out.println( "i is larger than 5" );
}

In other words, using break is just like saying "Please get out of the loop now !!!" and using continue is just like saying "Please move on to the next iteration now !!!".

I hope this makes everything clear.

Best of luck ...
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still not clear I just want to know form which loop it will come out. Could you please explain me.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Divya,


public static void main(String[] args)
{
for ( int i = 0; i < 10; i++ )
{
if ( i == 5 )
{
System.out.println( "Will get out of loop" );
break;
}

System.out.println( "Still In loop" );
}
System.out.println("Break encountered! Out of loop");//The execution will start from this line when break is encountered.

}


I have just added few lines to vassilis code.In the above program when i is equal to 5 the execution comes out of the for loop and prints Break encountered! Out of loop.


Regards
Nik
[ June 22, 2007: Message edited by: nik arora ]
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It breaks or continues the most inner loop, for example :



Now, the story will be a bit different when using labels with loops, as an example :


I hope everything is clear now.

Best of luck ...
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thank you very much for clearing me the diffrence between break nad continue . As I am preparing for SCJP1.5 and planning to give exam in august Could you please help me this as You have already cleared this. Please help me. I will be grateful for your valuable guidence . I should give empahsis in which topics so that I can clear in first attempt.
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think there are special weightages on marks for individual topics, questions appear in random fashion... may be threads of this kindmay be of some help always.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Break and continue more graphical:
http://www.ginko.de/user/burki/ranch/Break_Continue.gif
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for giving the link for graphical representation of break n continue but the images are not clear in the provided gif. I will be grateful if you could provide a clearer picture.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice visualization, Burkhard.

Divya, why don't you describe to us what you think is happening (and what excatly is not clear in that image), and we'll chip in with more explanations when you get stuck?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divya, "break" is valid only inside a loop or a switch. And a "continue" is only inside aa loop ( for and while). remember 'if' isnt a loop. for ( int i = 0; i < 10; i++ )
{
if ( i == 5 )
{
System.out.println( "Will get out of loop" );
break;
} In this case, the control goes out of the for loop when it encounters break;

System.out.println( "Still In loop" );
}

A continue example,

for ( int i = 0; i < 10; i++ )
{
if ( i <= 5 )
{
continue;
}

System.out.println( "i is larger than 5" );
}
In this case, when i<=5, it returns back to the top skipping the println statement in the current loop.
 
For my next trick, I'll need the help of a tiny ad ...
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic