Title: Switch statements
1Switch statements
- Test next week over conditional statements (if,
ifelse, and switch)
2Review
- int x 3
- int y 2
- if((xgt5)(y2))
-
- System.out.println("Yes")
- else
-
- System.out.println("No")
-
-
3Review
- int x 3
- int y 2
- if(((xgt5)(y2))(x!3))
-
- System.out.println("Yes")
- else
-
- System.out.println("No")
-
-
4Review
- int x 3
- int y 2
- if((xgt5)(y2))
-
- System.out.println("Yes")
- else
-
- System.out.println("No")
-
-
5Review
- String name "Bob"
- if(name.equalsIgnoreCase("bob"))
-
- System.out.println("Yes")
- else
-
- System.out.println("No")
-
-
6Review
- String name "Bob"
- if(name.equalsIgnoreCase("bob"))
-
- if(name.equals("bob")
-
- System.out.println("A")
- else
- System.out.println("B")
-
- else
-
- System.out.println("C")
-
7Review
- char letter 'r'
- if(lettergt'z')
-
- System.out.println("Yes")
-
- else
-
- System.out.println("No")
-
8switch statements
- The last major decision statement is the switch
statement - The switch statement is the equivalent of doing a
lot of if statements. - Although it sounds like a great thing, because of
a few limitations it is actually not used very
often.
9switch statements
- Think of a switch as a menu on a game, where you
can select one of several choices. - You have one variable (the menu selection) that
you are working with, and it can take several
values. Depending on which one is chosen, the
program will do different things
10switch syntax
- switch(variableName)
-
- case value1 statement(s)
- break
- case value2 statement(s)
- break
- case value3 statement(s)
- break
- default statement(s)
-
- Note the variableName can only be a char, byte,
short, or int variable - you cannot use double, float, long, boolean, or
Object variables with switch!!!!!!!
11Statement
variable
Case 3 Statement(s)
Case 2 Statement(s)
Case 1 Statement(s)
Statement
12switch in action (going with a game menu of some
sort)
- int menuChoice //get input from user
- switch(menuChoice)
-
- case 0 game.startGame() break
- case 1 game.showInstructions()
break - case 2 game.exitGame()
break - default System.out.println("Invalid choice")
13switch in action (with a char variable)
- String input JOptionPane.showInputDialog("How
are you feeling?") - char mood input.charAt(0) //first character
of whatever they entered - switch(mood)
-
- case 'a' System.out.println("Angry")
- break
- case 'h' System.out.println("Happy")
- break
- case 's' System.out.println("Sad")
- break
- default System.out.println("Unknown")
- break
-
14switch
- Make sure you include the word "break" after
each case otherwise, all the cases below the one
that was selected will execute as well. This can
be used to your advantage (see next slide) but
can also cause errors if you don't mean to do it - The value that goes next to the word "case" must
match the type of the variable being used in the
switch. - You can only use single literals/values for each
case. You cannot say - case xgt4 statement(s)
15switch in action (multiple cases doing the same
thing)
- String input JOptionPane.showInputDialog("How
are you feeling?") - char mood input.charAt(0) //first character
of whatever they entered - switch(mood)
-
- case 'a' case 'A'System.out.println("Angry")
- break
- case 'h'
- case 'H'System.out.println("Happy")
- break
- case 's'
- 'S'System.out.println("Sad")
- break
- default System.out.println("Normal")
- break
-
16switch
- The "default" case is what gets executed if the
value of the variable being switched on doesn't
match any of the other cases. Its use is not
required, but definitely recommended - The order of the cases (including the default
case) in the switch does not matter. However, it
is a good programming style to follow the logical
sequence of the cases and place the default case
at the end. - Like I said earlier, because of the fact that you
can't use boolean expressions as cases, and you
can't use Objects (like Strings) or doubles as
the switch variable, they are definitely limited
as far as what they can do.
17One more quick review
if(booleanValue) //firstTrueBlock else
if(booleanValue) //secondTrueBlock else
if(booleanValue) //thirdTrueBlock else
/if all above booleans are false, this gets
executed/
- if(booleanValue)
-
- //trueBlock
-
-
- if(booleanValue)
- //trueBlock
- else
-
- //falseBlock
-
18One more quick review
switch(variableName) case value1
statement(s) break case value2
statement(s) break case value3
statement(s) break default statement(s)
Remember Only char, int, short, and byte
variables can be used inside switches. The value
beside each case statement must match the type of
the variable used in the switch.
19One more quick review
- (true true) returns true
- (true false) returns false
- (false false) returns false
- (false true) returns false
- (true true) returns true
- (true false) returns true
- (false true) returns true
- (false false) returns false
- (false false) returns false
- (false true) returns true
- (true false) returns true
- (true true) returns false
is true only if BOTH of the booleans around
the operator are true.
is true if EITHER OR BOTH of the booleans
around the operator are true.
is true only if the booleans around the
operator ARE NOT THE SAME (one is false and one
is true)