Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2004-2005 - Curt Hill The C/C++ switch Statement A multi-path decision statement.

Similar presentations


Presentation on theme: "Copyright 2004-2005 - Curt Hill The C/C++ switch Statement A multi-path decision statement."— Presentation transcript:

1 Copyright 2004-2005 - Curt Hill The C/C++ switch Statement A multi-path decision statement

2 Copyright 2004-2005 - Curt Hill Flow of Control in C/C++ Recall the flow statements in C and C++: –Decisions If Switch Case –Loops for while do Break Here we consider the switch

3 Copyright 2004-2005 - Curt Hill Why? There are times when we would use the following type of nested if if(a == 2) x = 5; else if(a == 4) { x = 12 * y; … } else if(a == 8) {…} else if(a == 12) {…} else cout << “Error”;

4 Copyright 2004-2005 - Curt Hill General Form of This Each if has a similar condition –One variable compared with a constant –Comparison is always equality The Then statement does something The Else statement does another if There is only one variable and one comparison in all the ifs Generally such a construction needs a switch-case not an if

5 Copyright 2004-2005 - Curt Hill If and Switch An if is a decision of one path out of two –A bool has only two values A switch is a decision of one path out of many A switch is based on an int, char, enumeration –Which may have many values

6 Copyright 2004-2005 - Curt Hill General Form of Switch switch (integral expression) { case con1: many statements break; case con2: many statements break; case con3: many statements break; … }

7 Copyright 2004-2005 - Curt Hill Notes switch, case and break are reserved words The parenthesized expression is evaluated to obtain a value –This value chooses the path –The type of the value must be an integer, character or enumeration –May not be floating point, string or any object The case indicates a path –Must be followed by a constant –The constant value following the case must match the type of the expression –It may be a defined, literal or constant declaration

8 Copyright 2004-2005 - Curt Hill Example switch (credit_hours / 32) { case 0: standing = “freshman”; break; case 1: standing = “sophomore”; break; case 2: standing = “junior”; break; case 3: standing = “senior”; break; }

9 Copyright 2004-2005 - Curt Hill More Notes There is only one set of braces which surrounds all the cases The path starts with a case and ends with a break Multiple cases allow for multiple ways to identify a path The cases do not have to be in any particular order Duplicate cases are not allowed –Compiler actually checks for this

10 Copyright 2004-2005 - Curt Hill Multiple case example switch (grade) { case ‘a’: case ‘A’: points = 4 * credit_hours; break; case ‘b’: case ‘B’: points = 3 * credit_hours; break; … }

11 Copyright 2004-2005 - Curt Hill Missing Path What happens if the switch expression does not match any case label? Equivalent to the correct case followed by a break –It does nothing The default label allows a catch all case

12 Copyright 2004-2005 - Curt Hill Example Revisited AnsiString standing; switch (credit_hours / 32) { case 0: standing = “freshman”; break; … case 3: standing = “senior”; break; default: standing = “unknown”; cout << “Error”; }

13 Copyright 2004-2005 - Curt Hill default default is a reserved word The default label acts like a case label with out case or specifying a value It catches any value not explicitly mentioned in a case It is customarily last, but does not have to be there Leaving it out is the same as doing nothing for all the unmentioned cases

14 Copyright 2004-2005 - Curt Hill The Break Statement The break has the effect of leaving a switch or loop The last path does not need a break A return will also work since it leaves the entire function/method not just the switch It is not required, but leaving it out makes for interesting flow

15 Copyright 2004-2005 - Curt Hill Leaving out a break If a break is left out, execution will drop into the next part Leaving out the last break is not a problem since there is no next part Consider the following example

16 Copyright 2004-2005 - Curt Hill Omitted break example switch (val) { case 5: x += 5; case 12: x += 8; y = 2; break; case 6: x += 8; y = 4; break; } If val is 5, x will have 13 added to it and y will be set to 2. All the effect of val == 12 is also executed for x == 5.

17 Copyright 2004-2005 - Curt Hill Omitted break notes This feature, inherited from C, is both good and bad It is somewhat good in that it allows an economy of code –Very few switches actually use this feature It is usually bad because we seldom want it but the compiler never gives an error when a break is left out

18 Copyright 2004-2005 - Curt Hill Equivalence of if and switch We have already seen an if doing what a switch can do The reverse is also true: switch(b>c){ case true: …. break; case false: …. break; } Always use what is more natural

19 Copyright 2004-2005 - Curt Hill Common mistakes We cannot use strings or other objects for the case expression or labels We cannot use floating point values (float or double) for the expression or case labels –Recall the problems with floating point equality

20 Copyright 2004-2005 - Curt Hill Uses switch is much less used than an if –Just not as much need for it Often used to decode an integer or enumeration into a string –Month number into a name Used to select different actions for these as well Often used for a menu in console programs

21 Copyright 2004-2005 - Curt Hill Readability Indenting things is again the rule My choice: –Indent the case –Indent the code past the case Example: switch (item) { case 1: duh = duh2; … break case 2: … }

22 Enumerations and Switch We frequently use switch to input, output and decode enumerations Lets look at enumeration examples Suppose: enum colortype {ctRed, ctGreen, ctBlue, ctWhite, ctBlack} color; Copyright 2004-2005 - Curt Hill

23 Display For displayable output use a switch: String s = “None”; switch (color) { case ctRed: s = “Red”; break; case ctGreen: s = “Green”; break; … } // end of switch Copyright 2004-2005 - Curt Hill

24 Copyright © 2005-2007 Curt Hill Input Several approaches Read in integer and use switch case to make into enumeration Read in string and use nested ifs to create enumeration

25 Input with integer In a console program: cout > val; switch (val){ case 0: color = ctRed; break; case 1: … The same may be done using text input from windows Copyright 2004-2005 - Curt Hill

26 Decoding Decoding is using an enumeration to perform different actions Consider: switch (action){ case aExit: Close(); break; case aAbout: Application->MessageBox(… … Copyright 2004-2005 - Curt Hill

27 Finally Switch is the one of many decision Switch is the least used decision –Many more ifs than switches –Less frequently used than most loops Use it when the pattern is seen if (a==1) … else if (a==3) … else if (a==2) … Copyright © 2005-2007 Curt Hill


Download ppt "Copyright 2004-2005 - Curt Hill The C/C++ switch Statement A multi-path decision statement."

Similar presentations


Ads by Google