Solved

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

Posted on 2013-01-30
11
236 Views
Last Modified: 2013-08-30
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
Comment
Question by:teckwiz01
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 13

Expert Comment

by:stergium
ID: 38838572
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
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38838594
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
 
LVL 9

Expert Comment

by:user_n
ID: 38838788
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38839767
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
 
LVL 84

Expert Comment

by:ozo
ID: 38841439
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:teckwiz01
ID: 38845478
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
 
LVL 84

Accepted Solution

by:
ozo earned 150 total points
ID: 38845681
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
 

Author Comment

by:teckwiz01
ID: 38874962
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
 

Author Comment

by:teckwiz01
ID: 38924123
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38925743
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
 

Author Comment

by:teckwiz01
ID: 39027615
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now