Professional Documents
Culture Documents
Chapter 3 Selections Part 2
Chapter 3 Selections Part 2
Programming I
(TEC1204)
Learning Objectives
• To combine conditions using logical operators (!, &&, ||, and ^).
• To program using selection statements with combined conditions.
• To implement selection control using switch statements .
• To write expressions using the conditional expression.
• To examine the rules governing operator precedence and
associativity.
• To apply common techniques to debug errors.
Israa University 2
Logical Operators
• The logical operators !, &&, ||, and ^ can be used to create a compound
Boolean expression.
• Logical operators, also known as Boolean operators, operate on Boolean
values to create a new Boolean value.
Israa University 3
Truth Table for Operator !
true false !(age > 18) is false, because (age > 18) is true.
Israa University 4
Truth Table for Operator &&
Israa University 5
Truth Table for Operator ||
p2
false true true (age > 34) || (weight <= 140) is true, because (age > 34) is
false, but (weight <= 140) is true.
true
true false (age > 14) || (weight >= 150) is false, because (age
false false false (age > 34) ^ (weight > 140) is true, because (age > 34) is
false true true (age > 34) ^ (weight >= 140) is true, because (age > 34) is
false
true true
Israa University 7
Exercise
Israa University 8
Solution
System.out.println("Is " + number + " divisible by 2 and 3? " +
((number % 2 == 0) && (number % 3 == 0)));
System.out.println("Is " + number + " divisible by 2 or 3? " +
((number % 2 == 0) || (number % 3 == 0)));
System.out.println("Is " + number +
" divisible by 2 or 3, but not both? " +
((number % 2 == 0) ^ (number % 3 == 0)));
Israa University 9
Case Study: Determining Leap Year
LeapYear
Israa University 10
Case Study: Determining Leap Year
Israa University 11
Case Study: Determining Leap Year
Israa University 12
switch Statements
• A switch statement executes statements based on the value of a
variable or an expression.
Israa University 13
switch Statements
Israa University 14
switch Statements
• Do not forget to use a break statement when one is needed.
Israa University 15
Trace switch statement
Suppose day is 2:
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");
}
16
Trace switch statement
Match case 2
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");
}
17
Trace switch 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");
}
18
Trace switch 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");
}
19
Trace switch statement
Fall through case 5
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");
}
20
Trace switch statement
Encounter break
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");
}
21
Trace switch statement
Exit the 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");
}
22
switch: Chinese Zodiac Example
• 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.
• Note that year % 12 determines the Zodiac sign. 1900 is the year of
the rat because 1900 % 12 is 4.
23
switch: Chinese Zodiac Example
24
switch: Chinese Zodiac Example
25
switch: Chinese Zodiac Example
26
Exercise
27
Conditional Expressions
28
Conditional Expressions: Exercise
29
Conditional Expressions: Exercise
30
Operator Precedence and Associativity
31
Operator Precedence and Associativity
32
Debugging
– you can insert print statements in order to show the values of the variables or
the execution flow of the program.
33