|
Chapter: Branching
Relational Operators
Writing if() statements becomes really interesting when more than one
expression is used in the conditional. Up until now we have only used a
single expression as a condition.
Using a single
expression within a conditional
In the previous image the highlighted area indicates a single
expression for the conditional in the form of a variable called
myBooleanVar which has been datatyped as a boolean, in this case the
variable has been assigned the value of true, therefore the conditional
evaluates to true. This simplistic approach to using if statements can
be useful in some situations but, the ability to use comparison within
a conditional statement extends it's usefulness much further.
Comparison
A comparison requires at least two entities (which are most commonly
numerical values and/or expressions in Processing, but can extend
into strings in some other programming languages) and a relational
operator (also known as a comparison operator in other programming
languages). Comparisons included within a conditional, just like the
previous expressions we've been working with, must also evaluate to a
value of either true or false. Comparisons simply instruct the program
to evaluate the two expressions on either side of the relational
operator and compare the results of the two expressions with each other
and from this comparison return a boolean of either true or false.
Relational operators include:
< less than
> greater than
== equality
<= is less than or equal to
>= is greater than or equal to
!= inequality
Let's have a look at how we can use the a relational operator to
compare two values.
(5>1)
This is a conditional extracted from an if() statement, it consists of
a single comparison. The comparison returns a value of true and
would be read as “ five is greater than one”. The next example
demonstrates a comparison that returns a value of false:
(5<1)
This comparison is read as “five is less than one”. This is not true
and the program will return a value of false. Comparisons are not
limited to simple int's and can be used to evaluate the relationship
between complex expressions. Following is an example of a conditional
code fragment comparing two expressions:
(mouseY > height/2)
The Y position of the mouse is compared with the system variable,
height when it is divided by two, which as you know is the middle of
the Display Window's height. A conditional such as this can be more
useful in terms of creating dynamic content because when placed within
the draw() structure the value that is returned each time the code is
repeated may or may not be the same as the previous return value,
depending on whether the user's mouse is in the bottom half of the
Display Window or not.
Lets have a look at how we can use this code fragment in a sketch. Try
running the following code. Start by hovering your mouse over the top
of the Display window then moving it to the bottom of the Display
Window and watch what the console prints.
void setup() {
size(300,300);
}
void draw() {
if( mouseY >= height/2){
println("Mouse is in the bottom half of the sketch");
println(mouseY);
}
println("Mouse could be in either half");
}
The program starts, as always, by running the code within the setup()
structure then moves onto draw(). Within draw() it runs the if()
statement which instructs the program to check whether the mouse's Y
position is greater than or equal to the height of the Display Window
divided by 2. For example in our sketch the Y parameter of the size
function within setup() is 300 so if the user has placed the mouse at a
Y position that is anywhere between 150 and 300 then the conditional
will return a value of true as these values are all greater than or
equal to 150 (i.e. height/2). It's also worth noting that the
relational operator that has been used in this comparison is the >=
(greater than or equal to) operator, which means that the value that is
returned on the right side of the operator is included within the range
of values (as the minimum value in this range) that will cause the
conditional to evaluate to true. What this means is that if you want a
value of false returned the mouse must be within the range of 0 to 149.
If the conditional returns a value of true meaning that the mouseY
system variable has a current value of 150 or anything between 150 to
300, the program then runs the code delimited within the if()
structure, which in this case tells Processing to print "Mouse is in
the bottom half of the sketch" and then print the value of the mouseY
system variable. The program then moves on to the rest of the code
within the draw() structure. This tells the program to print "Mouse
could be in either half", as this statement exists outside of the if()
structure, it will be evaluated regardless of whether the if()'s
conditional is returned as true or false.
if()
structures can increase the amount of options that can be programmed
into a sketch.
The program then loops back to the beginning of the draw() function and
goes through the code within the draw() structure again as it has
reached the end of the draw() structure at this point. If the users
mouse is not within the bottom half of the Display Window this time the
comparison conditional will return a value of false, the program will
then skip the code delimited within the if() structure that tells it to
print "Mouse is in the bottom half of the sketch" and move onto the
rest of the code within the draw() structure that once again tells it
to print "Mouse could be in either half".
As you can see branching provides us with a very easy way of changing
the behavior of our program by including variables within a comparison
that in themselves are dynamic.
|
|