Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Josue Cedeno
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When is a "if/else" better than a loop? What makes it better?
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic