Quick question concerning switch statements

This is a discussion on Quick question concerning switch statements within the C++ Programming forums, part of the General Programming Boards category; Didn't I say that in the first place....?
Switches have to be constant...so you could just use a series of ...

As I understand it, switch statement is kind of like an array of instructions. A jump table is created, which is like an array of pointers to the code for each case. I.e., for case 1, the code at address jump_table[1] is executed. case 2, the code at jump_table[2] is executed, and so on. That makes using the switch much faster than a long chain of if/else statements, for the same reason that it's quicker to go to the nth element in an array than it is in a linked list (although a good compiler might optimize away that long chain of if/else statements into something like a switch). I know I'm not answering your question, but I hope it clears switches up a bit.

Also, I think statements like 0 || 1, 1 || 2, 2 || 3, etc. evaluate (as integers) to 1 if either operand is non-zero, and zero in the case 0 || 0, since || is a boolean and not an integer operator.

As I understand it, switch statement is kind of like an array of instructions. A jump table is created, which is like an array of pointers to the code for each case. I.e., for case 1, the code at address jump_table[1] is executed. case 2, the code at jump_table[2] is executed, and so on. That makes using the switch much faster than a long chain of if/else statements, for the same reason that it's quicker to go to the nth element in an array than it is in a linked list (although a good compiler might optimize away that long chain of if/else statements into something like a switch). I know I'm not answering your question, but I hope it clears switches up a bit.

Also, I think statements like 0 || 1, 1 || 2, 2 || 3, etc. evaluate (as integers) to 1 if either operand is non-zero, and zero in the case 0 || 0, since || is a boolean and not an integer operator.

Did you compile with no optimization? A switch statement shouldn't result in comparisons being done for every possible value (the list of cmp and je statements bunched together in the first example). It kind of defeats the purpose of having a switch statement in the language (an if/else chain is certainly more readable and less error prone, since it doesn't need the case labels or break statements).

Maybe the assembly for the switch program came out like that since there are so few cases (0,1,2, and default).

Did you compile with no optimization? A switch statement shouldn't result in comparisons being done for every possible value (the list of cmp and je statements bunched together in the first example). It kind of defeats the purpose of having a switch statement in the language (an if/else chain is certainly more readable and less error prone, since it doesn't need the case labels or break statements).

Maybe the assembly for the switch program came out like that since there are so few cases (0,1,2, and default).

I used VC++ 2008 express in Release mode. Maybe gcc could optimize better?

Although I don't see how a switch could possibly not check each case? It would have to be psychic then. I can try adding more cases to see if that makes a difference...
One more thing that has me confused is why VC++ used a sub instruction on the 3rd line after main() in the switch version and a push in the if/else version? I'm not that great with assembly, so I don't have a clue why it's different.

Since it won't execute ALL the satisfied branches, just the 1st satisfied branch.

I think the conditional fallthrough might be my solution.

While someone provided an answer here is another possible one:

Code:

switch(x)
{
case 0:
cout<<"X is either 0 or 1"<<endl;
break;
case 1:
cout<<"X is either 0 or 1"<<endl;
cout<<"X is either 1 or 2"<<endl;
break;
case 2:
cout<<"X is either 1 or 2"<<endl;
cout<<"X is either 2 or 3"<<endl;
break;
case 3:
cout<<"X is either 2 or 3"<<endl;
break;
}

I used VC++ 2008 express in Release mode. Maybe gcc could optimize better?

Although I don't see how a switch could possibly not check each case? It would have to be psychic then. I can try adding more cases to see if that makes a difference...
One more thing that has me confused is why VC++ used a sub instruction on the 3rd line after main() in the switch version and a push in the if/else version? I'm not that great with assembly, so I don't have a clue why it's different.

My best guess is that the compiler determined the brute force way wasn't too bad with 4 cases. Either that, or MS wants you to buy the pro compiler to get the benefits of using jump tables in switches (wow, that would suck if VC Express is crippled that badly).

Originally Posted by Computer Organization and Design: The Hardware / Software Interface

Case/Switch Statement

Most programming languages have a case or switch statement that allows the programmer to select one of many alternatives depending on a single value. The simplest way to implement switch is via a sequence of conditional tests, turning the switch statement into a chain of if-then-else statements.

Sometimes the alternatives may be more efficiently encoded as a table of addresses of alternative instruction sequences, called a jump address table, and the program needs only to index into the table and then jump to the appropriate sequence. The jump table is then just an array of words containing the addresses that correspond to labels in the code.

Maybe Microsoft just hates C++ and is sabotaging it to force you to use C#. :lol

No seriously, in Visual Studio 2005 (Pro!) you'll get deprecation warnings when using STL algorithms. I'll give you an example of of copying an array using STL: