Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use the loop ?  RSS feed

 
benny rusli
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello people,

i am immediately thinking about the loop while,for and do while. When should i use while, when to use for and when to use do while.



As far as i know all is the same, but what is the benifit from each other, when to use for loop is better than while loop or do while loop. Thanks for any help.
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you pointed out the looping constructs are pretty much the same. You could accomplish everything you need with just a while loop (and some if statements for the do-while). But for design purposes we have all three. The idea is to use the loop mechanism that fits your problem the best. This leads to more readable and maintainable code. The general idea is to use for loops when you are "counting" and use while loops when you are waiting for a condition to change. The do-while is very similar to the while in that you use that while waiting for a condition to change. The main difference is that a do-while loop will always execute the body at least once where a while loop body might never be executed.

In practice I see very few do-while loops.

Hope this helps.

_M_
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As this is the beginner forum, I thought it might just be worth pointing out that "break" and "continue" are statements not to be used without really good reason.

You should try hard to structure code so that the loop exit conditions are tested in the actual "while" or "for" statement, rather than at some point within the loop. The reason is that the code is much more readable, and hence easier to debug and to maintain. These things matter a lot in the real world.

The version of "break" that works with a label is even more in the category of nasty things to be used rarely, if at all. If you haven't heard of it, may I suggest you don't read up on it - that way you can't be tempted to over-use it!
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by arifin rusli:
Hello people,

i am immediately thinking about the loop while,for and do while. When should i use while, when to use for and when to use do while.



As far as i know all is the same, but what is the benifit from each other, when to use for loop is better than while loop or do while loop. Thanks for any help.


I think that what Mark is trying to say is that you don't need the if statements in any of these constructs. Instead, you can write these loops as

This will make your code much easier to understand, in my opinion.

As to your question, the for loop is typically used to count the number of times you want to repeat something. This means that you usually need to know exactly how many times you want to repeat. The while loop on the other hand is usually used when you don't know the exact number of times to repeat. Instead, you have a flag that indicates when you want to stop. Finally, the do while statement is useful when you know you need to do something at least once. Personally, I don't find the while loop very useful in practice.

Layne
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!