O'Reilly logo

SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION by Jesse Liberty

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

switch Statements

if and else...if combinations can become quite confusing when nested deeply, and C++ offers an alternative. Unlike if, which evaluates one value, switch statements enable you to change control flow based on any of a number of different values for an expression. The general form of the switch statement is

switch (expression)
{
   case constantexpression1:
					statement; break;
   case constantexpression2:
					statement; break;
   ....
   case constantexpression3:
					statement; break;
   default: statement;
}

expression is any legal C++ expression resulting in a character or other simple result (such as int or float), and the statements are any legal C++ statements or blocks.

switch evaluates expression and compares the result to each of the case constantexpression ...

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