Unit 3 College Board Booleans and If-Else
- all non zero numbers are true
- the most common example: 1 = true; 0 = false
- == checks for comparison
- not to be confused with = which is assignment
- != checks for inequality
- <, >, >=, <=
- these operands do their normal functions
- || relates to the or function
- && related to and function
New Vocab
Compound Boolean Expression
- Logical operators allow you to combine and connect booleans in useful ways.
- not, or, and
- ! operator takes precedence over &&, and && takes precedence over || Ex: a && b || b
Truth Tables Or operator true true true true false true false true true false false false And operator true true true true false false false true false false false false Not operator true false false true
DeMorgans Law
- easy reminder-- De Morgan’s Laws: move the NOT inside, AND becomes OR and move the NOT inside, OR becomes AND Ex: !(c == d) is equivalent to (c != d)
!(c != d) is equivalent to (c == d)
!(c < d) is equivalent to (c >= d)
!(c > d) is equivalent to (c <= d)
!(c <= d) is equivalent to (c > d)
!(c >= d) is equivalent to (c < d)
Comparing Strings, Objets, Numbers
- comparing the objects with == returns true
- You can also do this comparison with != to check if two objects are not aliases
- .equals(). This method is called from a variable and it passes a parameter to compare to in order to determine if the values are equal
strings Ex:
objets Ex: Rectangle one = new Rectangle(3,7); Rectangle two = one;if(favoritMovie.equals("Gentlemen")) { System.out.println("Good Choice"); } else { System.out.println("Are you sure that's your favorite movie?"); }
boolean same = one == two; // Will be true numbers Ex: if(one.equals(two)) { System.out.println(one + " is equal to " + two); }
int temp = 90;
boolean sunny = true;
if (temp > 70 && sunny) {
System.out.println("Time to get outside!");
}
boolean a = true;
boolean b = false;
if (a) {
System.out.println("True code block");
}
if (a && !b) {
System.out.println("True code block");
}
if (a || b) {
System.out.println("True or False code block");
}
if ((a && !b) && (a || a)) {
System.out.println("true or false");
}
if (!a && b ) {
System.out.println("False code block");
}
int x = 0;
int y= 1;
System.out.println(x == y);
public int getChargeStartTime(int chargeTime) // determines start time to charge battery at lowest cost
{
int startTime = 0;
for (int i = 1; i < 24; i++) // iterate through the differeant start times with a returned values <= 23
{
if (this.getChargingCost(i, chargeTime)
< this.getChargingCost(startTime, chargeTime))
{
startTime = i;
}
}
return startTime;
}
public boolean isStrictlyIncreasing(){
for(int i = 1; i < digitList.size(); i++){ // iterate through the digits
if (digitList.get(i-1).compareTo(digitList.get(i)) >= 0)
{
return false;
}
else
{
return true;
}
}
}
public boolean isBalanced(ArrayList<String> delimiters) {
int open = 0;
int close = 0;
for (String d: delimiters) {
if (d.equals(openDel)) {
open++;
} else if (d.equals(closeDel)) {
close++;
}
if (close > open) return false;
}
return open == close;
}