7 Replies - 301 Views - Last Post: 03 May 2019 - 07:08 AM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 709
  • Joined: 15-December 14

Questions about switches when to use them and not use them?

Posted 02 May 2019 - 07:47 PM

I'm at the end of the latest chapter of my book and one of the questions in the review at the end of the chapter. 14. When would a programmer prefer a switch-statement to an if-statement? I honestly can't think of anytime a switch would be more useful than using if statements or a turnary operator. Because with a switch you're limited to either using a char, enum or an integer. In the chapter when I was asked to make a program using a switch and then with if statements, I still found using if statements cleaner.

Would someone please explain to me when it would be more beneficial to use one?. I mean if its speed, a human wouldn't really notice the difference between the two in my opinion.

This is an honest question.

Is This A Good Question/Topic? 0
  • +

Replies To: Questions about switches when to use them and not use them?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15076
  • View blog
  • Posts: 60,208
  • Joined: 12-June 08

Re: Questions about switches when to use them and not use them?

Posted 02 May 2019 - 08:17 PM

A classic example is determining a letter grade for a number one. You can use implied ranges and pop out a letter.

Cleaning up a ton of if-else statements helps too.

Setting control access based on user level... done that before.
Was This Post Helpful? 0
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 709
  • Joined: 15-December 14

Re: Questions about switches when to use them and not use them?

Posted 02 May 2019 - 08:27 PM

Makes sense. I can see in your examples where it would be more useful to use a switch statement.

I do have another question for you. What do you mean by Setting control access based on user level?. Do you mean something like this?:
char people;
switch(people)
{
case 's'://>/student

case 't'://>/teacher

case 'p'://>/principal

default://everyone else
}

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15076
  • View blog
  • Posts: 60,208
  • Joined: 12-June 08

Re: Questions about switches when to use them and not use them?

Posted 02 May 2019 - 08:33 PM

Rudimentary example..

User has an "access level"

switch (access_level)
case 3: //read/write
// enable all textboxes
// enable all buttons
case 2: // limited write
// enable certain textboxes
//disable other textboxes
// enable certain buttons
//disable other buttons
case 1: // read only
//disable textboxes
//disable buttons
default: //none
//disable everything

Was This Post Helpful? 1
  • +
  • -

#5 albert003   User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 709
  • Joined: 15-December 14

Re: Questions about switches when to use them and not use them?

Posted 02 May 2019 - 08:35 PM

Ah ok. Thank you for your example.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7434
  • View blog
  • Posts: 15,411
  • Joined: 16-October 07

Re: Questions about switches when to use them and not use them?

Posted 03 May 2019 - 02:01 AM

First, I don't like switch and tend to avoid it. Given this bias, here are the only reasons I see to use them:

Two reasons, save a variable and elegant fall through:
int n = getValue(x);
if (n == 1 || n == 2)  {
    // thing1
} else if (n == 3)  {
    // thing2
} else {
   // thing3
}
// or
switch(getValue(x)) {
    case 1:
    case 2:
        // thing1
        break;
    case 3:
        // thing2
        break;
    default:
        // thing3
}


In old C the variable save was exceptionally handy as the language required all variables used to be declared at the top of the block. Also, from an optimization standpoint, such expressions can implicitly generate simple jump trees in assembly, as opposed to anything more complex. However, neither of these considerations are really still in play.

More functional languages make switch style statements more appealing, where the expression must exhaust all possible results or an error with be generated at compile. Indeed, in ML languages you'll often see admonishments like "you know, you can use if/then, you don't always have to use match (ML switch)."
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6929
  • View blog
  • Posts: 23,544
  • Joined: 05-May 12

Re: Questions about switches when to use them and not use them?

Posted 03 May 2019 - 07:04 AM

I think I mentioned this before, but your job as a programmer is to communicate your ideas to other programmers through code. It is only secondary that machines can also execute the code that you write. Sometimes if-else's will communicate an idea better, other times, a switch will communicate an idea better.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5732
  • View blog
  • Posts: 17,558
  • Joined: 25-December 09

Re: Questions about switches when to use them and not use them?

Posted 03 May 2019 - 07:08 AM

Quote

Because with a switch you're limited to either using a char, enum or an integer.

Really the type of variable is not the biggest limitation of a switch statement. The biggest limitation is that you must use a discrete value for each case statement, if you want a range then you should be using if() statements, and remember that you can always replace a switch statement with an if/else chain, but you can't always use a switch to replace if/else chains.

If you prefer using if/else chains then go for it, they can do every thing a switch statement can do.



Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1