Solved

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

Posted on 2013-01-30
11
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress
Suggested Courses

632 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