0

Possible Duplicate:
Advantage of switch over if-else statement
Why Switch/Case and not If/Else If?

I am currently learning "switch case" in school and am wondering what's the point of learning it when "if else" already exists. "if else" is basically another way of doing "switch case".

Please correct me if i am wrong.

Community
  • 1
  • 1
Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • 5
    They're not the same. You need to re-read how they work. – Marvo Dec 19 '12 at 03:23
  • 3
    Try implementing [duffs device](http://en.wikipedia.org/wiki/Duff%27s_device) as nicely with `if then else`. – Martin York Dec 19 '12 at 03:27
  • 2
    And even they were the same (at a functional level), it is important to understand the different ways of doing the same thing ... so that you know which one is most appropriate for the circumstance you are currently in. – Stephen C Dec 19 '12 at 03:27
  • 4
    To avoid downvotes and closed answers, try "more constructive" phrasing such as: "What are reasons to use switch-case over if-else?" and make sure such a "neutral objective question" is also posed in the title. In addition, showing more effort by proof-reading can get a more receptive response. –  Dec 19 '12 at 03:29
  • 2
    (However, it is a duplicate either way: http://stackoverflow.com/questions/1028437/why-switch-case-and-not-if-else-if?rq=1 , http://stackoverflow.com/questions/195802/switch-case-vs-if-else?rq=1 , http://stackoverflow.com/questions/97987/advantage-of-switch-over-if-else-statement) –  Dec 19 '12 at 03:31
  • 1
    apparently SO readers vote to close, and downvote, most any basic question that they would find hard to answer (manifestly complex questions are OK though). i wish they'd stop doing that, since it's very detrimental to the site's usefulness, AND promotes a culture of rote memorization and pattern matching rather than an engineer's understanding. but it's a paradox like the incompetence paradox: in order to be secure enough to not feel threatened by such questions, these folks have to gain a level of understanding that appears to require consideration of questions of just that kind. – Cheers and hth. - Alf Dec 19 '12 at 03:53
  • 1
    @Lim - The people who design programming languages are NOT idiots. And the people who teach programming languages are NOT idiots either. They have sound reasons for 1) including constructs in programming languages, and 2) teaching those constructs to their students. So if you don't see the point of what you are being taught, it is probably safe to say that that is because you haven't fully grasped the significance yet ... not that your teachers are teaching you something that you don't need to know. – Stephen C Dec 19 '12 at 04:27
  • @Lim Do you like program in assembler? – Paul Vargas Dec 19 '12 at 04:35
  • I ask you another question: Why do we learn "do loop", "for loop" along with "while loop" ;-) (Most of them translate to "goto back;" in assembly) – anishsane Dec 19 '12 at 06:59

5 Answers5

3

It kinda nostalgic to heard it. Both of them actually 'looked' the same. But is a little bit different when the codes executed.

Firstly, 'switch-case' is about comparing value-only. But 'if-else' could process a boolean expression (which would support much more complex clauses)

If you use general 'if-else' when you have found what you are actually searching for, the process will still run until it has finished processing the last if (but actually it could use jump-technique to have similar mechanism like 'switch-case'.) It won't happen if you use 'switch-case' because once the value you're searching for has been found, it will break and won't continue to the next case. Also, 'switch-case' is faster-to-process than if else because it only compares defined values (not expression). And 'switch-case' also has a good formatting structure (it's simple, compact, readable and clean).

Community
  • 1
  • 1
shrotavre
  • 98
  • 6
1

The more tools you have the better. Flat out the best statement of why you should know both... however a more detailed example -

A switch statement works on a single type of variable of the construct:

variable == value

So for example in C if you were trying to compare something to a few different strings in order to make a decision, you can't do that with a switch. In this case you need to know about the if/else constructs.

However if you have a large number of sequential checks:

var == 1  or
var == 2  or
var == 3  etc

The compiler may take your switch statement and convert it to a jump table, which would end up being faster than a large number of comparisons that an if/else list would be.

Mike
  • 47,263
  • 29
  • 113
  • 177
0

You should learn the switch construct because it is a useful tool provided by the C language.

It is not the same as if-else blocks.

In the comments section of your question, there are links to existing StackOverflow answers explaining what the differences are.

Each construct has its strengths and weaknesses, and over time you will learn when it is appropriate to choose one over the other.

paddy
  • 60,864
  • 6
  • 61
  • 103
0

You should learn both. While it is technically possible to implement any if / else sequence with a switch and vice versa, it would be extremely bad practice to do this ... in most cases.

So you need to learn the two constructs, understand their strengths and weaknesses, and learn to use your judgement as to when it is appropriate to use each one.

And the mere fact that C and C++ and Java (and C# and Pascal and many other languages) all support switch statements should tell you something about its usefulness ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Difference between switch-case and if-else constructs:

  1. Switch-case switches on values only, it does not evaluates boolean expressions.
  2. Switch-case offers execution of next cases below it automatically if you don't use break after your case block. This feature is sometimes useful for writing complex code, like "Telephone Dial Plan"
  3. Switch-case are more elegant compared to if-else when the number of comparisons are huge, like in displaying "Menu", etc.
manav m-n
  • 11,136
  • 23
  • 74
  • 97