Solved

Your Preference? For or While Loops

Posted on 2004-03-31
4
252 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
4 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 65 total points
Comment Utility
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 20 total points
Comment Utility
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 20 total points
Comment Utility
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 20 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now