• Post Reply Bookmark Topic Watch Topic
  • New Topic

For Loop vs. While Loop  RSS feed

 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what situation or circumstances would you use while loops as oppose to for loops? Can you give me an example? What is the different between the two?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definite versus indefinite loops.

"Loop over this list of values" versus "loop until some condition becomes true at some point"
 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you be more specific? When you say "Loop over this list of values", do you mean this is a while loop or a for loop?
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A for loop generally implies a known end point that can be sequentially reached by a counter:

A while loop generally implies a calculated or flagged exit condition:

Now having said that, that is the classic use, but you can force loops to mimic other types of loops.
Timothy Han wrote:In what situation or circumstances would you use while loops as oppose to for loops? Can you give me an example? What is the different between the two?
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of basic examples:

for loop: iterating over an array
You might have ten integers stored in an array that you wish to add up.
The array length is known at the loop's entry point so we can say loop for indexes 0 through 9 and each time around the loop you add the next value to the running total.

while loop: reading an unknown number of lines from the users keyboard or from a text file.
Each time around the loop we read the next line, loop could terminate when we encounter a pre-determined end-marker string.
Eg loop while input string not equal to "END"
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a little simple jiggery‑pokery you can use a while loop for everything.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:With a little simple jiggery‑pokery you can use a while loop for everything.

I've never really thought about it, but couldn't you also use a little pokery‑jiggery and use a for loop for everything?

 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I see it, a for loop is just syntactic sugar for a special setup of while loop.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy Han wrote:In what situation or circumstances would you use while loops as oppose to for loops?

My answer would be this: I generally use for loops because they're more flexible; but there are some cases where a while loop describes what I'm doing better - and in those cases I'll use it.

But otherwise, Les and Steffe's explanations say pretty much what I would have.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ole Kristian Sandum wrote:The way I see it, a for loop is just syntactic sugar for a special setup of while loop.

Maybe, but the ability to define variables in the scope of the loop is a pretty darn powerful piece of sugar.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I've never really thought about it, but couldn't you also use a little pokery‑jiggery and use a for loop for everything?

But wouldn't a do...while do exactly the same thing?

And even if that was your intent, shouldn't it be:
I just thought I'd pre-empt Campbell.

Winston
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
I've never really thought about it, but couldn't you also use a little pokery‑jiggery and use a for loop for everything?


If you want to actually mimic a while loop then it would be:

Of course you'd deserve everything you got in your code review...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
If you want to actually mimic a while loop then it would be:

Of course you'd deserve everything you got in your code review...


a) it was early. Pre coffee.
b) I just whipped it out in few seconds - basically as fast as I could type it. Didn't really think it through
c) I didn't say it was a GOOD idea...just that is was possible
d) It was more of a though exercise, trying to show all three basic loops are more or less interchangeable
e) I got to use the phrase "jiggery-pokery" (albiet mixed up a bit just because).

 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:. . . couldn't you also use a little pokery‑jiggery and use a for loop for everything? . . .
By missing out the first and third parts of the for loop header inside the () you can make it equivalent to a while loop.
Bertrand Meyer, in Eiffel the Language 1992, Preface, page ix wrote:Who needs three forms of loop when a general enough one will do …?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!