Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tricky Question about Continue Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tricky Question about Continue Statement" Watch "Tricky Question about Continue Statement" New topic
Author

Tricky Question about Continue Statement

Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
We know that continue statement encountered in a for loop result in jumping at the beginning of that for with next iteration.

What about within while loop,if continue is encountered within while loop ,then the iteration takes place ?

I think the answer is no. Need your suggestions.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Harshit Sethi wrote:
I think the answer is no. Need your suggestions.


Why do you think the answer is no? Aren't there cases where you are using a while loop, and would like to go to the next iteration of the loop?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
This is because I tried a program and the iteration was not happening.I wanna confirm about that if thats the case or my logic was wrong
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Harshit Sethi wrote:This is because I tried a program and the iteration was not happening.I wanna confirm about that if thats the case or my logic was wrong


Care to show us the code? We can't confirm your logic if we don't know what you did.

But to answer your question, the continue statement works for all three loops -- for, while, and do-while loops.

Henry
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75




See this code runs as an infinite loop,however if the iteration had been done then it should have printed 5,but the output is coming to be an infinite loop.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Hint: The continue statement skips the rest of the body of the loop, to run the next iteration -- what happens to the increment?

Henry
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
Yeah thats exactly what i am asking,in for loop it happens even if the rest of the body is skipped but in while loop by executing this program,it seems the same is not true for while loop.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Harshit Sethi wrote:Yeah thats exactly what i am asking,in for loop it happens even if the rest of the body is skipped but in while loop by executing this program,it seems the same is not true for while loop.


Remember that the for loop has four parts -- the initialization, the condition, the reinitialization, and the body. When a continue occurs, the rest of the body is skipped, the reinitialization is executed, and the condition is check for the next iteration. With a while loop, there is only two parts -- the condition, and the body. The behavior is pretty much the same. The rest of the body is skipped, and the condition is checked for the next iteration.

Obviously, with a while loop, the reinitialization isn't executed, because there is no such a thing.

Henry
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
So that means i have to be cautious and don't think that the increment after continue in while loop will be executed .

That's the whole point .Thanks.
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Just to add two more words

1) Be cautious in for loops too, if increment statements is not at its default place.

2) continue applies to Enhanced loop too.




SCJP 6.0 96%

(Connecting the Dots ....)
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
I think thats iteration not increment.There is a difference between them.Isn't it?
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Well i was just adding points to you post,

But

still what you are saying is not true (IMHO) because in for each loop there is automatic increment that is done by jvm !!!

Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
Okay .May be you are right .I don't know that for sure.
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
Thanks by the way ,the program you posted helped clearing the concept though.
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Dude....if it is not there, then how would cat ever be reached !!!
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
Thats how you thought the for-each loop works .I have think of it as working like this.

Each time the loop iterates through the collection or array and the subsequent values are assigned to the newly declared variable in each iteration.


Increment and iteration are different things .There is no variable whose value is incremented i.e 1 is not added to any variable after each iteration.
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
How would subsequent values are assigned to the variable .................through increment only

and increment does not literally means adding 1.

For me increment atleast means subsequent according to your context and logic applied in the loop

Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
Then what you were saying is true.However i think of incrementation as a value of a variable is incremented and iteration as subsequent.Anyways problem is solved.Everyone has his own way of thinking.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Tricky Question about Continue Statement