Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Switch Statement Works

How does a switch statement immediately drop to the correct location in memory? With nested if-statements, it has to perform comparisons with each one, but with a switch statement it goes directly to the correct case. How is this implemented?

like image 912
David Connolly Avatar asked Feb 09 '17 22:02

David Connolly


People also ask

How does a switch statement work in C?

A switch statement causes control to transfer to one labeled-statement in its statement body, depending on the value of expression . The values of expression and each constant-expression must have an integral type. A constant-expression must have an unambiguous constant integral value at compile time.

What is switch statement with an example?

Explanation: Switch statement only supports Integral data types, Any other data types will throw an Invalid type error. Other examples for Invalid switch: switch(4.5) , switch(10.0/7.1), switch(a + 4.5) etc. The part of the case clause must be a constant integral value or a constant expression followed by a colon.

How do switch statements work in Java?

The switch case in java executes one statement from multiple ones. Thus, it is like an if-else-if ladder statement. It works with a lot of data types. The switch statement is used to test the equality of a variable against several values specified in the test cases.

What is a switch statement and write its rules?

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.


1 Answers

There are many different ways to compile a switch statement into machine code. Here are a few:

  • The compiler can produce a series of tests, which is not so inefficient as only about log2(N) tests are enough to dispatch a value among N possible cases.

  • The compiler can produce a table of values and jump addresses, which in turn will be used by generic lookup code (linear or dichotomic, similar to bsearch()) and finally jump to the corresponding location.

  • If the case values are dense enough, the compiler can generate a table of jump addresses and code that checks if the switch value is within a range encompassing all case values and jump directly to the corresponding address. This is probably the implementation closest to your description: but with a switch statement it goes directly to the correct case.

Depending on the specific abilities of the target CPU, compiler settings, and the number and distribution of case values, the compiler might use one of the above approaches or another, or a combination of them, or even some other methods.

Compiler designers spend a great deal of effort trying to improve heuristics for these choices. Look at the assembly output or use an online tool such as Godbolt's Compiler Explorer to see various code generation possibilities.

like image 145
chqrlie Avatar answered Sep 21 '22 02:09

chqrlie