... other than the the fact that in switch case you dont need to put 'if(myvar==' and you can use only constants.
Does the compiler use any special optimizations while using switch-case? If so what are they?
And if it doesnt do anything special with switch-case then why dont programming langs allows using variables too in 'case' statements instead of allowing only constants?
Edit: I tried to look at the disassembly with vc++ 2002 on optimized switch-case code but the debugger wont trace till there and it also cant show me the asm lines corresponding to the C lines.
Does the compiler use any special optimizations while using switch-case? If so what are they?
And if it doesnt do anything special with switch-case then why dont programming langs allows using variables too in 'case' statements instead of allowing only constants?
Edit: I tried to look at the disassembly with vc++ 2002 on optimized switch-case code but the debugger wont trace till there and it also cant show me the asm lines corresponding to the C lines.
Dear Clippy
Not all programming languages, just damn C/c++ will not allow expressions into switch case
The only real difference between switch/case and if/elseif/else/endif is.... none
However compared with simple if/else/endif (aka without elseif) is that: switch/case allows many IF's in the same statement block :P
I used to think thta all switch in C/C++ is done via jumptables, at least on MS VC++ it is :D
But somebody on the MASM board showed me clear example that GNU gcc(++) does NOT
Not all programming languages, just damn C/c++ will not allow expressions into switch case
The only real difference between switch/case and if/elseif/else/endif is.... none
However compared with simple if/else/endif (aka without elseif) is that: switch/case allows many IF's in the same statement block :P
I used to think thta all switch in C/C++ is done via jumptables, at least on MS VC++ it is :D
But somebody on the MASM board showed me clear example that GNU gcc(++) does NOT
I think i heard somewhere that using switch/case the compiler can create a sort of binary tree in the mem so finding a value is faster rather than comparing with each and every value? Is that true?
With switch, the compiler (if it is built to do it) can choose a so-called "optimal" solution.
In fact, the C compilers on the early PDP Unixes did choose one of three implementations depending on the distribution and number of case values. The three implementations were: 1) individual if-then comparisons for each case value, 2) one table of jump addresses indexed by case value, and 3) one search table containing <value, address> pairs.
I believe that Dennis Ritchie published years and years ago how the old Unix C compilers chose which option.
In fact, the C compilers on the early PDP Unixes did choose one of three implementations depending on the distribution and number of case values. The three implementations were: 1) individual if-then comparisons for each case value, 2) one table of jump addresses indexed by case value, and 3) one search table containing <value, address> pairs.
I believe that Dennis Ritchie published years and years ago how the old Unix C compilers chose which option.
...so finding a value is faster rather than comparing ... Is that true?
Like Bogdan said, it's true for VC++. :D In fact, true whenever possible - let's say you have "case 100:" and "case 5000" - it won't make a jump table :grin: . Nor would any asm coder. Unless there are cases 101...4999 :grin:
If I ever use C++, it'll be VC++ , as it optimizes very well.
and when not doing jumptables, it does the binary search divide and conquer strategy with the conditional jumps, resulting in the least possible amount of branches.