O'Reilly logo

Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

ex10.c

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required