Video from Dave’s February 8th Class

GMT20230208-190409_Recording_1550x752.mp4

Today:

Numerical Expressions - Review

3 + 4 is an expression that evaluates to 7

4 + (9 * 2) is an expression that evaluates to 22

12 % 10 is an expression that evaluates to 2 (We covered % in last week’s review session, it is the modulus operator. It determines the reminder if the two numbers were divided. (12/10 is 1 with a remainder of 2… i.e. a modulus of 2)

Boolean Expressions

There is a certain type of expression which evaluates to a result of either true or false.

3 > 4 # Evaluates to **false**
3 <= 4 # Evaluates to **true**
3 == 3 # Evaluates to **true**
3 == 2 # Evaluates to **false**

# You can also use Boolean Logic such as AND and OR
# && (Two Ampersands) is the equivalent of AND
# || (Two Pipes) is the equivalent of OR
# On a Mac and most PCs, the Pipe **|** is located on key above RETURN/ENTER

(5 > 4) && (3 < 4) # Evaluates to true because both expressions are true
(5 > 4) && (3 > 4) # Evaluates to false because (3>4) is false 
(5 > 4) || (3 > 4) # Evaluates to true because (5>4) is true

# &&

# true && true evaluates to true
# true && false evaluates to false
# false && true evaluates to false
# false && false evaluates to false

# ||

# true || true evaluates to true
# true || false evaluates to true
# false || true evaluates to true
# false || false evaluates to false

# Note that you can also assign the result of a Boolean expression to a variable

x = (3 > 4);  # After this statement x will be equal to false

# So far all examples we have done in class have shown variables containing numbers
# In the above example we introduce another type of data, Booleans, which can also
# be stored in Variables. In summary, x is now a Boolean variable

# You can also initialize a variable with a boolean value

let x = true;
let y = false;

# You can store the result of an expression into a variable
let x = (5>4);   # x is equal to true
let y = (3<4);   # y is equal to true
let z = (3>4);   # z is equal to false

let a = (x && y)  # a is true because both x and y are true
let b = (x && z)  # b is false because x is true and z is false (they are not both true)

# The "not" command will invert the value of a boolean
# Not is done with the exclamation mark: "!"

let myValue = !false;   # myValue will equal true
let a = !(5>4);         # a will equal false

# There are some global P5 variables that are boolean
# mouseIsPressed  returns true if mouse is pressed
# keyIsPressed    returns true if a key on your keyboard is pressed

String Expressions

"Dave" + "Stein"    # Evaluates to "DaveStein"