Download presentation
Presentation is loading. Please wait.
1
The Java switch Statement
A multi-path decision statement Copyright © Curt Hill
2
Copyright © 2004-2017 - Curt Hill
Flow of Control in Java Recall the flow statements in Java: Decisions If Switch Case Loops for while do Break Here we consider the switch Copyright © Curt Hill
3
Copyright © 2004-2017 - 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 System.out.println(“Error”); Copyright © Curt Hill
4
Copyright © 2004-2017 - 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 Copyright © Curt Hill
5
Copyright © 2004-2017 - Curt Hill
If and Switch An if is a decision of one path out of two A switch is a decision of one path out of many An if is based on a boolean May only have two values A switch is based on an int, char, enumeration May have many values Copyright © Curt Hill
6
Copyright © 2004-2017 - Curt Hill
General Form of Switch switch (integral expression) { case con1: many statements break; case con2: many statements break; case con3: many statements break; … } Copyright © Curt Hill
7
Copyright © 2004-2017 - 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, byte or enumeration May not be floating point 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 Copyright © Curt Hill
8
Copyright © 2004-2017 - Curt Hill
Example switch (credit_hours / 32) { case 0: standing = “freshman”; break; case 1: standing = “sophomore”; case 2: standing = “junior”; case 3: standing = “senior”; } Copyright © Curt Hill
9
Copyright © 2004-2017 - 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 Copyright © Curt Hill
10
Copyright © 2004-2017 - Curt Hill
Multiple case example switch (grade) { case ‘a’: case ‘A’: points = 4 * credit_hours; break; case ‘b’: case ‘B’: points = 3 * credit_hours; … } Copyright © Curt Hill
11
Copyright © 2004-2017 - 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 Copyright © Curt Hill
12
Copyright © 2004-2017 - Curt Hill
Example Revisited switch (credit_hours / 32) { case 0: standing = “freshman”; break; … case 3: standing = “senior”; default: standing = “unknown”; System.out.println(“Error”); } Copyright © Curt Hill
13
Copyright © 2004-2017 - 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 Leaving it out is the same as doing nothing for all the unmentioned cases Copyright © Curt Hill
14
Copyright © 2004-2017 - 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 method not just the switch It is not required, but leaving it out makes for interesting flow Copyright © Curt Hill
15
Copyright © 2004-2017 - 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 Copyright © Curt Hill
16
Copyright © 2004-2017 - Curt Hill
Omitted break example switch (val) { case 5: x += 5; case 12: x += 8; y = 2; break; case 6: y = 4; } 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. Copyright © Curt Hill
17
Copyright © 2004-2017 - 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 Copyright © Curt Hill
18
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 Copyright © Curt Hill
19
Copyright © 2004-2017 - Curt Hill
Common mistakes We cannot use objects for the case expression or labels We cannot use floating point values (float or double) for the expression or case labels There are problems with floating point equality Copyright © Curt Hill
20
Copyright © 2004-2017 - Curt Hill
Java 7 Starting in Java 7 a string may be used Internally it converts into a sequence of nested if statements It is case sensitive Copyright © Curt Hill
21
Copyright © 2004-2017 - Curt Hill
Example int dayofweek; String day = scan.next(); switch (day) { case “Sunday”: dayofweek = 0; break; … case “Saturday”: dayofweek = 6; default: System.out.println(“Error”); } Copyright © Curt Hill
22
Copyright © 2004-2017 - Curt Hill
Nope! switch(yertle){ ... switch(real){ case 2.1: } Copyright © Curt Hill
23
Copyright © 2004-2017 - Curt Hill
Uses switch is much less used than an if Just not as much need 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 Copyright © Curt Hill
24
Copyright © 2004-2017 - Curt Hill
Ranges Similar statements may be able to handle a range of values, but not this one If the values between 1 and 5 inclusive are to use the same path then five case constructs are needed The only easy range is the default construct Copyright © Curt Hill
25
Copyright © 2004-2017 - Curt Hill
Finally The if chooses one of two paths, but the switch chooses one of many paths The new reserved words are: switch case default Copyright © Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.