Lecture-05
Switch Statements
By
Dr. Bharati Mishra
Switch Statements
Switch Statement
• Tax Program
switch (status) {
case 0:
//compute taxes for single filers;
break;
case 1:
//compute taxes for married file jointly;
break;
case 2:
//compute taxes for married file separately;
break;
case 3:
//compute taxes for head of household;
break;
default: [Link]("Errors: invalid status");
}
Switch Statement
• Tax Program
Switch Statement
• A closer look switch (switch-expression) {
case value1: statement(s)1;
• value1, ... valueN are break;
constant case value2: statement(s)2;
• the same data type as break;
the value of the …
switch-expression.
case valueN: statement(s)N;
break;
default: statement(s)-for-default;
}
• Statements are executed when the value in the
case statement matches the value of the
switch-expression.
Switch Statement
switch (switch-expression) {
• A closer look
case value1: statement(s)1;
Using break is optional, but it break;
should be used at the end of case value2: statement(s)2;
each case in order to terminate break;
the remainder of the switch …
statement. If the break
case valueN: statement(s)N;
statement is not present, the
next case statement will be break;
executed. default: statement(s)-for-default;
}
• The default case, which is optional, can be used to
perform actions when none of the specified cases
matches the switch-expression.
Program Trace
• Example
Suppose ch is 'a':
‘a’
switch (ch) {
case 'a': [Link](ch);
case 'b': [Link](ch);
case 'c': [Link](ch);
}
Program Trace
• Example
ch is 'a':
switch (ch) {
case 'a': [Link](ch);
case 'b': [Link](ch);
case 'c': [Link](ch);
}
Program Trace
• Example
Execute this line.
switch (ch) {
case 'a': [Link](ch);
case 'b': [Link](ch);
case 'c': [Link](ch);
}
Program Trace
• Example
Execute this line.
switch (ch) {
case 'a': [Link](ch);
case 'b': [Link](ch);
case 'c': [Link](ch);
}
Program Trace
• Example
switch (ch) {
case 'a': [Link](ch);
case 'b': [Link](ch);
case 'c': [Link](ch);
}
Execute this line.
Program Trace II
• Example
Suppose ch is ‘a’.
switch (ch) {
case 'a': [Link](ch); break;
case 'b': [Link](ch); break;
case 'c': [Link](ch); break;
}
Program Trace II
• Example
ch is ‘a’.
switch (ch) {
case 'a': [Link](ch); break;
case 'b': [Link](ch); break;
case 'c': [Link](ch); break;
}
Program Trace II
• Example
Execute this line.
switch (ch) {
case 'a': [Link](ch); break;
case 'b': [Link](ch); break;
case 'c': [Link](ch); break;
}
Program Trace II
• Example
Execute this line.
switch (ch) {
case 'a': [Link](ch); break;
case 'b': [Link](ch); break;
case 'c': [Link](ch); break;
}
Program Trace II
• Example
switch (ch) {
case 'a': [Link](ch); break;
case 'b': [Link](ch); break;
case 'c': [Link](ch); break;
}
End of switch.
Conditional Statement
Conditional Statement
• Conditional statement as
(boolean-expression) ? expression1 : expression2
if (x > 0)
y = 1; y = (x > 0) ? 1 : -1;
else
y = -1;
Conditional Statement
if (num % 2 == 0)
[Link](num + “is even”);
else
[Link](num + “is odd”);
Same as:
[Link](
(num % 2 == 0)? num + “is even” : num + “is odd”);
Operator Precedence and Associativity
Operator Precedence and Associativity
Operator precedence and associativity determine the order in
which operators are evaluated.
Example:-
3 + 4 * 4 > 5 * (4 + 3) – 1 && (4 - 3 > 5)
• If operators with the same precedence are next to each other, their associativity determines
the order of evaluation.
Operator Precedence and Associativity
Operator Precedence
1. var++, var--
2. +, - (Unary plus and minus), ++var,--var
3. (type) Casting
4. ! (Not)
5. *, /, % (Multiplication, division, and remainder)
6. +, - (Binary addition and subtraction)
7. <, <=, >, >= (Comparison)
8. ==, !=; (Equality)
9. ^ (Exclusive OR)
10. && (Conditional AND) Short-circuit AND
11. || (Conditional OR) Short-circuit OR
12. =, +=, -=, *=, /=, %= (Assignment operator)
Precedence Rules
• All binary operators except “assignment”
operators are left-associative.
a–b+c–d
is equivalent to
((a – b) + c) – d
• Assignment operators are right-associative.
Therefore, the expression
a = b += c = 5
is equivalent to
a = (b += (c = 5))
Case-01
Now let us write a program to find out the Chinese Zodiac sign for a given year. The
Chinese Zodiac is based on a twelve-year cycle, with each year represented by an animal—
monkey, rooster, dog, pig, rat, ox, tiger, rabbit, dragon, snake, horse, or sheep—in this cycle,
as shown in Figure.
Note that year % 12 determines the Zodiac sign. 1900 is the year of the rat because 1900
% 12 is 4. Listing 3.9 gives a program that prompts the user to enter a year and displays the
animal for the year.