Unit 3: Boolean Expressions & if Statements Flashcards
Under what conditions does an if statement execute?
An if statement executes when the boolean expression within the parentheses is true.
How do you compare Strings?
You compare strings through the “.equals()” method, which returns a boolean expression.
What is the difference between = and == ?
= is used to assign values
== is used when checking equality
Which of the following symbols is the “not equal” symbol?
a) =!
b) ==
c) !=
d) !!
c) !=
Under what conditions does an else statement execute?
Else statements execute if the initial boolean expression evaluates to false
What is the purpose of an else if statement?
Else if statements allow us to incorporate additional conditions to if-else statements
Will it return true or false?
!(true && false) || (false && false)
true
What are the 3 logical operators?
NOT !
AND &&
OR ||
What is the not operator?
!(not) evaluates a condition to the opposite boolean value
Are both of these equivalent?
p && !q || !p && q
p || q) && (!p || !q
yes
What is the and operator?
The &&(and) evaluates whether all conditions are true
What is the or operator?
|| (or) evaluates whether one condition is true
Do boolean operators also have an order of operations?
Yes!
What would the output be for the following code
double x = 15.0/11.0;
System.out.println(11.0*x == 15.0);
False
Is this statement true?
!(a && b) == !a or !b
True statement, De Morgan’s law
When && and || are combined in one logical expression, which one has a higher rank?
&& has a higher rank
How do you do the opposite of De Morgan’s law?
!(a || b) == ?????????
!a and !b, distribute negation and flip symbol
The boolean expression
a[i] == max || !(max != a[i])
can be simplified to:
(A) a[i] == max (B) a[i] != max (C) a[i] < max || a[i] > max (D) true (E) false
(A) a[i] = max
When does short circuit evaluation come into play?
When there is multiple conditions.
Also, the conditions are run in order:
Not all conditions are ran
What will this code return given checkMethod(false, true)?
public int checkMethod(boolean x, boolean y) { if(!x) { if(y) { return -1; } else { return 1; } } else { return 0; } }
-1
Given the following call, what value would be returned for findTheMiddle(2110890125)?
public static String findTheMiddle(int number)
{
String stringNum = “” + number;
int mid = stringNum.length()/2;
if(stringNum.length() % 2 == 1) { return stringNum.substring(mid,mid+1); } else { return stringNum.substring(mid-1,mid+1); } }
89
What will this program print if the value of grade is 80?
if(grade > 90) { System.out.println("A"); } else if(grade > 80) { System.out.println("B"); } else if(grade > 70) { System.out.println("C"); }
C