0% found this document useful (0 votes)
6 views30 pages

3 - Boolean Statements

This lesson covers Boolean statements and relational operators in Java, including examples of how to use them to compare values. It explains logical operators such as AND (&&), OR (||), and NOT (!), along with the order of operations for evaluating expressions. Additionally, it introduces DeMorgan's Law for simplifying Boolean expressions and includes practice questions to reinforce understanding.

Uploaded by

ctaylor
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)
6 views30 pages

3 - Boolean Statements

This lesson covers Boolean statements and relational operators in Java, including examples of how to use them to compare values. It explains logical operators such as AND (&&), OR (||), and NOT (!), along with the order of operations for evaluating expressions. Additionally, it introduces DeMorgan's Law for simplifying Boolean expressions and includes practice questions to reinforce understanding.

Uploaded by

ctaylor
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/ 30

Lesson 3 - Boolean

Statements
AP Computer Science A - Summer 2024
Mr. Taylor
Relational Operators
Relational operators relate things to one another
● Many times in code, it is useful to compare two numbers to see if
they’re equal, or if one is greater than the other, etc.
○ Relational operators allow us to do just that
List of relational operators
● > - Greater than
● < - Less than
● >= - Greater than or equal to
● <= - Less than or equal to
● == - Equal to
○ IMPORTANT: Single equals is assignment, double equals is comparing equality

● != - Not equal to
Relational Operators Examples
● int a = 8;
○ a == 10; // false
○ a < 10; // true
○ a >= 10; // false
○ a != 10; // true
● double b = 5.0;
○ b == 10.0; // false
○ b < a; // false
○ b >= a; // true
○ b != 5.000000001; // true
Checking doubles
● Sometimes we want to check if two doubles are close to each other,
but not necessarily exactly equal
○ The problem is that 10.0000001 == 10.0 is false

double value = 10.0000001;


double goal = 10.0;
double difference = Math.abs(value - goal);
System.out.println(difference <= 0.001); // true
DIY 7
What Number?
Logical Operators
Logical Operations
● What if we need two things to both be true before we run code?
○ We can use the && operator

○ a && b; // In order for this to be true, a and b must both be true

● What if only one of two things needs to be true to run code?


○ We can use the || operator (called the or operator)

○ a || b; // This is true if a or b (or both) is true

● We can also use ! as a way of saying “not”


○ a && !b; // This is only true if a is true and b is false
Order of Operations
● Java will do all math first (According to PEMDAS)
○ +, -, *, /, %

● Then all relational operators second


○ >, <, >=, <=, ==, !=

● Then && third


● Then || fourth
Lightning Review 5
True || False
Question 1
Consider the following expression.
int a = 3, b = 4, c = 5;
boolean result = a <= 3 && b != 4;

Does result evaluate to true or false?


● (A) True
● (B) False
Question 1
Consider the following expression.
int a = 3, b = 4, c = 5;
boolean result = a <= 3 && b != 4;

Does result evaluate to true or false?


● (A) True
● (B) False
Question 2
Consider the following expression.
int a = 3, b = 4, c = 5;
boolean result = b % 2 == 1 || c / 2 == 1;

Does result evaluate to true or false?


● (A) True
● (B) False
Question 2
Consider the following expression.
int a = 3, b = 4, c = 5;
boolean result = b % 2 == 1 || c / 2 == 1;

Does result evaluate to true or false?


● (A) True
● (B) False
Question 3
Consider the following expression.
int a = 3, b = 4, c = 5;
boolean result = a > 2 && (b > 5 || c < 6);

Does result evaluate to true or false?


● (A) True
● (B) False
Question 3
Consider the following expression.
int a = 3, b = 4, c = 5;
boolean result = a > 2 && (b > 5 || c < 6);

Does result evaluate to true or false?


● (A) True
● (B) False
Question 4
Assume that x, y, and z have been declared as follows:
boolean x = true;
boolean y = false;
boolean z = true;

Which of the following expressions evaluates to true?


● (A) x && y && z
● (B) x && y || z && y
● (C) !(x && y) || z
● (D) !(x || y) && z
● (E) x && y || !z
Question 4
Assume that x, y, and z have been declared as follows:
boolean x = true;
boolean y = false;
boolean z = true;

Which of the following expressions evaluates to true?


● (A) x && y && z
● (B) x && y || z && y
● (C) !(x && y) || z
● (D) !(x || y) && z
● (E) x && y || !z
Short-Circuiting
● When Java evaluates boolean operators, sometimes it short-circuits
(in a good way)
○ If Java knows that a statement will be true or false halfway through evaluating it,
it’ll just stop

● In order for a && b to be true, we know both a and b have to be


true
○ If Java sees that a is false, it won’t even check b

● In order for a || b to be true, we know either a or b has to be true


■ If Java sees that a is true, it won’t even check b
An extreme example
● 1 < 3 || (10 >= 4 - 5 % 2 + (3 + 9 / 7) - (24 % (9 % 5))
○ True or false?
DIY 8
Short-Circuit Superhero
DeMorgan’s Law
DeMorgan’s Law
● DeMorgan’s Law is a special rule that we can use to simplify and
rewrite boolean expressions
○ It’s like the Distributive Property but for boolean logic

● !(a && b) is the same as !a || !b


○ In order for a && b to be false, only one of the two needs to be false

● !(a || b) is the same as !a && !b


○ In order for a || b to be false, both a and b need to be false
DeMorgan’s Law (cont’d)
● DeMorgan’s Law can flip inequality symbols
○ !(a < b) is the same as a >= b

○ !(a > b) is the same as a <= b

○ !(a <= b) is the same as a > b

○ !(a >= b) is the same as a < b

● It can also flip equals with not equals


○ !(a == b) is the same as a != b

○ !(a != b) is the same as a == b


Lightning Review 6
DeMorgan’s Law
Question 1
Which conditional statement is equivalent to the expression shown
below?

if((temp > 80) && !(80 < temp))


(A) if(temp == 80)
(B) if(temp != 80)
(C) if((temp >= 80) || (temp > 80))
(D)The statement is always false
(E)The statement is always true
Question 1
Which conditional statement is equivalent to the expression shown
below?

if((temp > 80) && !(80 < temp))


(A) if(temp == 80)
(B) if(temp != 80)
(C) if((temp >= 80) || (temp > 80))
(D)The statement is always false
(E)The statement is always true
Question 2
Assume that a, b, and c have been declared and initialized with in
values. The below expression

!(a > b || b <= c)


is equivalent to which of the following?
(A) a > b && b <= c
(B) a <= b || b > c
(C) a <= b && b > c
(D) a < b || b >= c
(E) a < b && b >= c
Question 2
Assume that a, b, and c have been declared and initialized with in
values. The below expression

!(a > b || b <= c)


is equivalent to which of the following?
(A) a > b && b <= c
(B) a <= b || b > c
(C) a <= b && b > c
(D) a < b || b >= c
(E) a < b && b >= c

You might also like