Exercise 10. Switch Statements
In other languages, like Ruby, you have a switch-statement
that can take any expression. Some languages, like Python, don’t have a switch-statement
because an if-statement
with Boolean expressions is about the same thing. For these languages, switch-statements
are more like alternatives to if-statements
and work the same internally.
In C, the switch-statement
is actually quite different and is really a jump table. Instead of random Boolean expressions, you can only put expressions that result in integers. These integers are used to calculate jumps from the top of the switch
to the part that matches that value. Here’s some code to help you understand this concept of jump tables:
Get Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.