• 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

Which one to use - for loop vs while loop ?

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They can do the same things. Then what is the difference ? How do I decide whether I should use for or while ?

I looked at stack overflowand saw that we can use the while counter once while loop ends.
Cannot do that with for loop.

another - while is used when you don't know how many times you have to execute a loop.

Any more ?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the for-loop when you know exactly how many times you want to loop, while should be used in case the condition does not depend on a specific number of iterations. In general, use the option that "sounds better" in English.

I also use for-loops when I'm filtering a collection using an iterator:
 
Ranch Hand
Posts: 47
MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


For loop is used when you know the number of iterations you have to make, I mean when you know how many times to execute a loop.
WHILE is used when you are not sure about the iterations but you know what the condition is and then you can loop that till the condition is met.

One main difference is while loops are best suited when you do not know ahead of the number of iterations that you need to do. when you know this before entering the loop you can go for for loop.



And while can behave like for:
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andy Jack wrote:I looked at stack overflowand saw that we can use the while counter once while loop ends.
Cannot do that with for loop.


You can do that with a for loop, you just need to declare the counter outside the loop. I actually look at it as an advantage the other way - a for loop allows you to declare a counter where the scope is restricted to the loop. That's one reason why I'd use Stephan's approach when using an Iterator. You can do it with a while loop, and it's no more complicated. And you can argue that since you're not iterating for a fixed number of times a while loop is more natural. But then you have to declare the Iterator before the loop.

But generally, the most important difference is they express a different intent - fixed number of iterations vs. iterating till a condition occurs. And your code is generally easier to read if the intent expressed by the code matches what you're trying to achieve.
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic