• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

k & b exercise 5-2, why endless loop

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nisha,

What you have understood from fallowing ?
continue outer;
 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my expectation was that when if condition will be true, the msg "get driver's ..." will be printed and while loop will continue till it reaches 21...
 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more doubt...why didn't it reach the following stmts when the if statement was false?,

System.out.println("Another year");
age++;
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nisha,

Try this modified, and find out where you are missing?



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 " + age);
continue outer;
}
System.out.println("Another year " + age);

age++;
}
}
}




Thanks and Regards,
cmbhatt
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here all the things are happning at
where(age==16)
after this statement incriment is not happening , so the value of the age wud be always 16 and thats the reason why it is goint to infinite loop
 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks !thats right vikas, but...
still the ques remains...why it did not print "Anothr year" before it reached to 16 ?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you understand what continue does?
 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what i understood is...as long as if is false, it will print "Another year" and increment (which it didn't...need to know why?). but once reached to 16, if condition is true, it will print "Get your..", cease that iteration...in this case it goes into infinite loop because it cannot get to increment code and always stays 16, as vikas said.
I would be very thankful if someone could clarify it for me where am i going wrong with continue stmt.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this code, this is the beginning of the output.



So what is happening is that while age is less than 16, the condition of the if statement isn't true, so the body isn't executed.

So we see Another year printed.

But when age does become 16, the condition of the if statement is true, so its body is executed.

In the body of the if statement, we have the statement



The continue statement causes the remainder of the body of the loop labeled outer to be skipped.

The next time the loop executes, since there wasn't an increment, age is still 16.

So the body of the if statement executes again.

Since age isn't incremented, age is still 16, so the condition of the if statement is always true.
 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to get this output continue needs to be replaced by break.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I completely agree with Keith Lynn. The purpose of the continue statement is to skip the rest of the body of the loop after it is called & start executing the loop again starting from the check condition or the label it has made a call to.

In this case, as explained by very well by Keith Lynn, when the statement "continue outer" is reached, the control gets transfered to the label outer & starts execution from that point on thus skipping the rest of the body of the while loop & since the value of age is not being incremented before the continue statement, the while condition always returns true & continues getting executed.

A break statement instead of a continue statement would prove better over here but then after executing the body of the if statement, it will come out of the loop & the value of age will remain 16 & also no other statement will be executed from the while body after that.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's what you want:

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");

}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic