File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

k & b exercise 5-2, why endless loop

 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nisha,

What you have understood from fallowing ?
continue outer;
 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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++;
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
vikas singh
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ?
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand what continue does?
 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • 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: 2399
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to get this output continue needs to be replaced by break.
 
Sourin K. Sen
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Kevin Ren
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • 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");

}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic