"Switch" vs "If-Then" 
Author Message
 "Switch" vs "If-Then"

As a C++ beginner, I would like to say that after playing around with the
Switch statement in C++ I find it to be pretty limiting, especially because
I am a VB programmer (C++ wannabe).

In VB I can do all sorts of things using the equivalent "Select Case"
statement. This is not what I was expecting form the C++ programming
language. Not that I have anything against C++, it's just that it has always
been glorified to me like the 8th wonder of the world and I was expecting to
see more options in all the programming features.

I also couldn't help noticing that the If-Then statement can pretty much do
what the Switch statement can do.

So the question is. What is the purpose for the Switch statement? Except for
making the code a little easier to read at times, I can't see any other
benefit. I am I missing something? Should I just use the If-Then and forget
about the Switch?

Just wondering.
Thanks.



Tue, 14 Oct 2003 11:01:48 GMT  
 "Switch" vs "If-Then"
Don't let yourself be fooled.  C++ blows the doors off VB in almost every area.
VB's advantages are (1) easier to learn, (2) harder to{*filter*}up, and (3) faster
to create simple GUI prototypes.  But C++ can turn cartwheels around VB in
performance, flexibility and ability.  The world needs VB'ers, but I wouldn't
want to be one.

Switch case is a cleaner construct when you would otherwise do this:
if (x == 1)
   // blah
else if (x == 2)
   // blah-blip
else if (x == 3)
   // blah-blum
else if (x == 4)
   // blah-blee
else if (x == 5)
   // blah-bloh
else
   // blah-de-blah

The equivalent switch is:
switch (x)
{
case 1:
   // blah
   break;
case 2:
   // blah-blip
   break;
case 3:
   // blah-blum
   break;
case 4:
   // blah-blee
   break;
case 5:
   // blah-bloh
   break;
default:
   // blah-de-blah

Quote:
}



Tue, 14 Oct 2003 11:38:34 GMT  
 "Switch" vs "If-Then"

Quote:

> As a C++ beginner, I would like to say that after playing around with the
> Switch statement in C++ I find it to be pretty limiting, especially because
> I am a VB programmer (C++ wannabe).

> In VB I can do all sorts of things using the equivalent "Select Case"
> statement. This is not what I was expecting form the C++ programming
> language. Not that I have anything against C++, it's just that it has always
> been glorified to me like the 8th wonder of the world and I was expecting to
> see more options in all the programming features.

> I also couldn't help noticing that the If-Then statement can pretty much do
> what the Switch statement can do.

> So the question is. What is the purpose for the Switch statement? Except for
> making the code a little easier to read at times, I can't see any other
> benefit. I am I missing something? Should I just use the If-Then and forget
> about the Switch?

> Just wondering.
> Thanks.

The design of the C and C++ languages was done without a marketing
department.  No glory, no wonders, very few "features".  In fact,
awesomely few keywords.  These are strengths for those who want
performance, who want to do what the hardware is capable of doing.  C
and C++ are molded by the way computers really work.  They just barely
hide it and make all of it accessible efficiently.  

The purpose of the switch statement (when the language was developed)
was to make jump tables.  There is no faster way to do a multiway
branch on a CPU.  The case constants are indexes into a table of
function addresses.  What you have here is a racing car and you are
looking for the power windows.  You will find such things in addon
libraries, but not in the language.  Lean and mean, and powerful
enough to build operating systems :)

--
Scott McPhillips [VC++ MVP]



Tue, 14 Oct 2003 11:48:06 GMT  
 "Switch" vs "If-Then"
Don't forget the ability to combine cases.  If you want to do the same thing in
2 different case, but would like to add an action to one, you can combine the
code.

switch()
{
    case 0:
        printf();
    case1:
        SomeStuffToDo();
        break;
    case 2:
        OtherStuff();

Quote:
}

In this switch, not having a break after case 0 will cause the code to drop to
case 1 and do the code there also.

Quote:

> As a C++ beginner, I would like to say that after playing around with the
> Switch statement in C++ I find it to be pretty limiting, especially because
> I am a VB programmer (C++ wannabe).

> In VB I can do all sorts of things using the equivalent "Select Case"
> statement. This is not what I was expecting form the C++ programming
> language. Not that I have anything against C++, it's just that it has always
> been glorified to me like the 8th wonder of the world and I was expecting to
> see more options in all the programming features.

> I also couldn't help noticing that the If-Then statement can pretty much do
> what the Switch statement can do.

> So the question is. What is the purpose for the Switch statement? Except for
> making the code a little easier to read at times, I can't see any other
> benefit. I am I missing something? Should I just use the If-Then and forget
> about the Switch?

> Just wondering.
> Thanks.

--
Topochicho
Houston, TX
00 Nomad FI Silver/Burgandy
Corbin Dual Tour
VROC #4938


Tue, 14 Oct 2003 22:27:13 GMT  
 "Switch" vs "If-Then"


Quote:
> Don't let yourself be fooled.  C++ blows the doors off VB in almost every
area.
> VB's advantages are (1) easier to learn, (2) harder to{*filter*}up, and (3)
faster
> to create simple GUI prototypes.  But C++ can turn cartwheels around VB in
> performance, flexibility and ability.  The world needs VB'ers, but I
wouldn't
> want to be one.

> Switch case is a cleaner construct when you would otherwise do this:
> if (x == 1)
>    // blah
> else if (x == 2)
>    // blah-blip
> else if (x == 3)
>    // blah-blum
> else if (x == 4)
>    // blah-blee
> else if (x == 5)
>    // blah-bloh
> else
>    // blah-de-blah

> The equivalent switch is:
> switch (x)
> {
> case 1:
>    // blah
>    break;
> case 2:
>    // blah-blip
>    break;
> case 3:
>    // blah-blum
>    break;
> case 4:
>    // blah-blee
>    break;
> case 5:
>    // blah-bloh
>    break;
> default:
>    // blah-de-blah
> }

Please explain why the switch version is clearer?

NeilB



Tue, 14 Oct 2003 22:01:56 GMT  
 "Switch" vs "If-Then"
If you use correct indentation, the if-then (I'll call it if-else
for obvious reasons) would read:

if (x==1) {

Quote:
}

else
    if (x==2) {
    }
    else
        if (x==3) {
        }
        ...

An awful lot of indenting, not easy to read. OTOH, there's a
performance difference. Coding if-else means at runtime, every
if has to be tested until a TRUE is met. That's not true with switch.
switch uses a branch table where the input expression serves as
a branch table index. Much faster! Also, you might uitlize
non-break switch cases, ie. kind of fall through, which is not easily
possible with if-else.

Yours, Nikolaus



Tue, 14 Oct 2003 23:54:10 GMT  
 "Switch" vs "If-Then"


Quote:
> If you use correct indentation, the if-then (I'll call it if-else
> for obvious reasons) would read:

> if (x==1) {
> }
> else
>     if (x==2) {
>     }
>     else
>         if (x==3) {
>         }
>         ...

> An awful lot of indenting, not easy to read.

If you indent like that. I would say the "correct" way of indenting this is

if (x==1) {

Quote:
}
else if (x==2) {
}
else if (x==3) {
}

Indeed, I've _never_ seen it done otherwise.

Quote:
> OTOH, there's a
> performance difference. Coding if-else means at runtime, every
> if has to be tested until a TRUE is met. That's not true with switch.
> switch uses a branch table where the input expression serves as
> a branch table index.

Might do, might not. The language standard has nothing to say about this.

Quote:
> Much faster! Also, you might uitlize
> non-break switch cases, ie. kind of fall through, which is not easily
> possible with if-else.

Yes, and how many bugs have been caused by this so-called "feature".

NeilB



Tue, 14 Oct 2003 23:08:49 GMT  
 "Switch" vs "If-Then"
It's also clearer that EVERY condition is a comparison against possible values
of x.  With the if-else if construct, it can be much less obvious to the reader.


Wed, 15 Oct 2003 01:39:28 GMT  
 "Switch" vs "If-Then"

Quote:

>If you indent like that. I would say the "correct" way of indenting this is

There are at least three different indenting methods in common use
(A,B,C below) :

if (test) {
    code

Quote:
}

if (test)
{
    code

Quote:
}

if (test)
  {
    code
  }

There is NO "correct" one. Except for B, which is the one I use, and
therefore is obviously, by definition, correct :-)

Feel free to start a holy war here. I fully intend to bow out at this
point...

--
Bob Moore [WinSDK MVP]
http://www.mooremvp.freeserve.co.uk/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Do not reply via email unless specifically requested to do so.
Unsolicited email is NOT welcome and will go unanswered.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Wed, 15 Oct 2003 05:51:00 GMT  
 "Switch" vs "If-Then"
I also use 'B'. I find 'A' hard to follow when reviewing someone else's code.

Quote:

> There are at least three different indenting methods in common use
> (A,B,C below) :

> if (test) {
>     code
> }

> if (test)
> {
>     code
> }

> if (test)
>   {
>     code
>   }

> There is NO "correct" one. Except for B, which is the one I use, and
> therefore is obviously, by definition, correct :-)

> Feel free to start a holy war here. I fully intend to bow out at this
> point...

> --
> Bob Moore [WinSDK MVP]
> http://www.mooremvp.freeserve.co.uk/

Phil Frisbie, Jr.
Lead Developer, Hawk Software
http://www.hawksoft.com


Wed, 15 Oct 2003 06:17:29 GMT  
 "Switch" vs "If-Then"
A is very easy to follow, but you do have to adjust your thinking slightly.
Instead of looking for the opening brace to match up, you look for the
beginning of the statement (in this case the "if").  By using the beginning
of the statement as the "anchor" rather than the opening brace, you save one
line of code  for each block statement, which can give you as much as 10 or
15 more lines of code visible on the screen, without effecting readability
(for anyone familiar with the style).

I used to be the other way around, until I had the epipheny.  It just looks
better and is easier to read for me now.



Quote:
> I also use 'B'. I find 'A' hard to follow when reviewing someone else's
code.


> > There are at least three different indenting methods in common use
> > (A,B,C below) :

> > if (test) {
> >     code
> > }

> > if (test)
> > {
> >     code
> > }

> > if (test)
> >   {
> >     code
> >   }

> > There is NO "correct" one. Except for B, which is the one I use, and
> > therefore is obviously, by definition, correct :-)

> > Feel free to start a holy war here. I fully intend to bow out at this
> > point...

> > --
> > Bob Moore [WinSDK MVP]
> > http://www.mooremvp.freeserve.co.uk/

> Phil Frisbie, Jr.
> Lead Developer, Hawk Software
> http://www.hawksoft.com



Wed, 15 Oct 2003 06:36:28 GMT  
 "Switch" vs "If-Then"

Quote:

> >If you indent like that. I would say the "correct" way of indenting this
is

> There are at least three different indenting methods in common use
> (A,B,C below) :

> if (test) {
>     code
> }

> if (test)
> {
>     code
> }

> if (test)
>   {
>     code
>   }

> There is NO "correct" one. Except for B, which is the one I use, and
> therefore is obviously, by definition, correct :-)

Notice the quote marks around "correct" in my post?

And C above is definitely not correct - I allow the first two when I manage
projects, but have a complete ban on the last one.

NeilB



Wed, 15 Oct 2003 05:53:08 GMT  
 "Switch" vs "If-Then"


Quote:
> I also use 'B'. I find 'A' hard to follow when reviewing someone else's

code.

People like Dennis Ritchie  disagree with you - but hey, what does he know
:-)

NeilB



Wed, 15 Oct 2003 05:55:12 GMT  
 "Switch" vs "If-Then"

Quote:

> What you have here is a racing car and you are
>looking for the power windows.

I love that comment!


Wed, 15 Oct 2003 09:41:43 GMT  
 "Switch" vs "If-Then"

Also, The 'switch' statement can not be used for ranges which require the
use of 'if' statement.
M


  As a C++ beginner, I would like to say that after playing around with the
  Switch statement in C++ I find it to be pretty limiting, especially
because
  I am a VB programmer (C++ wannabe).

  In VB I can do all sorts of things using the equivalent "Select Case"
  statement. This is not what I was expecting form the C++ programming
  language. Not that I have anything against C++, it's just that it has
always
  been glorified to me like the 8th wonder of the world and I was expecting
to
  see more options in all the programming features.

  I also couldn't help noticing that the If-Then statement can pretty much
do
  what the Switch statement can do.

  So the question is. What is the purpose for the Switch statement? Except
for
  making the code a little easier to read at times, I can't see any other
  benefit. I am I missing something? Should I just use the If-Then and
forget
  about the Switch?

  Just wondering.
  Thanks.



Thu, 16 Oct 2003 14:51:33 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. remove() vrs fopen("""w")

2. Displaying binary data as ascii "1"'s and "0"'s

3. Looking for "Shroud"/"Obfus"

4. ""help with TSR""

5. Parse trees and "("")"

6. Error "free"-ing "malloc"-ed memory

7. Displaying binary data as ascii "1"'s and "0"'s

8. Attention "C"/"C++" gurus and "C" newbees

9. merits of "#define", "const", and "enum" ???

10. why not "cout", "<<" , and "endl"??

11. "Switching off" loading of resource files

12. "#define" versus "con

 

 
Powered by phpBB® Forum Software