O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

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

The switch Statement

The switch statement is a mechanism by which you can test a given scalar variable against a set of discrete values. If you have an integer variable that you want to test against a limited set of constant values, you have two choices:

  • A series of if statements.

  • A multipart switch statement.

The key points to note are that the switch statement is available only when you’re testing for integer values, and you can only test for equality against a constant value. You cannot test for a variance (between one value and another), nor can you test floating-point values. The final restriction is that you cannot use a variable as a value to test against.

The generic syntax for the switch statement emulates multiple if/else if statements ...

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