Solved

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

Posted on 2013-01-30
11
238 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 83

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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

777 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