In fact, before the compiler, how the switch is executed in the code.
However, I understand that when it is suitable (i.e. relatively dense cases), a transition table is used.
This would mean something like:
switch(i) { case 0: doZero(); break; case 1: doOne(); case 2: doTwo(); break; default: doDefault(); }
In the end, everything will be compiled to something like (a terrible pseudo-assembler, but, in my opinion, it should be clear).
load i into REG compare REG to 2 if greater, jmp to DEFAULT compare REG to 0 if less jmp to DEFAULT jmp to table[REG] data table ZERO ONE TWO end data ZERO: call doZero jmp END ONE: call doOne TWO: call doTwo jmp END DEFAULT: call doDefault END:
If this is not the case, there are other possible implementations that allow to some extent "better than a sequence of conditional expressions."
Vatine Dec 28 2018-12-12T00: 00Z
source share