Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

continue label

 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A{
public static void main(String[] args){
int age=10;
label:
while(age<=18)
{
if(age==16)
{

System.out.println("16");
continue label;
}

System.out.println("nottt");
age++;
}
}
}


output:
n infinite loop printing 16??
why is it so shouldnt it print
nott
nott
nott
nott
nott
16
nott
nott
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought to Use Code Tags to preserve the indentation, then print it out and follow the flow of execution with a pencil. Then the problem will become obvious.

There are good reasons why the JavaRanch style guide disapproves of labelled continue. Read them. It is regarded as equivalent to "goto" and many people (myself included) look on it as poor programming style.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will keep the above point in mind while posting next time.
thank you
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you find the problem? (Hint- what happens to age after printing "16")
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i guess that it prints nott for 5 times and when it becomes 16 the condition (age==16)becomes true and it prints 16 then due to continue statement it moves back to the label
so age++ never executes after age become 16
so age==16 always evaluates to true after the first encounter and thus the infinite looping
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yipppee
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic