What's good about using switch?

I just notice that it's used a lot. It seams more convenient and intuitive to just use a series of if statements. Is there any reason why a switch statement might be better in any case?
A switch generates more efficient compiled code than a series of ifs

Note that a switch is less flexible, you could have inequalities etc in your series of ifs, but a switch needs a single control variable.
A switch generates more efficient compiled code than a series of ifs

Not it doesn't. Most modern compilers would probably produce the same machine code independent of whether ifs or a switch was used.

It seams [sic] more convenient and intuitive to just use a series of if statements.

Actually no, it doesn't. And how is it more "convenient" to write a huge series of if statements in which you'd have to look at every single one every time to see what it actually checks for?

Anyways: Both, switch statements and if-series are often completely unnecessary. Consider this:

If:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int daysAMonth(int month, bool leapYear)
{
if(month==1)
{
return 31;
} else if (month==2)
{
if(leapYear)
{
return 29;
} else {
return 28;
}
} else if...
}


switch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int daysAMonth(int month, bool leapYear)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;

case 4:
case 6:
case 9:
case 11:
return 30;

case 2:
if(leapYear)
{
return 29;
} else {
return 28;
}

default:
return -1;
}
}


With an array:

1
2
3
4
5
6
7
int daysAMonth(int month, bool leapYear)
{
int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
month--;
month = month%12;
return (month == 2 && leapYear) ? days[month]+1 : days[month];
}


And there are even better solutions out there, those are just examples.
Last edited on
as what i was taught, for beginners in programming, switches are usually used for menus for its quick and hassle free redirection. for example

1
2
3
4
5
6
switch (input)
{
  case 1: abc(); break;
  case 2: def(); break;
  //and so on
}

if-else statements pretty much covers the rest as if-else statements can cover a range.
Here is an example of where I used an if statement. You could accomplish the exact same thing using switch. My teachers would probably have used a switch. It is easy for me to do it like this, and because I can just copy and past a whole bunch at a time, and change the conditions and arguments using the direction keys really fast.

I'm not saying I think there is something wrong with switch, i just prefer using the ifs. I was just wondering, if people who see my code will think less of me as a programer because I didn't use a switch?


if (key == 'q') alSourcePlay(source[WAV1]);
if (key == 'w') alSourcePlay(source[WAV2]);
if (key == 'e') alSourcePlay(source[WAV3]);
if (key == 'r') alSourcePlay(source[WAV4]);
if (key == 't') alSourcePlay(source[WAV5]);
if (key == 'y') alSourcePlay(source[WAV6]);
if (key == 'u') alSourcePlay(source[WAV7]);
if (key == 'i') alSourcePlay(source[WAV8]);
if (key == 'o') alSourcePlay(source[WAV9]);
if (key == 'p') alSourcePlay(source[WAV10]);
if (key == 'a') alSourcePlay(source[WAV11]);
if (key == 's') alSourcePlay(source[WAV12]);
if (key == 'd') alSourcePlay(source[WAV13]);
if (key == 'f') alSourcePlay(source[WAV14]);
if (key == 'g') alSourcePlay(source[WAV15]);
if (key == 'h') alSourcePlay(source[WAV16]);
if (key == 'j') alSourcePlay(source[WAV17]);
I think it would be better to use a switch there. It makes more sense to me as the programmer. One of the major advantages to using cleaner syntax is that other programmers are more able to read and interpret your code, and therefore will be better able to understand and modify your code if they need something that's slightly different than what you wrote, without having to re-invent the wheel.
Topic archived. No new replies allowed.