Boolean Expressions
Overview: 3.1.0
Boolean
variables and expressions can only havetrue
andfalse
values
Testing Equality: 3.1.1
==
and!=
can be used to compare values- Return
true
orfalse
boolean values - Single equals is assignment (changes a variable’s value); double equals checks for equality! (doesn’t change value)
- Also used to test if two reference values refer to the same object
- Return
Relational Operators: 3.1.2
- Relational operators compare numeric values/expressions
<
less than>
greater than<=
less than or equal to>=
greater than or equal to==
equal to!=
not equal to
Testing with mod: 3.1.3
// Test if a number is positive
(number > 0)
//Test if a number is negative
(number < 0)
//Test if a number is even by seeing if the remainder is 0 when divided by 2
(number % 2 == 0)
//Test if a number is odd by seeing if there is a remainder when divided by 2
(number % 2 > 0)
//Test if a number is a multiple of x (or divisible by x with no remainder)
(number % x == 0)
Summary: 3.1.5
- Primitive values and reference values can be compared using relational operators
- Arithmetic expressions and values can be compared using relational operators
- An expression using relational operators evaluates to a boolean value
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.