LOGBOOK

HELP

Quiz Entry - updated: 2026.07.14

When does a compiler choose a jump table versus an if-else chain (or decision tree) for a switch?

Jump table when the case values are dense (few gaps); an if-else chain for just a few cases; a balanced decision tree when there are many but sparse values.

A decision on case count, range, and density fans out to jump table for dense values, if-else chain for few cases, and decision tree for many sparse values.

* Density decides: jump table (dense), if-else chain (few cases), or decision tree (many sparse). *

The compiler weighs lookup speed against memory wasted on empty table slots:

Implementation Used when Lookup cost
Jump table Cases are dense over a small range O(1)
If-else chain Only a handful of cases O(n)
Decision tree Many cases, but spread far apart O(log n)

It decides based on the number of cases, the range of their values, and the resulting density (what fraction of the range is actually used).

  • case 1,2,3,5,6 → 7-entry table, mostly full → jump table (fast, little waste).
  • case 1, 100, 10000 → a table would need ~10000 entries for 3 cases → if-else / decision tree instead.

Why you care: the compiler, not you, picks the low-level form — and the same switch source can compile to entirely different machine code depending on how the case labels happen to cluster.

Go deeper:

From Quiz: REVE1 / Translation of C to Assembly | Updated: Jul 14, 2026