• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loops  RSS feed

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q: For loop, while loop, which is better and why?
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without a purpose: neither.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Judging by the topics you have made today, I would recommend taking a read through here:

Java

I think you will find some good information there.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raj chiru wrote:Q: For loop, while loop, which is better and why?

which is better - a hammer or a wrench?
 
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which is better - a hammer or a wrench?


http://www.freepatentsonline.com/5970553.html
 
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
raj chiru wrote:Q: For loop, while loop, which is better and why?

which is better - a hammer or a wrench?


Let me elaborate that,

Your Question is Totally abstract, It totally depends on your Usage in code which logic ib better.

If you try to answer fred rosenberger's Question, the first thing you would exclaim : what for ?
If i want to bang a nail, a hammer is useful (try banging a nail with a wrench )

I would presonally prefer a while loop in conditions like:



a do while loop:



a for loop
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree.

Normally use a for loop when you know in advance how many times you want it to run.
i.e:
- you want to iterate over all items in a list
- you want to create a fixed number of objects.

Use a while loop when you don't know in advance when to exit.
i.e:
- you are reading data from a stream/file. You want to stop reading when there is no more data.
- In a game loop you probably want the loop to keep running until the user wants to exit.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!