Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,415 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,412 people online right now. Registration is fast and FREE... Join Now!




When are Switch statements appropriate

 
Reply to this topicStart new topic

When are Switch statements appropriate

INTERROBANG!?
15 Oct, 2008 - 10:36 AM
Post #1

New D.I.C Head
*

Joined: 7 Oct, 2008
Posts: 6


My Contributions
So i have a question...

i was searching around the forum and after looking at some advice people were giving started to wonder "When is a switch statement appropriate to use"(as opposed to a loop with a if statement to check for bad user input).

the only case i can think of is a somewhat complicated menu or such in which the choices can not be easily organized into a array or some other data type.

let me know what you guys think.

-INTERROBANG!?
User is offlineProfile CardPM
+Quote Post

ibaraku
RE: When Are Switch Statements Appropriate
15 Oct, 2008 - 11:06 AM
Post #2

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 164



Thanked: 1 times
My Contributions
QUOTE(INTERROBANG!? @ 15 Oct, 2008 - 11:36 AM) *

So i have a question...

i was searching around the forum and after looking at some advice people were giving started to wonder "When is a switch statement appropriate to use"(as opposed to a loop with a if statement to check for bad user input).

the only case i can think of is a somewhat complicated menu or such in which the choices can not be easily organized into a array or some other data type.

let me know what you guys think.

-INTERROBANG!?


I would say that I would choose a switch statement over if statements when if it makes your code more readable.
It is much easier to read this

CODE

switch (expr) {
  case c1:
        statements  // do these if expr == c1
        break;
        
  case c2:
        statements  // do these if expr == c2
        break;
        
  case c3:
          ...
  case c4
           ...
        
  default:
        statements
}


than this

CODE


if (expr==c1) {
    statements
} else if (expr==c2) {
    statements
} else if (expr==c2 || expr==c3 || expr==c4) {
    statements
} else {
    statements
}


User is online!Profile CardPM
+Quote Post

Hyper
RE: When Are Switch Statements Appropriate
15 Oct, 2008 - 11:03 PM
Post #3

D.I.C Head
**

Joined: 15 Oct, 2008
Posts: 134



Thanked: 3 times
My Contributions
QUOTE(INTERROBANG!? @ 15 Oct, 2008 - 11:36 AM) *

So i have a question...

i was searching around the forum and after looking at some advice people were giving started to wonder "When is a switch statement appropriate to use"(as opposed to a loop with a if statement to check for bad user input).

the only case i can think of is a somewhat complicated menu or such in which the choices can not be easily organized into a array or some other data type.

let me know what you guys think.

-INTERROBANG!?


To put it blunt without an example.

When you have say, 4 to five if-else statements chained together, it's alot easier to read & understand a switch statement than a chain, however. Switches are based on numerical values, sometimes you can't use a string (and converting strings with atoi() function is a whole other story) in a switch, but you can use them in if-else chains.
User is offlineProfile CardPM
+Quote Post

Linkowiezi
RE: When Are Switch Statements Appropriate
15 Oct, 2008 - 11:38 PM
Post #4

D.I.C Head
**

Joined: 7 Oct, 2008
Posts: 72



Thanked: 11 times
My Contributions
Switch statements also doesn't end if there ain't a break; somwhere.
So this would be possible:
cpp
switch (expr) {
case c1:
statements...
break; // we break here

case c2:
statements...
// it will continue to the next case since there is no break here

case c3:
statements...
break; // and here we reached a break
default:
break; // and we also reach a break here
}

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: When Are Switch Statements Appropriate
16 Oct, 2008 - 01:51 AM
Post #5

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,462



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
I will use a switch statement anytime that I have more than 2 if conditionals.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:47PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month