Switch Statements Flashcards
(7 cards)
1) Switch Statements
Switch statements can be used instead of n_______i____ e____ statements when you have too many conditions to deal with.
nested-if-else
2)
A switch statement may be used when
• only o______ variable is being checked in each condition
• the check involves specific values of that variable (e.g. ‘A’, ‘B’) and not r________
(for example > 40).
one
ranges
3) Study the following code;
What does the KEYWORD ‘case’ refer to?
char group;
Scanner keyboard = new Scanner(System.in);
System.out.println(“Lab Times”);
System.out.println(“Enter your group (A,B,C)”);
group = keyboard.next().charAt(0);
switch(group)
{
case ‘A’: System.out.println(“10.00 a.m.”);
break;
case 'B': System.out.println("1.00 p.m."); break; case 'C': System.out.println("11.00 a.m."); break; default: System.out.println("No such group"); }
The variable that is being checked.
In this example is the ‘group’ variable
4) Study the following code.
What is the command ‘break’ doing?
char group;
Scanner keyboard = new Scanner(System.in);
System.out.println(“Lab Times”);
System.out.println(“Enter your group (A,B,C)”);
group = keyboard.next().charAt(0);
switch(group)
{
case ‘A’: System.out.println(“10.00 a.m.”);
break;
case 'B': System.out.println("1.00 p.m."); break; case 'C': System.out.println("11.00 a.m."); break; default: System.out.println("No such group"); }
It is forcing the program to skip the rest of the switch
statement.
The break statement is important because it means that once a matching case is
found, the program can skip the rest of the cases below.
If it is not added, not only will the instructions associated with the matching case be executed, but also all the
instructions associated with all the cases below it.
Notice that the last set of instructions does not need a break statement as there are no other cases to skip.
5) Study the following code.
What does the ‘default’ case do?
char group;
Scanner keyboard = new Scanner(System.in);
System.out.println(“Lab Times”);
System.out.println(“Enter your group (A,B,C)”);
group = keyboard.next().charAt(0);
switch(group)
{
case ‘A’: System.out.println(“10.00 a.m.”);
break;
case 'B': System.out.println("1.00 p.m."); break; case 'C': System.out.println("11.00 a.m."); break; default: System.out.println("No such group"); }
The default case checks for input errors.
It is optional and can be thought of as ‘otherwise’ .
You can use it to check errors, or offer an alternative for when none of the conditions above are true.
6) Can you combine cases if they have the same instruction/s?
e.g. Group A and Group C both have a labs at 10:00
Yes, you can.
e.g.
// groups A and C have been processed together
switch(group)
{
case ‘A’: case ‘C’: System.out.println(“10.00 a.m.”);
break;
case ‘B’: System.out.println(“1.00 p.m.”);
break;
default: System.out.println(“No such group”);
}
7) Removing break statements
Break statements allow us to s____ unwanted code in switch statements.
But we don’t have to skip code if we don’t want to.
e.g.
int security;
Scanner keyboard = new Scanner(System.in);
System.out.println(“Secret Agents”);
System.out.println(“Enter security level (1,2,3)”);
security = keyboard.nextInt();
switch(security) // check level of security
{
// level 3 security
case 3: System.out.println(“The code to access the safe is 007.”);
// level 2 security
case 2: System.out.println(“Jim Kitt is really a double agent.”);
// level 1 security
case 1: System.out.println(“Martinis in the hotel bar may be poisoned.”);
break; // necessary to avoid error message below
default: System.out.println(“No such security level.”);
}
skip
In this example, someone with level 3 security would see all three clues.
Someone with level 2 would only see 2 clues.
Someone with level 1 would only see the last clue.