The branch table construction is commonly used when programming in assembly language but may also be generated by compilers, especially when implementing optimized switch statements whose values are densely packed together.
It usually consists of the following 3 steps: The following pseudocode illustrates the concept Another method of implementing a branch table is with an array of pointers from which the required function's address is retrieved.
The resulting list of pointers to functions is almost identical to direct threaded code, and is conceptually similar to a control table.
PL/I label variables are not simply the address of the statement, but usually contain additional information on the state of the code block to which they belong.
Programmers frequently leave the decision of whether or not to create a branch table to the compiler, believing that it is perfectly capable of making the correct choice from the known search keys.
However, compilers are not as intelligent as humans and cannot have a deep knowledge of 'context', believing that a range of possible search key integer values such as 1, 2, 4, 6, 7, 20, 23, 40, 42, 50 & 1000 would generate a branch table with an excessively large number of empty entries (900+) for very little advantage.
A good optimizing compiler may then presort the values and generate code for a binary chop search, as a 'second best' option.