CPCS202 03 Selections S19
CPCS202 03 Selections S19
Selections
Objectives
▪ To declare boolean variables and write Boolean expressions using relational
operators (§3.2).
▪ To implement selection control using one-way if statements (§3.3).
▪ To implement selection control using two-way if-else statements (§3.4).
▪ To implement selection control using nested if and multi-way if statements
(§3.5).
▪ To avoid common errors and pitfalls in if statements (§3.6).
▪ To generate random numbers using the Math.random() method (§3.7).
▪ To program using selection statements for a variety of examples
(SubtractionQuiz, BMI, ComputeTax) (§§3.7–3.9).
▪ To combine conditions using logical operators (&&, ||, and !) (§3.10).
▪ To program using selection statements with combined conditions (LeapYear,
Lottery) (§§3.11–3.12).
▪ To implement selection control using switch statements (§3.13).
▪ To write expressions using the conditional expression (§3.14).
▪ To examine the rules governing operator precedence and associativity (§3.15).
▪ To apply common techniques to debug errors (§3.16).
Motivations
Consider this problem from Chapter 2:
Example:
if (radius >= 0) {
area = radius * radius * PI;
System.out.println(“The area ” +
“for the circle ” +
“of radius ” + radius +
“ is ” + area);
}
(a) (b)
– but if several statements are inside the if, then the block braces are required
Solution:
if (score > 90)
pay *= 1.03;
else
pay *= 1.01;
– (a) is wrong
the braces are required
– (b) is correct
the required braces are now included
if (i > j) {
if (i > k)
System.out.println("A");
}
else
System.out.println("B");
Solution:
newline = count % 10 == 0;
|| or logical disjunction
p !p
true false
false true
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
case 7: System.out.println(“SEVEN”);
case 2: System.out.println(“THREE”);
}
a is 1:
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
case 7: System.out.println(“SEVEN”);
case 2: System.out.println(“THREE”);
}
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
case 7: System.out.println(“SEVEN”);
case 2: System.out.println(“THREE”);
}
Output :
ONE
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
case 7: System.out.println(“SEVEN”);
case 2: System.out.println(“THREE”);
}
Output :
ONE
SEVEN
© Dr. Jonathan Cazalas Chapter 3: Selections page 99
animation
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
case 7: System.out.println(“SEVEN”);
case 2: System.out.println(“THREE”);
}
Output :
ONE
SEVEN
THREE
© Dr. Jonathan Cazalas Chapter 3: Selections page 100
animation
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
case 7: System.out.println(“SEVEN”);
case 2: System.out.println(“THREE”);
}
Next statement;
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
break;
case 7: System.out.println(“SEVEN”);
break;
case 2: System.out.println(“THREE”);
break;
}
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
break;
case 7: System.out.println(“SEVEN”);
break;
case 2: System.out.println(“THREE”);
break;
}
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
break;
case 7: System.out.println(“SEVEN”);
break;
case 2: System.out.println(“THREE”);
break;
}
Output :
ONE
© Dr. Jonathan Cazalas Chapter 3: Selections page 104
animation
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
break;
case 7: System.out.println(“SEVEN”);
break;
case 2: System.out.println(“THREE”);
break;
}
Output :
ONE
© Dr. Jonathan Cazalas Chapter 3: Selections page 105
animation
int a= 1;
switch (a) {
case 1: System.out.println(“ONE”);
break;
case 7: System.out.println(“SEVEN”);
break;
case 2: System.out.println(“THREE”);
break;
}
Next statement;
© Dr. Jonathan Cazalas Chapter 3: Selections page 106
animation
switch (a) {
case 1: System.out.println(“ONE”);
break;
case 7: System.out.println(“SEVEN”);
break;
case 2: System.out.println(“THREE”);
break;
default :
System.out.println(“ERROR”);
}
Pay attention:
Output : Here, we used the default option,
ERROR which is used for any case not listed.
switch (ch) {
case 'a': System.out.println(ch);
case 'B': System.out.println(ch);
case 'c': System.out.println(ch);
}
ch is 'a':
char ch = ‘a’;
switch (ch) {
case 'a': System.out.println(ch);
case 'B': System.out.println(ch);
case 'c': System.out.println(ch);
}
switch (ch) {
case 'a': System.out.println(ch);
case 'B': System.out.println(ch);
case 'c': System.out.println(ch);
}
Output :
a
switch (ch) {
case 'a': System.out.println(ch);
case 'B': System.out.println(ch);
case 'c': System.out.println(ch);
}
Output :
a
a
switch (ch) {
case 'a': System.out.println(ch);
case 'B': System.out.println(ch);
case 'c': System.out.println(ch);
}
Output :
a
a
a
switch (ch) {
case 'a': System.out.println(ch);
case 'B': System.out.println(ch);
case 'c': System.out.println(ch);
}
Next statement;
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'B': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'B': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'B': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Output :
a
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'B': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Output :
a
switch (ch) {
case 'a': System.out.println(ch);
break;
case 'B': System.out.println(ch);
break;
case 'c': System.out.println(ch);
}
Next statement;
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
– Solution:
– Example:
Assignment operators are right associative
Therefore,