GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Infinite loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Infinite loop" Watch "Infinite loop" New topic
Author

Infinite loop

asha ganapathy
Ranch Hand

Joined: Nov 03, 2006
Posts: 54
i dont understand why this code going on an infinite loop.
public class test1{
public static void main(String[] args){
int age=1;
foo:
while(age<21){
if(age==16){
System.out.println("get your driving licence");
continue foo;
}
else
System.out.println("Another year");
age++;

}
}
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by asha ganapathy:

When age is 16, it keeps looping and never gets incremented past 16.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Please use Code Tags to keep the formatting intact.
Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
I just put the code into jGrasp like this (nearly copied and pasted, just a few // as you can see)


and it compiled fine:

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 driving licence
Another year
Another year
Another year
Another year
asha ganapathy
Ranch Hand

Joined: Nov 03, 2006
Posts: 54
But if i use the same code with a for loop there is no infinite loop

why is that so?
asha ganapathy
Ranch Hand

Joined: Nov 03, 2006
Posts: 54
with for loop i got the output 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 driving licence
Another year
Another year
Another year
Another year

where as with the while loop it was infinite loop
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by asha ganapathy:
But if i use the same code with a for loop there is no infinite loop
... why is that so?

A for loop has a built-in "incrementing" statement that executes at the bottom (end) of each iteration. In this case, that statement is age++, and this is executed with the "continue" statement, which means "continue with the next iteration."

In a while loop, any incrementing must be included explicitly within the loop body. In your original example, you increment age in the "else" statement, but this is only executed if the "if" condition before it is false. So when the condition is true (at age == 16), there is no incrementing.
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
Originally posted by asha ganapathy:
But if i use the same code with a for loop there is no infinite loop

why is that so?


Because age gets incremented between foo and continue foo.

In your first example, once age == 16, there was nothing to increment it. Why are you using continue? It is not necessary and would have avoided the infinite loop in the first place:



"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
asha ganapathy
Ranch Hand

Joined: Nov 03, 2006
Posts: 54
i was doing this excersise...

Creating a Labeled while Loop
-----------------------------
Try creating a labeled while loop. Make the label outer and provide a condition to check whether a variable age is less than or equal to 21. Within the loop, increment age by one. Every time the program goes through the loop, check whether age is 16.
If it is, print the message "get your driver's license" and continue to the outer loop. If not, print "Another year."
-> The outer label should appear just before the while loop begins.
-> Make sure age is declared outside of the while loop.
--------------------------------------------------------------------
I would like to know if my understanding of this excersise was wrong?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Well, this exercise doesn't make much sense.

I would think you would want to break out of the loop after age is 16, because after that there's no need to iterate through ages 17-21. (Of course, if you know you're doing that, then the while condition should probably end at 16 instead of 21.) And there's no need for a label here -- you can just break. You could use continue while the age is less than 16, but there's no need for that either, and (again) you don't need a label for this.

With all that in mind, they seem to be asking for something like this...

The detail that was causing problems was that you need to increment "age" in both cases -- whether age is 16 or not. Otherwise, age will remain stuck at 16 as the loop continues to iterate.
[ August 09, 2007: Message edited by: marc weber ]
 
GeeCON Prague 2014
 
subject: Infinite loop