Everyone's Favourite LOOP

Questions about programming languages and debugging
Post Reply

Your Favourite Loop

while()
4
67%
for()
2
33%
 
Total votes: 6

eppik
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 212
Joined: 26 Mar 2006, 16:00
18
Location: Infinite Loop
Contact:

Everyone's Favourite LOOP

Post by eppik »

I'm interested in what you guys think of each looping method and which is your favourite.

IMO for() is the most horrid thing compared to the while, as it combines a lot of stuff in its arguments

while seems like the best choice because you tell him: START LOOPING and it tells you: TILL WHEN.
Its much simpler and easier to understand.

ex:

Code: Select all

# Sample 1

porn_on_suckoforums = 0

while(badbrain_sleeping = 1)
{
    getsleepiness();                            #function that checks sleep levels and returns badbrain_sleeping
    porn_on_suckoforums++;
} 

#Sample 2

for(porn_on_suckoforums=0, badbrain_sleeping=1,porn_on_suckoforums++)
{
    getsleepiness();
}
See what I mean? (I hope I wrote the for one right)

You look at a while loop and dont even have to think, but looking at a for you kinda need to remember what your looking at.

User avatar
ph0bYx
Staff Member
Staff Member
Posts: 2039
Joined: 22 Sep 2008, 16:00
15
Contact:

Re: Everyone's Favourite LOOP

Post by ph0bYx »

Damn it dude! BB's gonna find out about our underground porn factory now! :evil:

OT: I'm kinda fine with both loops. But I'm a newb, what do I know anyway :D

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: Everyone's Favourite LOOP

Post by bad_brain »

what? 8O :lol:

I pick while(), because it's actually a little easier.. :lol:
Image

User avatar
lilrofl
Siliconoclast
Siliconoclast
Posts: 1363
Joined: 28 Jan 2009, 17:00
15
Location: California, USA
Contact:

Re: Everyone's Favourite LOOP

Post by lilrofl »

I can't think of a time, recently, that I've used a for loop instead of a while loop, when either could be used. I use 'for' mainly for grabbing letters out of a tuple or for x in range manipulation of list.

That probably came out convoluted...
knuffeltjes voor mijn knuffel
[img]http://i911.photobucket.com/albums/ac320/stuphsack/Sig.jpg[/img]

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: Everyone's Favourite LOOP

Post by maboroshi »

Python for loops are the best imo

I'm just saying, just saying

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: Everyone's Favourite LOOP

Post by Gogeta70 »

I prefer for loops. Here's why:

WHILE

Code: Select all

//values is defined as: char values[64]; and already contains data.
//If i want to iterate through the 'values' array with a while loop...

unsigned short i = 0;
while(i < sizeof(values))
{
    some_function(values[i]);
    i++;
}
FOR:

Code: Select all

//values is defined same as above. To iterate through it with a for loop...

for(unsigned short i = 0; i < sizeof(values); i++)
{
    some_function(values[i]);
}
A for loop allows you to define an 'index' variable, specify a condition, as well as execute a line of code before/after each iteration in the loop.

A while loop simply checks if a condition is true.
¯\_(ツ)_/¯ It works on my machine...

babagabble
forum buddy
forum buddy
Posts: 11
Joined: 10 Dec 2010, 17:38
13

Re: Everyone's Favourite LOOP

Post by babagabble »

I tend to use whichever loop is more clear for the situation. If a while can do it, there's no point in using a for loop. It just makes you have to check more closely if you're scanning.

IMHO

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Re: Everyone's Favourite LOOP

Post by Lundis »

There's a time for each one, you can't just choose between one of them and stick to it at all times if you'd like to call yourself a coder.

If you want to run something N times, a for loop is usually better, as shown in this example:

Code: Select all

for( int i = 0; i < N; ++i) { //everything in one place, you can tell what's gonna happen at a glance
    ...
}

int i = 0; //initialization
while (i < N) { //condition
    ... // this could be a huge amount of lines which causes the next line to not even show up on the screen
    ++i; //increment
}
On the other hand I would use a while loop when not knowing how many times it'd be run, such as when waiting for user input, checking that a certain condition is true (such as a network connection being alive) or reading a file, one line at a time.

Post Reply