Multiway Branch
   HOME

TheInfoList



OR:

Multiway branch is the change to a program's
control flow In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an ''imper ...
based upon a value matching a selected criteria. It is a form of conditional statement. A multiway branch is often the most efficient method of passing control to one of a set of program labels, especially if an
index Index (or its plural form indices) may refer to: Arts, entertainment, and media Fictional entities * Index (''A Certain Magical Index''), a character in the light novel series ''A Certain Magical Index'' * The Index, an item on a Halo megastru ...
has been created beforehand from the
raw data Raw data, also known as primary data, are ''data'' (e.g., numbers, instrument readings, figures, etc.) collected from a source. In the context of examinations, the raw data might be described as a raw score (after test scores). If a scientist ...
.


Examples

*
Branch table In computer programming, a branch table or jump table is a method of transferring program control (Branch (computer science), branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of b ...
*
Switch statement In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map. Switch statements function some ...
- see also alternatives below *
Multiple dispatch Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of ...
- where a subroutine is invoked and a return is made


Alternatives

A multiway branch can, frequently, be replaced with an efficient indexed table lookup (using the data value itself or a calculated derivative of the data value, as the index of an
array An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
)
"''...the implementation of a switch statement has been equated with that of a multiway branch. However, for many uses of the switch statement in real code, it is possible to avoid branching altogether and replace the switch with one or more table look-ups. For example,the Has30Days example resented earliercan be implemented as the following: example''"A Superoptimizer Analysis of Multiway Branch Code Generation"
by Roger Anthony Sayle switch (x) can be replaced, using a "safe-hashing" technique, with - unsigned int t = x , 2; switch (t) or it can be replaced, using an
index mapping Index mapping (or direct addressing, or a trivial hash function) in computer science describes using an array, in which each position corresponds to a key in the universe of possible values. The technique is most effective when the universe of keys ...
table lookup, with - x %= 12; /* to ensure x is in range 0-11*/ static const int T 2=; /* 0-based table 'if 30 days =1,else 0' */ return T /* return with boolean 1 = true, 0=false */ (in view of the simplicity of the latter case, it would be preferable to implement it in-line, since the overhead of using a function call may be greater than the indexed lookup itself.)


Quotations


See also

*
Conditional (programming) In computer science, conditionals (that is, conditional statements, conditional expressions and conditional constructs,) are programming language commands for handling decisions. Specifically, conditionals perform different computations or actio ...
*
Lookup table In computer science, a lookup table (LUT) is an array that replaces runtime computation with a simpler array indexing operation. The process is termed as "direct addressing" and LUTs differ from hash tables in a way that, to retrieve a value v wi ...


References

{{Reflist


External links


Coding Multiway Branches Using Customized Hash functions
by H. G. Dietz
Learning Python
By Mark Lutz
Programming in C++
By Nell B. Dale, Chip Weems
A Superoptimizer Analysis of Multiway Branch Code Generation
by Roger Anthony Sayle Conditional constructs