Boolean operators

Edit on Github instead

Relational operators

Booleans respect relational operators:

Operator Example Meaning
< 5 < 10 "5 is less than 10"
> 10 > 7 "10 is greater than 7"
<= 5 <= 10 "5 is less than or equal to 10"
>= 10 >= 7 "10 is greater than or equal to 7"
== 7 == 7 "7 is equal to 7"
!= 8 != 10 "8 is not equal to 10"

Boolean operators

Boolean operators allow you to express statements that combine pieces together.In English, we use words like AND, OR, IS and NOT to do this:

  • "It is raining today and you should carry an umbrella."
  • "I am full or the refridgerator is empty."
  • "Your house is not the same as my house, and mine is better."

Boolean operators replace those words with keywords the computer can understand:

Operator Example Translation
&& rain == true && umbrella == true "It's raining today and you should carry an umbrella"
|| full == true || emptyRefridgerator == true "I am full or the refridgerator is empty."
! yourHouse != myHouse && myHouse > yourHouse "Your house is not the same as my house, and mine is better."

Here's a few examples:

Expression Translation Answer
!true "not true" false
!!false "not not-false" true
!(2 == 2) "2 isn't equal to 2" false
7 > 4 && 8 > 34 "7 is greater than four and 8 is greater than 34" false
7 > 4 || 8 > 34 "7 is greater than four or 8 is greater than 34" true

Suggest changes

Related links

URL Topic Source