I did ex 5-2 for creating a Labeled while loop. For the code as follows,
public class LabeledWhile
{
public static void main(
String[] args)
{
int age = 0;
outer:
while(age<=21)
{
age++;
if(age==16)
{
System.out.println("Get your driver's license");
continue outer;
}
System.out.println("Another year");
}
}
}
I get the result as,
Another year
Another year
Another year
Another year
Another year
Another year
Another year
Another year
Another year
Another year
Another year
Another year
Another year
Another year
Another year
Get your driver's license
Another year
Another year
Another year
Another year
Another year
Another year
But if I change the code as follows with age++ in different place,
public class LabeledWhile
{
public static void main(String[] args)
{
int age = 0;
outer:
while(age<=21)
{
if(age==16)
{
System.out.println("Get your driver's license");
continue outer;
}
System.out.println("Another year");
age++;
}
}
}
It gets into an endless loop printing "Get your driver's license".
Could somebody please clarify why is this so?