• 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

Infinite loop

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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++;

}
}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by asha ganapathy:


When age is 16, it keeps looping and never gets incremented past 16.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use Code Tags to keep the formatting intact.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But if i use the same code with a for loop there is no infinite loop

why is that so?
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic