• 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

Loops

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay Loops. I know many beginners like me would have similar questions.
What are the advantages and disadvantages for each loops - "For", "do while", and "while"?

Thank you.
 
Ranch Hand
Posts: 56
4
Mac OS X IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yuji,

it seems to me you posted to the wrong forum (html, css, javascript), as your question seems Java related, but anyway.
Each loop can serve all purposes, but each has some special area where it is most helpful in.
For loop - it automatically has a counter variable like "int i", so it is good if you need the index for something later one.
while loop - it is good if you don't care about the index, or if you don't continously step over each element, but have some special condition to be checked.
do while - rarely needed, and harder to understand, would be used in similar situations like the while loop, just that the first time the condition is not checked.
I would try to replace it by the regular while loop.
And in case yo are talking of Java > version 5, you would also have the
for each loop, (Element element: elements) {} which is very cool to iterate over collections.
Under the hood, it will use the iterator returned from your collection, so it should be very performant.

Actually, I have two videos made comparing the different loops. Check out episode 8 + 9 of my Free Java Video Course.

Hope that helps.

Marcus
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Beginning Java.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For loops are generally preferred when you know EXACTLY how many iterations you need. i.e you are going to ask a user to input 5 numbers.

While loops can be used when you don't. i.e. have the user enter positive integers, until they enter "-1"

do-while is not much different than a while - it's just guaranteed to run at least once.

for-each loops are good when you have a collection of things, and you want to process each one.

Having said all that...I don't believe there is a situation where you cannot convert one to any of the others.
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:for-each loops are good when you have a collection of things, and you want to process each one.


It should be emphasized that in the JavaScript context (which the OP was actually inquiring about) there is no "language support" for this - instead the feature is implemented by the collections themselves.

ECMAScript 5:
Array.prototype.forEach()
ECMAScript 6:
Map.prototype.forEach()
Set.prototype.forEach()

On arrays contemporary JavaScript code will often use the "forEach" method (or some library's equivalent) in favor of JavaScript's "for" iteration syntax. jQuery.each() helped popularize that approach.
Once you understand how Array.prototype.forEach() works there are some other methods worth looking into:
Array.prototype.every()
Array.prototype.some()
Array.prototype.map()
Array.prototype.reduce()

Mozilla Developer Network: Loops and Iteration
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quick examples could look like:



 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In LV's 2nd example, you would expect something in the loop to change running to false.
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic