Solved

Your Preference? For or While Loops

Posted on 2004-03-31
4
270 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
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 20 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 20 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 20 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Inorder binary search tree 5 166
negation in C function 14 154
Global Keyboard Hooks Blocked 4 77
How to install SVN Command Line Client? 5 67
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

803 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