• 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

for loop question

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for(; is infinite loop. right ?
what is for(;;++i) ??
(assume i has been declared and instantiated earlier) is it also an infinite loop where i keeps incremented.... ??
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exactly, as long as you don't have any condition that may prevent the loop body from executing, well, it keeps looping...
So in clear in for(X ; Y ; Z )
you can have whatever you want in X and Z but if there is nothing in the place of Y then you have an infinite loop...
Actually that's not quite accurate if you have an if-statement with a break statement inside the loop, like this:

but this is bad code and I don't see the advantage of doing so... But you know there are tons of bad java code out there, so be aware
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
val:
yes there was a question is jqplus and i don't get it
int i=1;int j=5;
lab: for(;;i++)
{for(;;--j) if(i>j) break lab;}}
System.out.println(i+" , "+j);

Originally posted by Valentin Crettaz:
exactly, as long as you don't have any condition that may prevent the loop body from executing, well, it keeps looping...
So in clear in for(X ; Y ; Z )
you can have whatever you want in X and Z but if there is nothing in the place of Y then you have an infinite loop...
Actually that's not quite accurate if you have an if-statement with a break statement inside the loop, like this:

but this is bad code and I don't see the advantage of doing so... But you know there are tons of bad java code out there, so be aware

 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's exactly what I told you, there is an if-statement that may allow the "infinite"-loop to break sometimes.
Try to run it and outputting the value of i and j at strategic places in the code...
 
reply
    Bookmark Topic Watch Topic
  • New Topic