• 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 and continue statement

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Pls clarify this.

If the break or continue statement is within a if statement which is again inside a do-while loop or any other loop then how will the break and continue statement beahe.

For eg.
int i=0 ,j=10;
do{
if(i<j){
break;
}
}while (i<6)

or-----------

int i=0 ,j=10;
do{
if(i<j){
continue;
}
}while (i<6)

when the break and continue is encountered then will it come entirely out of the loop?

Pls explain

thx in advance
megha
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The break/continue statments function in coordination with the looping constructs, not the if statements. I have changed your code slightly to explain the behaviour:

The above code will print "After loop condition test" only. Since the value of i is less than j, the if condition is executed immediately and execution starts at the next command following the loop construct.
 
megha kanth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i=10,j=15;

do{
if(i<j){
continue;}
}while (i<5);

what will be the output. Will the continue statment work even if the while condition turns to be false.

Pls explain

Megha
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Megha,

have you tried running this code? you'll probably remember this stuff better if you try it, see what it does, then figure out why. put in print statements to make sure it's doing what you really think it's doing. then, write something similar, predict what you think will happen, then run that.

having said that, your question doesn't make a lot of sense. your sample code doesn't have ANY output, no matter what the values of i or j are.

now, if your asking what the flow in this code is, we can just walk through it.

we enter the do loop. do loops garuntee we execute the body of the loop at least once. that means the condition is testes AFTER we do everything.

the body of the loop says IF whatever. in this case, i<j is true, so we go inside the if statement. it says "continue".

the continue statement says "stop everything, and go to the test condition of whatever loop i'm in".

so, we go to the test condition - i<5. this is false, since i = 10. so, we quit the loop, and don't execute it again. the next thing we do is whatever is AFTER your "while (i = 5)" line.
 
megha kanth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi fred,

Thx for the reply. Your statement "the continue statement says "stop everything, and go to the test condition of whatever loop i'm in". clarified my doubt.

I just wanted to know abt the continue statement. As i read somewhere that in a loop the continue statement goes to the next iteration and hence i thought that it will go to iteration without checking the condition.

But now its clear. thanks a lot for that.

megha
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know, i've read that too, but never thought about it much. You're right that when they say "continue with next iteration", it can be interpreted as "skip the check".

Note, however, that what i said isn't even exactly correct. if you are in a "for" loop, it doesn't go to the test condition, it goes to the "at the end of the loop do this" statement. in other words here:



the third time through the loop, when i does indeed equal 2, we will hit the continue statement. we will then immediatly jump to the i++ part of the loop, not the test condition. so i will become 3, THEN we will test...

in other words, the continue statement can be thought of as a closing brace to the loop. whichever kind of loop it is, it will act as if you hit that brace.

[corrected typos]
[ June 28, 2004: Message edited by: fred rosenberger ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic