GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes k & b exercise 5-2, why endless loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "k & b exercise 5-2, why endless loop" Watch "k & b exercise 5-2, why endless loop" New topic
Author

k & b exercise 5-2, why endless loop

Nisha Pinjarkar
Greenhorn

Joined: Apr 01, 2007
Posts: 20
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

Joined: Feb 15, 2007
Posts: 557
Hi Nisha,

What you have understood from fallowing ?
continue outer;


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Nisha Pinjarkar
Greenhorn

Joined: Apr 01, 2007
Posts: 20
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

Joined: Apr 01, 2007
Posts: 20
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

Joined: Feb 28, 2007
Posts: 1707
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


cmbhatt
vikas singh
Greenhorn

Joined: Apr 03, 2007
Posts: 8
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

Joined: Apr 01, 2007
Posts: 20
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

Joined: Feb 07, 2005
Posts: 2367
Do you understand what continue does?
Nisha Pinjarkar
Greenhorn

Joined: Apr 01, 2007
Posts: 20
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

Joined: Feb 07, 2005
Posts: 2367


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

Joined: Apr 01, 2007
Posts: 20
to get this output continue needs to be replaced by break.
Sourin K. Sen
Ranch Hand

Joined: May 02, 2006
Posts: 86
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.


Regards,<br />Sourin.
Kevin Ren
Greenhorn

Joined: Nov 02, 2004
Posts: 5
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");

}
 
GeeCON Prague 2014
 
subject: k & b exercise 5-2, why endless loop