# Comp 110 Boolean Operators

The following operators form boolean expressions because they evaluate to a single boolean value. For example, 1 < 2 evaluates to true while 3 < 2 evaluates to false.

== - Is equal to? - Be careful with this one! One equals sign assigns a value, two equals signs checks for equality. See code below for example.

``````int myAge = 18; // Gives the myAge variable a value of 18
if (myAge == 18){ //myAge == 18 is the same thing as writing true!
System.out.println("This code executes, because 18 == 18");
}``````

!= - Is not equal to? - This boolean operator checks to see if values are not equal to each other.

``````int aNumber = 3; // Gives aNumber a value of 3
if (aNumber != 500){
System.out.println("This code executes because 3 does not equal (!=) 500");
}``````

> - Greater than - This boolean operator checks values to see if the one value is greater than another value.

``````int yourCreditHours = 25;
int maxCreditHours = 18;
if (yourCreditHours > maxCreditHours){
System.out.println("Oh no! You have too many credit hours!");
}``````

>= - Greater than or equal to - This boolean operator can be used to see if a value is greater than or equal to a value.

``````int yourAge = 18;
int minimumAgeOfPresident = 35;
if (yourAge >= minimumAgeOfPresident){
System.out.println("You can run for election");
}//note, the code above does not execute because you are not 35 or older``````

< - Less than - This boolean operator checks values to see if the one value is less than another value.

``````int coffeeCupsDrankToday = 5;
System.out.println("You can have another coffee");//we are all addicted
}``````

<= - Less than or equal to - This boolean operator can be used to see if a value is less than or equal to a value.

``````//you can use boolean operators with double values too!
double priceOfLargeFrosty = 2.29;
double moneyInWallet = 0.0;
if (moneyInWallet <= priceOfLargeFrosty){
System.out.println("You cannot afford a Large Frosty");
}``````

&& - Logical AND operator - This boolean operator is used to link two boolean expressions together, in order to make sure BOTH expressions evaluate to true. See if you can figure out why the code blocks below execute, or why they do not.

``````double myGPA = 2.5;
if (myGPA > 2.0 && myGPA <= 3.0){
System.out.println("Your GPA is a C average");// this code executes
}
if (myGPA > 3.0 && myGPA <= 3.5){
System.out.println("Your GPA is a B average");// this code doesn't execute
}``````

|| - Logical OR operator - This boolean operator is used to link two boolean expressions together, in order to make sure EITHER expression evaluates to true. The code below

``````double phoneBattery = 15.0;
boolean broughtCharger = true;
if (phoneBattery > 80.0 || broughtCharger){
System.out.println("You can play Pokemon Go for more than 5 minutes!");
}``````

Notice that the above code executes, even though phoneBattery is less than 80.0. This is because the other expression in the boolean statement is true. Remember, with && both statements must be true, and with || only one statement has to be true.