Boolean Expressions
A Boolean expression is one that evaluates to true or false.
They are used in loops to tell us when to stop and in ifs to tell us when to enter
each part of the if.
Boolean Operators (join up pieces of a Boolean expression)
- && - and – both things must be true for the expression to be true
- || - or – one or both things must be true for the expression to be true
- ! – not – if the expression is true, it becomes false, and vice versa
Relational Operators (show relationships between data)
- < - less than
- > - greater than
- == - equal to. NOTE: it isn’t =, that is the assignment statement symbol
- >= - greater than or equal to, also <=
- != - not equal to
Important Notes about Boolean Expressions (compareTo and ==)
- Because Strings are built up of chars, it is impossible to use == to compare
two strings and see if they are equal. Instead you must use compareTo.
- To see if two Strings, fred and “apple” are equal type:
fred.compareTo(“apple”)==0
- To see if two String variables banana and cat are not equal type:
- Remember that = is used only for assignment statements. You cannot use
it for a Boolean expression. Use == if you want to test if two primitive types
are equal.