"For" loops Vs. "While" loops

Which do you prefer?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 6205 Views - Last Post: 11 July 2011 - 09:32 PM

#31 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: "For" loops Vs. "While" loops

Posted 07 July 2011 - 02:46 PM

how the hell did this become a conversation? really i want to know, that way i can figure out how Jersey Shore come to be.

riddle me this, if for loops where better than while loops(or vise versa) then why would basically every programing language that uses loops have both? the simple truth that has been chimed over and over again is that there are meant for different situations.

my uncle has a saying "right tool for the right job", it applies SOOOO many places in programing(even though he was talking about hammers and such). would you use a sledge hammer to pound a nail? no, but you can...
Was This Post Helpful? 0
  • +
  • -

#32 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 144
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: "For" loops Vs. "While" loops

Posted 08 July 2011 - 06:33 AM

Quote

the simple truth that has been chimed over and over again is that there are meant for different situations

Agree!

While reading the posts here, I remembered something I recently saw in one of the topics in the Java forum, which becomes pretty funny in this context. Basically, all the author wanted was an infinite loop, but it seems he/she didn't know that there was something like while (true), so he/she improvised (I am not kidding):
for (int i = 0; i <= 0; i--) {...}
Guess that makes to correct answer to "right tool for the right job": "If all you've got is a hammer, everything looks like a nail."
Was This Post Helpful? 1
  • +
  • -

#33 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2793
  • View blog
  • Posts: 15,899
  • Joined: 24-September 08

Re: "For" loops Vs. "While" loops

Posted 08 July 2011 - 07:59 AM

Not to mention how this is a necropost.

Really, all loops are just branch statements ;)
Was This Post Helpful? 0
  • +
  • -

#34 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: "For" loops Vs. "While" loops

Posted 08 July 2011 - 08:04 AM

And recursive.
Was This Post Helpful? 0
  • +
  • -

#35 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2793
  • View blog
  • Posts: 15,899
  • Joined: 24-September 08

Re: "For" loops Vs. "While" loops

Posted 08 July 2011 - 09:09 AM

Recursion is just an applicaiton of branching ;)
Was This Post Helpful? 0
  • +
  • -

#36 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: "For" loops Vs. "While" loops

Posted 08 July 2011 - 09:43 AM

free pr0n plixx
Was This Post Helpful? 0
  • +
  • -

#37 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: "For" loops Vs. "While" loops

Posted 08 July 2011 - 02:09 PM

Neither. I use languages where for loops and while loops aren't really useful and make little sense. Instead, I prefer recursion and better yet, higher order functions that abstract the looping away.
Was This Post Helpful? 0
  • +
  • -

#38 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: "For" loops Vs. "While" loops

Posted 08 July 2011 - 02:38 PM

yes you see but the rest of the world struggles to get by with imperative languages. we meager mortals find your "higher order programing" difficult and godlike. if you could only bless us with your gift we might see the light
Was This Post Helpful? 0
  • +
  • -

#39 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: "For" loops Vs. "While" loops

Posted 08 July 2011 - 03:58 PM

 (defn foldl [f start s] (if (seq s) (recur f (f start (first s)) (rest s)) start))


The left fold. It shall be your salvation.

For those who care, there is a fold function already in Clojure and is called 'reduce'.
Was This Post Helpful? 2
  • +
  • -

#40 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: "For" loops Vs. "While" loops

Posted 08 July 2011 - 04:12 PM

Looping via Recursion Examples in VB.net

In Nemerle the For loop macro is based on recursion.

Fib in Nemerle
Spoiler

This post has been edited by AdamSpeight2008: 08 July 2011 - 04:40 PM

Was This Post Helpful? 0
  • +
  • -

#41 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,710
  • Joined: 03-August 09

Re: "For" loops Vs. "While" loops

Posted 08 July 2011 - 06:03 PM

i think Haskell wins this argument

foldl f z []     = z
foldl f z (x:xs) = let z' = z `f` x 
                   in foldl f z' xs



funny Haskell code i just came up with
main = forever alone
    where alone = ()


This post has been edited by ishkabible: 08 July 2011 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

#42 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: "For" loops Vs. "While" loops

Posted 11 July 2011 - 04:26 PM

Something that hasn't been mentioned is that for loops allow tighter scope control. Before Java allowed the for..each loop, everyone did iterators like this:

Iterator it = collection.iterator();
while (it.hasNext()) {
  Object o = it.next();
  // do something with o
}


Turns out that wasn't the best practice. You were supposed to write this:

for(Iterator it = collection.iterator(); it.hasNext();)/> {
  Object o = it.next();
  // do something with o
}


Now, the scope of it is limited to the loop.

It gets really messy when you add in generics!
Was This Post Helpful? 0
  • +
  • -

#43 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 165
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: "For" loops Vs. "While" loops

Posted 11 July 2011 - 09:32 PM

View Postcfoley, on 11 July 2011 - 11:26 PM, said:

Something that hasn't been mentioned is that for loops allow tighter scope control. Before Java allowed the for..each loop, everyone did iterators like this:

Iterator it = collection.iterator();
while (it.hasNext()) {
  Object o = it.next();
  // do something with o
}


Turns out that wasn't the best practice. You were supposed to write this:

for(Iterator it = collection.iterator(); it.hasNext();)/> {
  Object o = it.next();
  // do something with o
}


Now, the scope of it is limited to the loop.

It gets really messy when you add in generics!


Looks like someone has been reading Effective Java?
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3