• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

continue doubt...

 
Arun Kumaar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
though my question seems to be silly, but something in it is nagging me...

1. int I = 0;
2. label:
3. if (I < 2) {
4. System.out.print("I is " + I);
5. I++;
6. continue label;
7. }

The above code will give a compile time error, since the continue is not within the loop, but isn't it residing inside the if loop construct.

Regards
Arun
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if is not a loop it is a conditional statement. continue and break are specifically for loops. This means for, while and do while loops

continue means stop execution here and begin at the next iteration fo the loop. since if is only run once and is not looped it does not make sense to use continue with an if statement
[ May 23, 2005: Message edited by: Damien Howard ]
 
Arun Kumaar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't it becomes a looping construct when combined with continue...

Regards
Arun
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No its still a conditional,
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Kumaar:
Doesn't it becomes a looping construct when combined with continue...

Regards
Arun


continue means to begin the next iteration, but since if is not a loop there is no next iteration.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't it becomes a looping construct when combined with continue...


If Java allowed it, it would effectively become a loop. but it's not allowed. a continue statement can only be inside the body of one of the three defined loops.

I'm sure if you read the Java Language Specification, it'd spell it out a little more formally. But for now, you'll just have to take our word for it!!!

:-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic