Solved

Your Preference? For or While Loops

Posted on 2004-03-31
4
288 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 45

Accepted Solution

by:
Kent Olsen 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 how to use strings and some functions related to them 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.

718 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