Chapter 3 - Review - Part 2
Chapter 3 - Review - Part 2
2
Examples
Examples
&& (and) (1 < x) && (x < 100)
|| (or) (age > 60) || (age <=18)
! (not) !(x==2) is equivalent to (x!=2)
!(x<2) is equivalent to (x>=2)
3
Truth Table for Operator &&
4
Truth Table for Operator ||
5
Truth Table for Operator ^
6
Truth Table for Operator !
7
Truth Table for Operator !
8
Examples
!(1>2) is equivalent to (1<=2)
9
10
11
Example Program
12
Output of Example Program
13
Note (De Morgan’s law)
!(condition1 && condition2)
is the same as
!condition1 || !condition2
!(condition1 || condition2)
is the same as
!condition1 && !condition2
14
Examples
Example 1:
!(number % 2 == 0 && number % 3 == 0)
is better written as:
(number % 2 != 0 || number % 3 != 0)
Example 2:
!(number == 2 || number == 3)
is better written as:
(number != 2 && number != 3)
15
3.6 Nested if and Multi-Way if-else
Statements
Nested If
An if statement can be inside another if statement to form a
nested if statement.
if (i > k)
if (j > k)
System.out.println("i and j are greater than k");
16
Nested If (Continued)
if (i > k)
if (j > k)
System.out.println("i and j are greater than k");
The output statement will be executed only when both conditions are true.
17
Multi-Way if-else
The Multi-Way if-else statement is also know as the nested if
else statement. The statement in the if-else is another if-else
statement.
if (score >= 90.0) if (score >= 90.0)
grade = 'A'; grade = 'A';
else else if (score >= 80.0)
if (score >= 80.0) Equivalent grade = 'B';
grade = 'B'; else if (score >= 70.0)
else grade = 'C';
if (score >= 70.0) else if (score >= 60.0)
grade = 'C'; grade = 'D';
else else
if (score >= 60.0) grade = 'F';
grade = 'D';
else
grade = 'F';
18
Multi-Way if-else (Continued)
19
animation
Trace nested if-else statement
Suppose score is 70.0 The condition is false
20
animation
Trace nested if-else statement
Suppose score is 70.0 The condition is false
21
animation
Trace nested if-else statement
Suppose score is 70.0 The condition is true
22
animation
Trace nested if-else statement
Suppose score is 70.0 grade is C
23
animation
Trace nested if-else statement
Suppose score is 70.0 Exit the if statement
24
3.7 Common Errors in Selection Statements
Common Error 1: Forgetting Necessary Braces
The braces can be omitted if the block contains a single statement.
However, forgetting the braces when they are needed for grouping
multiple statements is a common programming error.
For example, the following code in (a) is wrong. It should be
written with braces to group multiple statements, as shown in (b).
This is considered a logic error. Without braces the output
statement is considered to be outside the if the statement so it will
always be executed whether the condition is true or false.
25
3.7 (Continued)
Common Error 2: Wrong Semicolon at the if Line
Adding a semicolon at the end of an if clause is a common
mistake. This mistake is also a logic error.
What happens below is that when the condition is true nothing (the
empty statement) is selected and when the condition is false
nothing is ignored. So the statements that come after the if are
considered independent from the if. They will be always executed.
26
3.7 (Continued)
Common Error 3: Redundant Testing of Boolean Values
To test whether a boolean variable is true or false in a test
condition, it is redundant to use the equality comparison
operator like the code in (a) below. This is not really an error.
It is just a redundancy issue.
Output: B
29
Listing 3.8 – Page 106 in your book:
Determining Leap Year
This program first prompts the user to enter a year as an int value and
checks if it is a leap year. A year is a leap year if it is divisible by 4 but
not by 100, or it is divisible by 400.
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
30
Solution - Determining Leap
import java.util.Scanner;
Year
public class LeapYear {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean isLeapYear;
System.out.print("Enter a year: ");
int year = input.nextInt();
31
Listing 3.7 – Page 103 in your book:
Test Boolean Operators
Listing 3.7 gives a program that checks whether a number is
divisible by 2 and 3, by 2 or 3, and by 2 or 3 but not both.
import java.util.Scanner;
public class TestBooleanOperators {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
if (number % 2 == 0 && number % 3 == 0)
System.out.println(number + " is divisible by 2 and 3.");
if (number % 2 == 0 || number % 3 == 0)
System.out.println(number + " is divisible by 2 or 3.");
if (number % 2 == 0 ^ number % 3 == 0)
System.out.println(number + " is divisible by 2 or 3, but not both." );
}
}
32
Programming Exercise 3.8 page 123
Write a program that sorts three integers. The integers are entered from the
input dialogs and stored in variables num1, num2, and num3, respectively.
The program sorts the numbers so that num1<=num2<=num3.
import java.util.Scanner; if ((num1<=num2)&&(num2<=num3))
public class TestBooleanOperators { System.out.println(num1 + " <= " +num2+ " <= " + num3);
public static void main(String[] args) { else if ((num1<=num3)&&(num3<=num2))
// Create a Scanner System.out.println(num1 + " <= " +num3+ " <= " + num2);
Scanner input = new Scanner(System.in); else if ((num2<=num1)&&(num1<=num3))
// Receive an input System.out.println(num2 + " <= " +num1+ " <= " + num3);
System.out.print("Enter three numbers: "); else if ((num2<=num3)&&(num3<=num1))
int num1 = input.nextInt(); System.out.println(num2 + " <= " +num3+ " <= " + num1);
int num2 = input.nextInt(); else if ((num3<=num1)&&(num1<=num2))
int num3 = input.nextInt(); System.out.println(num3 + " <= " +num1+ " <= " + num2);
else if ((num3<=num2)&&(num2<=num1))
System.out.println(num3 + " <= " +num2+ " <= " + num1);
}
}
33