0% found this document useful (0 votes)
20 views33 pages

Chapter 3 - Review - Part 2

This document discusses logical operators and selection statements in Java. It begins by defining logical operators such as &&, ||, !, and ^ and providing truth tables for each. It then covers nested if statements, multi-way if-else statements, and common errors in selection statements like missing braces and incorrect semicolon usage. Examples are provided throughout to demonstrate logical operators and nested/multi-way if-else statements.

Uploaded by

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

Chapter 3 - Review - Part 2

This document discusses logical operators and selection statements in Java. It begins by defining logical operators such as &&, ||, !, and ^ and providing truth tables for each. It then covers nested if statements, multi-way if-else statements, and common errors in selection statements like missing braces and incorrect semicolon usage. Examples are provided throughout to demonstrate logical operators and nested/multi-way if-else statements.

Uploaded by

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

Chapter 3 Selections

3.11 Logical Operators


3.6 Nested if and Multi-Way if-else Statements
3.7 Common Errors in Selection Statements
Liang, Introduction to Java
Programming, Eighth Edition, (c)
1
2011 Pearson Education, Inc. All
3.11 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.
Operator Name Description
! not negation
&& and logical and
|| or logical or
^ xor exclusive or

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.

The statement in an if can be any Java statement, including


another if statement. The inner if statement is said to be nested
inside the outer if statement. The inner if statement can contain
another if statement; in fact, there is no limit to the depth of the
nesting. For example, the following is 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");

Assume i = 3, j = 2, and k = 1. The condition in the first if is checked. Since it


is true, the condition in the second if is checked. Again it is true therefore the
output statement will be executed.

Assume i = 3, j = 1, and k = 1. The condition in the first if is checked. Since it


is true, the condition in the second if is checked. It is false therefore the
output statement will not be executed.

Assume i = 1 and k = 1. The condition in the first if is checked. Since it is


false, then the second if will not be executed. Hence in this case no matter
what the value of j is the output statement will not be executed.

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

if (score >= 90.0)


grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';

20
animation
Trace nested if-else statement
Suppose score is 70.0 The condition is false

if (score >= 90.0)


grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';

21
animation
Trace nested if-else statement
Suppose score is 70.0 The condition is true

if (score >= 90.0)


grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';

22
animation
Trace nested if-else statement
Suppose score is 70.0 grade is C

if (score >= 90.0)


grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';

23
animation
Trace nested if-else statement
Suppose score is 70.0 Exit the if statement

if (score >= 90.0)


grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';

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.

Using the = operator instead of the == operator to compare the


equality of two items is a common error. It could lead to the
following erroneous statement:
if (even = true)
System.out.println("It is even.");
This statement does not cause a syntax error. It assigns true to
even. 27
3.7 (Continued)
Common Error 4: Dangling else Ambiguity
The else clause matches the most recent if clause in the
same block.

No output since the condition of the first if is false.


28
3.7 (Continued)
To force the else clause to match the first if clause, you must add
a pair of braces:

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();

// Check if the year is a leap year


isLeapYear =(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

// Display the result


System.out.println(year + " is a leap year? " + isLeapYear);
}
}

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

You might also like