File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tricky Question about Continue Statement

 
Harshit Sethi
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harshit Sethi
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Harshit Sethi
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think thats iteration not increment.There is a difference between them.Isn't it?
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay .May be you are right .I don't know that for sure.
 
Harshit Sethi
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks by the way ,the program you posted helped clearing the concept though.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dude....if it is not there, then how would cat ever be reached !!!
 
Harshit Sethi
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic