An Introduction To Programming With Processing

Extending the if() structure with else

In our previous example, we where able to run statements based on whether the mouse was in the bottom half of the Display Window. However, If we change the conditional by substituting a less than or equal to relational operator instead of a greater than or equal to relational operator (as in the original example):
(mouseY <= height/2)
We could get Processing to run a set of statements based on whether the mouse is in the upper half of the Display Window. But what if we wanted Processing to run a specific set of statements when the mouse is in the upper half of the Display Window and a different set of statements when the mouse is in the bottom half of the Display Window? Using an if statement by itself to fulfill this purpose would not be possible, but fortunately we can extend the if() statement into an if() structure with the else keyword. Use of the else keyword within an if() structure ensures that even if the conditional returns a value of false that the code that follows else (which is still within the if() structure) will be run, before the program exits the if() structure and moves on to the rest of the statements making up the program. This can effectively provide us with a fail safe method of ensuring that code is run within an if() structure before the program exits the structure. For example if we wanted our sketch to tell us whether the mouse is specifically in the upper or lower half of the Display Window we would modify the previous sketch to look like this:
void setup() {
  size(300,300);
 }

void draw() {
  if( mouseY >= height/2){
        println("Mouse is in the bottom half of the sketch");
        println(mouseY);
  }else{
        println("Mouse is in the upper half of the sketch");
        println(mouseY);
  }
println("Mouse could be in either half");
}
Notice that as soon as you start the sketch and if your mouse is not hovering over the Display Window, Processing will read the mouseX and mouseY system variables as being 0 and 0. As a result the first branch of the if() structure is skipped because the conditional evaluates to false (0 is less than 150) and the second branch is immediately run, that being the else code block. Once you start moving your mouse around in the Display Window you will see the println() statements execute depending on which half of the Display Window the mouse is in.

Extending the if() construct with if else if else

There are many times when you will require more than the two branches that an if else structure will permit, and in that case you can use the process of nesting multiple if else structures within others to form an if() construct. For example, in our previous sketch when our program started and our mouse was not hovering over the Display Window, Processing rightfully skipped the first if() code block or more generically and appropriately named control structure and went to the else structure. However we could capture this unique possibility of the mouse having an X and Y value both equating to 0 in a condition and associate it with it's own control structure. An if else if else construct enables us to capture as many possibilities as we can conceive and associate them with their own unique control structures. An example of the pattern that an if else if else construct follows, is listed below:

if(condition){
    ...statements
    }else 
    if(condition){
       ..statements
                   }else{
                 ...statements
}
As you can see nesting if else constructs within each other is pretty easy, but when typed in the previous format they can be a bit difficult to read. As a result they are generally written in the following format, remember that because Processing is a free form programming language you are free to format the statements as you wish, however certain formats might be easier to read than others, for example the traditional format for an if else if else construct follows:
if (condition){
   ...statements
   }else if(condition){
      ...statements
      }else{
         ...statements
}
There are no limits to how many if else structures you can nest within an if else construct, but bear in mind that nesting too many if else structures within others can make it difficult to follow and keep track of what is going on in your code. Processing and many other higher level languages have structures such as switch() that might serve your purposes better, if you want your program to be able to branch between a possibility of three or more control structures.

Logical Operators

Logical operators allow us to to extend our conditionals by creating a means of comparing one relational expression with another. They come in the format of:

&&      logical AND
||      logical OR
!       logical NOT
These operators can be used within a conditional to return a value of true or false based on the context in which they are used, for example logical AND will return a value of true only when both expressions on either side of it evaluates to true and can be used as in the following examples:
(5>3 && 3>1)        //Returned as True, because both expressions are true

(5>3 && 3<1)        //Returned as False, because the expression in the right is false
logical OR returns a value of true if at least one of the expressions on either side of it is true, for example:
(5>3 || 3>1)        //Returned as True, because both expressions are true 

(5>3 || 3<1)        //Returned as True, because the expression on the left is true

(5<3 || 3<1)        //Returned as False, because both expressions are false
logical NOT (also traditionally referred to as “bang”) works in a somewhat different manner to that of it's counterparts. It will return a value that is the opposite of the expression it is evaluating. For example if an expression such as x>1 evaluates to true, then !(x>1) will evaluate to false. However, be cautious of how you choose to use this operator as you can often express the meaning of your code in another format that might be easier to read, for example:
if (!(x>1))
...     
//can also be expressed as, 
if (x<=1)
... 
However the logical NOT operator can be useful, for toggling the boolean value of an expression when that expression is required in several places in a certain state and only once in the opposite state. For example:
boolean x = true;
ellipseMode(CORNER);
if (x) {
  rect(25, 25, 50, 50);
}else{
  ellipse(25, 25, 50, 50);
}
if (!x) {
  rect(25, 25, 50, 50);
}else{
  ellipse(25, 25, 50, 50);
}
println(x);    //x still has the value of true associated with it
Now that we have an understanding of the context in which branching exists, the following example of slider.pde will demonstrate a usage for it amongst other programming features within a typical sketch.