0% found this document useful (0 votes)
133 views11 pages

Switch Statements

The switch statement provides another way to decide which statement to execute based on an expression. The switch statement evaluates an expression and attempts to match the result to several possible cases. The match must be an exact match. Each case contains a value and list of statements. If the expression matches a case value, control jumps to that case's statement list. The break statement can be used to transfer control out of the switch statement. If no cases match, or no break is used, control may fall through to subsequent cases. A default case can also be used to handle non-matches.

Uploaded by

CHRISTIAN GERALD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views11 pages

Switch Statements

The switch statement provides another way to decide which statement to execute based on an expression. The switch statement evaluates an expression and attempts to match the result to several possible cases. The match must be an exact match. Each case contains a value and list of statements. If the expression matches a case value, control jumps to that case's statement list. The break statement can be used to transfer control out of the switch statement. If no cases match, or no break is used, control may fall through to subsequent cases. A default case can also be used to handle non-matches.

Uploaded by

CHRISTIAN GERALD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Switch Statements

Comparing Exact Values


The Switch Statement
• The switch statement
provides another way to
decide which statement to switch ( expression ){
execute. case value1 :
statement-list1
• The switch statement case value2 :
evaluates an expression statement-list2
enclosed, then attempts to case value3 :
match the result to one of statement-list3
several possible cases case ...
• The match must be an }
exact match.

2
The Switch Statement
• Each case
contains a value switch ( expression ){
and a list of case value1 :
statements statement-list1
case value2 :
• The flow of control statement-list2
case value3 :
transfers to statement-list3
statement case ...
associated with
the first case value }
that matches
3
Switch - syntax
• The general syntax of a switch statement is:
switch
switch ( expression ){
and
case value1 :
case
statement-list1
are
case value2 :
reserved
statement-list2
words
case value3 :
statement-list3
case ...
If expression
} matches value3,
control jumps
to here
The Switch Statement
• The break statement can
be used as the last switch ( expression ){
statement in each case's case value1 :
statement list statement-list1
break;
• A break statement causes case value2 :
control to transfer to the statement-list2
break;
end of the switch statement
case value3 :
• If a break statement is not statement-list3
break;
used, the flow of control
case ...
will continue into the next
case }
Switch Example
• Examples of the switch statement:
switch (option){
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
}
Switch – no breaks!!!
• Another Example:
switch (option){
case 'A': switch (option){
aCount++; case 'A':
break; aCount++;
case 'B': case 'B':
bCount++; bCount++;
break; case 'C':
case 'C': cCount++;
cCount++; }
break;
}
Switch - default
• A switch statement can have an optional default
case

• The default case has no associated value and


simply uses the reserved word default

• If the default case is present, control will transfer to


it if no other case value matches

• If there is no default case, and no other value


matches, control falls through to the statement after
the switch
The switch Statement

• Switch switch (option){


with case 'A':
default aCount++;
break;
case: case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
default:
otherCount++;
break;
}
To Switch or not to Switch
• The expression of a switch statement must result in an
integral type, meaning an integer (byte, short, int,
long) or a char

• It cannot be a boolean value or a floating point value


(float or double)

• The implicit boolean condition in a switch statement is


equality

• You cannot perform relational checks with a switch


statement
Questions??

To switch or not to
switch, that’s the
question….

You might also like