Chapter 3: Boolean Expressions

Ace your homework & exams now with Quizwiz!

order to evaluate boolean expressions

NAO (nuns are old): 1. not ! 2. and && 3. or ||

object creation

assigning a value to a variable

conditional expressions

boolean expressions (true or false)

how do you check to see if two variables are equal to each other?

boolean or int values: - use equality operator (==) double values: - use Math.abs(f1 - f2) < range value string values: - use methods of the string class (.equals or .equalsIgnoreCase) char values: - use 'x' < 'y' - possible to do because char is primitive data and has numerical values assigned to it *NOTE: DO NOT USE = BECAUSE THAT'S THE OPERATOR THAT'S USED TO ASSIGN VALUES TO VARIABLES **NOTE: == IS ALMOST NEVER USED FOR DOUBLE VALUES ***NOTE: DO NOT USE == FOR STRING VALUES ****NOTE: for char values, capital letters have a different numerical value compared to lowercase letters

de morgan's law

can be used to simplify boolean expressions (distributing a !) ex. !(a && b) -> (!a || !b) ex 2. !(a < b) -> (a >= b) ex 3. !(a == b) -> (a != b) STEPS: 1. change boolean expression sign (&& changes to || and || changes to &&) 2. change variables to opposite a. if >, change to <= b. if <, change to >= c. if variable, change to !variable (ex. a -> !a) *NOTE: DO NOT CHANGE VARIABLE TO IF IT'S A NUMERICAL VALUE. YOU WILL GET AN ERROR

object reference

declaring a variable

--

decrement operator; subtracts 1 from the value ex. a--; *NOTE: SAME THING AS a = a - 1; AND a -= 1;

syntax for if-else statements

if (condition) statement1; else statement2;

syntax for else-if statements

if (condition) statement1; else if (condition2) statement2; else statement3; statement4;

syntax for if statements

if (condition) statement; *NOTE: THE CONDITION IS ALWAYS IN PARENTHESIS AND IT MUST BE A BOOLEAN EXPRESSION **NOTE: THE SEMICOLON GOES AFTER THE STATEMENT LINE. NOT AFTER THE CONDITION.

syntax for block statements

if (condition) { statement1; statement2; }

nested/cascading if statements

if statements inside of other if statements

++

increment operator; adds 1 to the value ex. a++; *NOTE: SAME THING AS a = a + 1; AND a += 1;

&&

means "and" in java; binary operator because it needs TWO operands; both operands MUST be TRUE ex. a && b - a and b must both be true for a && b to evaluate as true (if either a or b is false, it will evaluate to false) *NOTE: a AND b NEED TO BE BOOLEAN VARIABLES BECAUSE IT DOESN'T MAKE SENSE FOR THEM TO BE ANYTHING ELSE

>=

means "greater than or equal to" in java ex. a >= b means a is greater than or equal to b

>

means "greater than" in java ex. a > b means a is greater than b

<=

means "less than or equal to" in java ex. a <= b means a is less than or equal to b

<

means "less than" in java ex. a < b means a is less than b

!

means "not" in java; unary operator (only needs 1 operand) ex. != means not equal to !a means not a !(a < b) means a is not less than b *NOTE: ALSO CALLED LOGICAL NEGATION OR LOGICAL COMPLEMENT

||

means "or" in java; binary operator because it needs TWO operands; both operands could be true, but only ONE needs to be true ex. a || b - either a or b needs to be true. if one is true, a || b will evaluate as true. if both are true, a || b will evaluate as true. if both are false, a || b will evaluate as false. *NOTE: a AND b NEED TO BE BOOLEAN VARIABLES BECAUSE IT DOESN'T MAKE SENSE FOR THEM TO BE ANYTHING ELSE

truth tables

shows possible true/false combinations for terms number of variables determine the number of possibilities: - 2^x where x = # of variables *NOTE: USE && AND || OPERATORS TO FIGURE OUT HOW MANY VARIABLES THERE ARE (idk if this makes sense but it makes sense to me sooo)

which if statement is the else applied to? if (condition) { if (condition) statement1; } else statement2;

the else statement is applied to the 1st if statement

which if statement is the else applied to? if (condition) if (condition) statement1; else statement2;

the else statement is applied to the 2nd if statement *NOTE: the else statement is ALWAYS applied to the nearest if statement UNLESS it is manipulated otherwise with brackets {}.

which if statement is the else applied to? if (condition) if (condition) statement1; else statement2;

the else statement is applied to the 2nd if statement *NOTE: the else statement is ALWAYS applied to the nearest if statement UNLESS it is manipulated otherwise with brackets {}. **NOTE: indentation is nothing more than white space, which only helps humans read the code. the spacing may look like it gets applied to the 1st if statement, but white space doesn't change how the computer reads the code.

are two objects that have the same value assigned equal to each other? ex. House myHouse = new House("Green", 1850, 3); House annasHouse = new House("Green", 1850, 3); House bobsHouse; House momsHouse = myHouse is myHouse and annasHouse the same OBJECT? is (myHouse == annasHouse) true or false? is myHouse.equals(annasHouse) true or false?

they are not the same object. they just hold the same value. (myHouse == annasHouse) is false. myHouse.equals(annasHouse) is true.

short circuiting

when the left operand is sufficient, so the right operand isn't evaluated && statements: - if left operand is false, the computer won't even evaluate/check the right operand because it'll be false no matter what. - useful to prevent runtime errors ex. if (count != 0 && total/count > MAX) - prevents division of 0 || statements: - if left operand is true, the computer won't even evaluate/check the right operand because it'll be true no matter what.

assignment operators syntax

x += y; - same as x = x + y; x -= y; - same as x = x - y; x *= y; - same as x = x * y; x /= y - same as x = x/y; x %= y - same as x = x%y;

are two objects that are assigned equal to each other the same object? ex. House myHouse = new House("Green", 1850, 3); House annasHouse = new House("Green", 1850, 3); House bobsHouse = new House("Blue", 2400, 3) House momsHouse = myHouse is myHouse and momsHouse the same OBJECT? is (myHouse == momsHouse) true or false? is myHouse.equals(momsHouse) true or false? is myHouse.equals(bobsHouse) true or false?

yes, they are the same object (myHouse == momsHouse) is true. myHouse.equals(momsHouse) is true. myHouse.equals(bobsHouse) is false.

how will these lines of code run? if (condition) statement1; statement2;

2 possibilities 1. if condition is true, it will run statement1. then, it will run statement2. 2. if condition is false, it will skip statement1. then, it will run statement2. *TAKEAWAY: the if statement will only affect the NEXT line of code (unless it is a block statement)

how will these lines of code run? if (condition) { statement1; statement2; } else statement3; statement4;

2 possibilities 1. if condition is true, statement1 and statement2 will run. it'll skip statement3 and it'll run statement4. 2. if condition is false, it'll skip statement1 and statement2. statement3 will run and it'll run statement4.

how will these lines of code run? if (condition) statement1; else statement2; statement3;

2 possibilities 1. if condition is true, statement1 will run. it'll skip statement2 and then it'll run statement3. 2. if condition is false, it will skip statement1. statement2 will run and then it'll run statement 3.

how will these lines of code run? if (condition) statement1; else if (condition2) statement2; else statement3; statement4;

3 possibilities 1. if condition is true, statement1 will run. it'll skip condition2 (therefore skipping statement2) and it'll skip statement3. it'll run statement4. 2. if condition is false, it'll skip statement1. if condition2 is true, it'll run statement2. it'll skip statement3. it'll run statement4. 3. if condition is false, it'll skip statement1. if condition2 is also false, it'll skip statement2. it'll run statement3 and it'll run statement4.


Related study sets

Life Insurance Exam Question (Texas)

View Set

D2L Quiz: Dental Materials Chapter 12

View Set

Life insurance policy provisions, options, and riders

View Set

Health Education: Psychological Health (CONTINUED)

View Set

Unit 1: Basic Economic Concepts and Supply & Demand

View Set

Client Pro Midterm Question Chap 1 - 7

View Set

Chapter 11 Lesson 3: Striving for Equality

View Set