4a – Conditionals revised

We have seen how conditional if statements allow us for test for something in our code.

Screen Shot 2015-02-16 at 9.22.43 PM

These tests are evaluated as true or false and are defined by the following operators:

Screen Shot 2015-02-16 at 9.19.36 PM

When these conditionals are true, we run the code block defined by the curly brackets, when they are false we skip through ignoring the code in the brackets.

These if statements can be combined using an else or an else if, as shown below. (Image is from Reas and Fry textbook, chapter 5, control 1)

Screen Shot 2015-02-16 at 9.24.53 PM

Logical Operators

Logical operators are used to combine two or more relational expressions and to invert logical values. They allow for more than one condition to be considered simultaneously. The logical operators are symbols for the logical concepts of AND, OR, and NOT:

Screen Shot 2015-02-16 at 11.08.02 PM

These are all the possible combinations and results:

Screen Shot 2015-02-16 at 11.07.57 PM

 

Exercises

1. Using mouseX and mouseY, and fill() create a roll-over rectangle that changes color when the mouse is hovering over it.

2. Duplicate your rectangle sketch so that it only changes color when the mouse is hovering over it and is pressed.