• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

What is a good rule of tub for when to use case statements versus if/then statements.

Curious about when it's best to use if/then statement versus cases statements. A couple of writeups say that it's languague dependent, but they discuss several languages and I'm specifically concerned with Korn shell scripting. Other say that if you have multiple ifelse in the if/then loop, then it's best to use case. I looking for a good rule of tub as it seems I tend to use if/then statements way too much and maybe in the wrong way where a case statement is more appropriate. Anybody?
0
teckwiz01
Asked:
teckwiz01
  • 4
  • 2
  • 2
  • +3
1 Solution
 
stergiumCommented:
hello.
To my logic a good choice of case would be when you have more than two states that you want to check (instead of too many nested ifs)
0
 
Dave BaldwinFixer of ProblemsCommented:
A case statement only checks the value of a single variable.  If you need to test more than one variable, you will need 'if' statements.
0
 
user_nCommented:
I think that it is your choice.  You will see that in some moment if caseses becomes to much and it is hard to  see the logic. With case statement the logic is more clear. More clear logic givse lesser mistakes.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
TommySzalapskiCommented:
There are some places where the decision is obvious (x<5 or x>23, multiple variables, you want fall through, etc). For the other places, it's up to you.

My personal rule of thumb is that if there's more than two possibilities, and you can use a case cleanly, do it.
0
 
ozoCommented:
If you tend to use if/then statements too much where a case statement is more appropriate, then maybe a rule of thumb for you would be to use case statements in those situations.
0
 
teckwiz01Author Commented:
ozo, that's almost like the old doctor's joke - doc, it hurts when I do this & the doc tell you well don't do this. The issue is that I'm feeling like I'm using if/then statements in situations where I should probably use a case statement but haven't put my finger on which situations are best suited for that, which is why I was asking for other's rule of thumb that may help me define one for myself..
0
 
ozoCommented:
One rule of thumb I use is to use case statements when case statements are simpler and to use if/then statements when if/then statements are simpler,
or to use the one which can be most easily modified to change the program to do a different useful function that is closely related to what the current program is doing.
But most situations seem to naturally lend themselves to one or the other without a lot of deep analysis.  
Maybe if you can tell us more about the situations that seem to you to be tricky borderline cases, we can tell you more about how we would decide in those cases.
0
 
teckwiz01Author Commented:
A good example is in the post: http://www.experts-exchange.com/OS/Linux/Q_28015165.html

I was using so many if/then statements that I actually lost myself. The script now doesn't even work. I was trying to compensate for the lack of having the same system. We are getting moved around alot from machine to machine and I'm being forced to manually recreate users and restore their files. In doing so, I wanted to keep the same UID/GIDs to not have them have access issues. I lost myself in using if/then inside of if/then inside of more if/then to the point that the scripts doesn't actually work & I lost the idea of what I was trying to do. I basically had 2 files that included all the entries from /etc/passwd & /etc/shadow pertaining to each user and trying to parse it throught the script to replecate the enviroment. I didn't even get as far as using what was in /etc/shadow as I lost the idea and even the first part of just creating the IDs doesn't work.
0
 
teckwiz01Author Commented:
Ok, so I gave an example per Ozo's request. This isn't an abandoned question by me, I just haven't heard back.
0
 
TommySzalapskiCommented:
If you can't get it to work with 'if', 'else if', and 'else', then a switch most likely isn't going to make it any easier. There's nothing a switch can add (except fall through).
A switch case can make your code a little cleaner, but it's unlikely that it would make unreadable code readable.
0
 
teckwiz01Author Commented:
I guess I can accept that. I guess I'm just a little paranoid and thinking that there has to be a better way when maybe there doesn't have to be.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now