Title: Switch Statements
1Switch Statements
- Selecting Among More than Two Choices
2Why Multiple Transfers
- Each "if" statement transfers control to either
statement-1 or statement-2, depending on the
boolean expression, since the boolean expression
can only be true or false. - if (boolean-expression)
- statement-1
- else
- statement-2
3When you have more than 2 options, you can ...
- use additional "if" statements, each of which has
two branches - these can be placed in sequence, or
- these can be "nested," i.e. an if within an if
- use a "switch" statement
4To Use "if" Statements
- if (boolean-expression-1)
- statement-1
- else if (boolean-expression-2)
- statement-2
- else if (boolean-expression-3)
- statement-3
- else
- statement-4
5Rewriting in Conventional Style (which uses
indents to show relationships)
- if (boolean-expression-1)
- statement-1
- else
- if (boolean-expression-2)
- statement-2
- else
- if (boolean-expression-3)
- statement-3
- else
- statement-4
6Example Use "if else if" Statements
- if (studentGradeLevel 1)
- froshStudentList.add(studentId)
- else if (studentGradeLevel 2)
- sophStudentList.add(studentId)
- else if (studentGradeLevel 3)
- juniorStudentList.add(studentId)
- else
- seniorStudentList.add(studentId)
7Example Use "if" with Conventional Formatting
- if (studentGradeLevel 1)
- froshStudentList.add(studentId)
- else
- if (studentGradeLevel 2)
- sophStudentList.add(studentId)
- else
- if (studentGradeLevel 3)
- juniorStudentList.add(studentId)
- else
- seniorStudentList.add(studentId)
8Implications of Use of Deeply Nested ifs ...
- Deeply indented if's are tough to write and tough
to read (and get them correct) - If you have a lot of choices, then you will end
up with many indents, maybe too many - When you use the "if else if" notation it can
be fairly simple, but it requires different
formatting than for an "if" statement, and it is
still interpreted as if it is indented in the
traditional way
9Structure of the "Switch" Stmt
- switch (controlVar)
-
- case 'a'
- statement-1
- break
- case 'b'
- statement-2
- break
- default
- statement-3
- break
-
10Points to Note
- Control Variable can be an integer expression,
generally an int or char, but can not be a String - The name of the control variable is placed in
parentheses - Following the word switch and control variable,
you have all the cases set up in a single block
(i.e., set of curly braces) - All the reserved words (switch, case, default)
start with a lower case letter
11Points to Note (more)
- Do not use a semi-colon after (control var)
- Each case ends with a colon character (note not
a semi-colon) - Each statement can be a simple statement or a
compound statement (i.e., block) - Normally each case (including the last one) ends
with a break statement - Each case label has to be unique
- Good idea to always use default
- There can be only one "default" label
12How the "switch" Stmt Works
- The "switch" statement identifies which case
matches the control variable, and starts
executing there - Statements get executed in sequential order until
the end of the switch (i.e., until the closing
curly brace) - If a "break" statement is executed, then control
passes to the statement after the closing curly
brace of the switch statement
13What Happens?
- charges 0.
- switch (actType)
-
- case 'B'
- charges charges 2.00
- case 'D'
- charges charges 5.00
- default
- charges charges 10.00
-
- Question if actType is B, what value will
charges have?
14Documenting "FALLTHROUGH"
- char actType
-
- charges 0.
- switch (actType)
-
- case 'B'
- charges charges 2.00
- //FALLTHROUGH
- case 'D'
- charges charges 5.00
- //FALLTHROUGH
- default
- charges charges 10.00
- //FALLTHROUGH
-
15What Happens
- char actType
-
- charges 0.
- switch (actType)
-
- case 'B'
- case 'R'
- charges charges 2.00
- break
- case 'D'
- case 'G'
- case 'T'
- charges charges 5.00
- break
- default
- charges charges 10.00
- break
-
16Written with Better Style
- char actType
-
- charges 0.
- switch (actType)
-
- case 'B' case 'R'
- charges charges 2.00
- break
- case 'D' case 'G' case 'T'
- charges charges 5.00
- break
- default
- charges charges 10.00
- break
-
17What Happens if There is No Match
- actType 'Z'
- charges 0.
- switch (actType)
-
- case 'B'
- charges charges 2.00
- break
- case 'D'
- charges charges 5.00
- break
-
18What Happens Here
- actType "B"
- charges 0.
- switch (actType)
-
- case "B"
- charges charges 2.00
- break
- case "D"
- charges charges 5.00
- break
-
19Summary of the Switch Statement
- Is easier to use for multi-way selection than
nested if's - Switch can only rely on one variable or
expression (unlike if's which can involve several
unrelated conditions) - Can only switch on int and char variables (unlike
if's) - Looks for first matching value in a case
statement, and then starts working sequentially
from there - Be careful of drop throughs
- You can specify a default action to be performed
if none of the other case values match