• 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

break & continue

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
can anybody please please tell me as to when these
two can be used.
is it right to write
for(int i = 0; i<3; i++){
continue;//or break;
System.out.println("hi all");
}
please ans this
[This message has been edited by shilpa gupta (edited March 02, 2001).]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,the code can not be compiled. Because the line
System.out.println("hi all");
will never be executed.

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shilpa,
You use <code>continue</code> when you want a loop to jump to the next iteration.
For example,

The <code>continue</code> statement causes iteration '3' to be short-circuited.
You use <code>break</code> when you want to jump out of the loop completely.
Example

Execution of the loop stops when the <code>break</code> is executed.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited March 03, 2001).]
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Well the reply of jane will make clear the use of
break & continue but be aware of the following code
for(int i=0;i<=10;++i)
{
try
{
if(i==2)
continue;
System.out.println(i);
}finally
{
System.out.println("oo God Finally I am in Picture");
}
}//End of For
Try the o/p of this code Cause it dosent follow the
Rule of continue
i.e continue says forget abt the following stamt. &
continue with next value of i(in for loop)
but in this case it will first print message of finally
& then continue the loop;
same is the case with following code
public void method()
{
int i=4;
try
{
if(i==4)
return;
}
finally
{
System.out.println("I am Printed heeeeee");
}
}
can u guess the o/p well The stmt of finally will be first
printed & then control will be returned.
this question i faced in my exam.
....praful
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
For all exits(Note this) from a try block, except those due to exceptions, the catch blocks are skipped and control is transfered to the finally block, if one is specified. Even a break statement or continue statement will not overrule this.
For all exits from try block resulting in exceptions, control is transfer to the catch block, if any such blocks are specified. If no catch block matches the thrown exception ,controlled is transferred to the finally block.(Common explaination)
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic