It's not a secret anymore!*
The moose likes Cattle Drive and the fly likes Which is better Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Which is better "if/else" or "loop"? " Watch "Which is better "if/else" or "loop"? " New topic
Author

Which is better "if/else" or "loop"?

Josue Cedeno
Ranch Hand

Joined: Feb 06, 2001
Posts: 71
When is a "if/else" better than a loop? What makes it better?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

if/else is one way to perform "selection"

whereas looping is well... "repetition".

So they really aren't comparable.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Not totally sure in which context you are making the comparison, but like Mike said I can't really see how you could compare the two. Is this from a nitpick?
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1745
    
    2
I don't know if this is what Josue is getting at, but any for or while...do (or do...while) loop can be built with an if statement and break/continue.
(Remember languages that don't have blocks?...)
If the point of the code is looping, then go ahead and use a built-in looping structure.
Reserve if statements for conditional execution of blocks of code.
(According to JavaRanch Style Guide, never use "continue" and never use "break" other than in a switch statement.)
Josue Cedeno
Ranch Hand

Joined: Feb 06, 2001
Posts: 71
This question actually is the result of a nitpick. It was just suggested that an "if/else" may work better, and I wondered if it was performance related. Or maybe it is a design pattern. I don't know (just curious )
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Could it be that you used a "loop" for a very small number of iterations. Then "if" statements are generally easier to understand and maintain.
I remember getting a similar comment way back in one of my nitpicks. I wrote the code so one could extend it. The assignment however only required a small fixed #. Have to find the nitpick in question to be 100% sure though.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Sounds like a 4B nitpick to me. Normally, if you have less than 4 iterations on a loop, it's easier to write out the calls than messing with a loop. However, you still wouldn't need an if/else statement for that, unless you actually had one in your loop in the first place. But that's for for loops, perhaps you were using a while?
Josue Cedeno
Ranch Hand

Joined: Feb 06, 2001
Posts: 71
It was because of a small number of iterations. I asked the question because I thought it may have related to a performance issue.
Mario Levesque
Ranch Hand

Joined: Nov 01, 2000
Posts: 51
Sometimes you can perform tests using if statements but it is better to use a while loop instead.
See explanantion on Threads chapter 7 of RHE.
This has to do with constantly rechecking the value of a state in a monitor before going on.
This is an example of where you can compare using one technique over the other. As far as calling one better than the other, I think that both serve their own programming purpose and are equally valuable.


<a href="http://www.ajmasters.com" target="_blank" rel="nofollow">http://www.ajmasters.com</a> Real Estate, Tampa Florida
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Which is better "if/else" or "loop"?
 
Similar Threads
a new way to do the for loop
For or If-Else?
inefficient
How dose the loop impact the performance? How to measure?
Loops