An Introduction To Programming With Processing

Branching

When we write a program we generally don't want the script to do the same thing every time we use it. Often, what we want is a script that is versatile in it's application. Excluding this versatility would make the interactive software we create predictable and boring. We therefore use certain built-in structures that help us control the way in which our programs run, allowing us to skip blocks of code and execute other blocks of code before others in a non-linear way. This can be done in a dynamic and changing sequence based on the users input, the logic of the program, the time of day or whatever other characteristic we choose to model into a program. The process of creating this dynamic ordering that determines the programs flow of control is what is referred to as branching. Branching is also referred to as conditional programing and is the process of creating code that has the ability to choose a certain path based on a set of conditions stipulated in the program but which can also be influenced by conditions outside of the program, such as a user interaction. The conditional statement is syntactically a single statement made up of multiple branching statements. What determines the course of action that the program follows is a boolean value of true or false (which we will discuss in more detail later) returned from a comparison within the structure of a conditional statement.

The if() structure

The if() structure can also be referred to as an if statement because although, the statement is in practice made up of multiple statements the structure of the if statement is treated like a single statement during compilation and/or interpretation. if statements must always have a conditional to qualify as an if statement. An example of the structure of an if statement follows:

if() statement structure
Multiple if statements can be used within the same program and even within the same structural function such as draw(). They are identified by the if keyword followed by parenthesis containing a conditional. The conditional for an if() statement will always return a value that is true or false through the process of evaluation which can be based on a comparison expression. We'll have a look at the process of comparison a bit later, for now we'll focus on the boolean datatype. Data that can either be true or false is a primitive datatype known as a boolean. Just like all primitive data types booleans can be declared with a keyword followed by a variable name.
Type Casting Errors
The expression within the conditional of an if() statement must, through the process of evaluation, return a boolean value of either true or false in Processing, in order for the program to continue. In other programming languages the conditional can also evaluate to a 1 or a 0, with 1 being representative of true and 0 being representative of false. The language's compiler/interpreter will then convert the 0 or 1 to a boolean equivalent value through a process known as type casting. Processing, however, requires that the conditional evaluate to a boolean value of true or false, so that automatic type casting can be avoided. As you are aware 1 and 0 are of type int in Processing and the Processing does not automatically convert them to boolean values. In the event of your conditional evaluating to an int or datatype other than a boolean you will receive a “cannot convert” datatype error. Later we will have a look at how to use Processing's in-built ability to prevent automatic type casting to our advantage. Although automatic type casting is favored in some programming languages it is usually an inherent legacy feature of a lower level language that a current higher level language is based on. In Processing type casting must be done explicitly with a type casting function. If we were to declare a boolean variable and use it in an if statement, it might look something like this:
boolean myValue = true;        /*initialization*/

if(myValue){                              /*if conditional returns true*/ 
      … run these statements    /*statements within if() structure are run*/
    }                
                    
...then run these statements    /*if conditional is false entire if structure is skipped*/
In the previous code listing we have used initialization to declare a variable of type boolean and assign the value of true to it. We then used the new variable in an if() statement conditional (the part between parenthesis following the if keyword). An if statement works by evaluating the expression within it's conditional, if the conditional evaluates to true the code delimited within the if() statements braces will be run. However if the conditional evaluates and a value of false is returned, it will skip the code within the if() statement's braces and proceed to the statements that follows, for example:
boolean myValue = false;                 /*variable initialized to false*/

if(myValue){                                        /*conditional evaluates to false*/
      //… don't run these statements    /*statements within if() structure are skipped*/
    }

...run these statements                     /*entire if() structure is skipped, code following if() structure is run instead*/
Using this method of programming we have allowed our program to make a decision based on certain conditions, in a real world situation the conditions the program would evaluate would usually be dynamic for example a user interaction or some other dynamic feature.