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...
"For" loops Vs. "While" loopsWhich do you prefer?
42 Replies - 4017 Views - Last Post: 11 July 2011 - 09:32 PM
#32
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."
#33
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
Really, all loops are just branch statements
#35
Re: "For" loops Vs. "While" loops
Posted 08 July 2011 - 09:09 AM
Recursion is just an applicaiton of branching
#37
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.
#38
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
#39
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'.
#40
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
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
#41
Re: "For" loops Vs. "While" loops
Posted 08 July 2011 - 06:03 PM
i think Haskell wins this argument
funny Haskell code i just came up with
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
#42
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:
Turns out that wasn't the best practice. You were supposed to write this:
Now, the scope of it is limited to the loop.
It gets really messy when you add in generics!
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!
#43
Re: "For" loops Vs. "While" loops
Posted 11 July 2011 - 09:32 PM
cfoley, 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:
Turns out that wasn't the best practice. You were supposed to write this:
Now, the scope of it is limited to the loop.
It gets really messy when you add in generics!
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?
|
|

New Topic/Question
Reply







MultiQuote







|