?
Solved

Your Preference? For or While Loops

Posted on 2004-03-31
4
Medium Priority
?
290 Views
Last Modified: 2010-08-05
Hi, just looking to get some feedback on what programmers out there prefer and if they have any opinons about when to use on or the other. I've searched and have only been able to find what the two do and very little reasoning of why to use one or the other. For loops seem to be cleaner and have everything in one line, IMHO. I just can't see when you would prefer a while over it. Looking for a good discussion.
thanks, m.
0
Comment
Question by:unityxx311
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 260 total points
ID: 10724329
Hi unityxx311,

Both have their place.

In general, use while() if you want a continuous loop, or have several mutually exclusive exit conditions.  Use for() when you are looping through an object (or set of objects) according to an index.

These aren't "gospel", but a good set of guidelines.


Kent
0
 
LVL 16

Assisted Solution

by:imladris
imladris earned 80 total points
ID: 10724486
Not all loops are of the iterative sort. For instance, suppose you're parsing your way through a message. I wrote something like:

while(!msgEnded())
{   t=msgType();
    switch(t)
    {     case TYPE1:
                  ....
          case TYPE2:
                 ...
                 ...
                 ...
    }
}


In this case, since there is no need for an iterator variable, the while is sufficient.
0
 
LVL 4

Assisted Solution

by:booki
booki earned 80 total points
ID: 10724782
unityxx311,

also note that the for loop can mimick the while loop

while (fCondition) {
  ...
}

is the same as.

for (;fCondition;) {
  ...
}

b.
0
 
LVL 2

Assisted Solution

by:anupvijay
anupvijay earned 80 total points
ID: 10725218
It actually depends a lot on the developer himself who designs the code for specific requirement
of his.
I prefer using for loops when I know before hand the number of iterations it will take.
And when I need to loop depending on the status of some flag then I prefer while loop.
Like in the case of reading from a file will EOF.

A favorite usage of while loop is in a message queue application in the form while(TRUE){}

And of course I even prefer going the recursive way for fewer iterations instead of 'for' loops
sometimes.

Cheers,
Anup
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question