SWITCH STATEMENT against IF ELSE IF STATEMENT in C ++

I have a technical task, guys here, the switch statement is faster, this thing I know.

but I want to know how this works faster than if and else if?

how can he find a suitable controlExpression case among all his cases?

and if I assumed that it was written using if else, if it starts itself and finds a suitable case, so it should not be executed faster, it will work the same as if else, if?

so please answer me? thanks in advance

+3
source share
1 answer

switch : var == a, var == b, var == c ..

, , "" :

  1. switch case - , case 3: ... case 4: ... case 5: .... - , . , if-else if. ( , , .)
  2. switch case - , case 12: ... case 106: ... case 9: .... if-else if, if-else if.
  3. switch , , case - , , O(log(n)) , . ( , , , , .)

, , : , , 3x+5, , ((caseNum - 5) / 3), - (, , goto, -style. ""). case "-style O(1).

+5

All Articles