• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loops  RSS feed

 
Yuji Ashizawa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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.
 
Marcus Biel
Ranch Hand
Posts: 51
IntelliJ IDE Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Beginning Java.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • 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
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick examples could look like:



 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In LV's 2nd example, you would expect something in the loop to change running to false.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!