conditionals

Conditional statements allow the program to make a decision between two or more options. In Processing, we use if () / else statements to do this. In order to understand how to use if () / else statements, we first need to take a quick swing through Boolean logic.

Boolean logic

We have already seen the basics of Boolean logic: a datum is either true or false.

The basic Boolean (logical) operators are:

The basic Boolean operations are:

if ()

The semantics for this kind of statement are simple:

if test is true then do these statements.

In Processing, the proper syntax is:

if (condition) { 
  // statements 
}

condition must take the form of a Boolean statement, meaning that it must evaluate to either true or false. In Processing, we create Boolean statements by using variables of type boolean and logical operators represented by the following special symbols:

Here are some examples.

// remember that true and false are reserved words
boolean trueArg = true;
boolean falseArg = false;

if (trueArg) { 
  println("1. This conditional executes and you will see this text output.");
}     

if (falseArg) {
  println("2. This conditional does NOT execute and you will NOT see this text output.");
}    

if (trueArg && falseArg) {
  println("3. This conditional does NOT execute and you will NOT see this text output.");
}

if (trueArg || falseArg) {
  println("4. This conditional executes and you will see this text output.");
}

if (!trueArg) {
  println("5. This conditional does NOT execute and you will NOT see this text output.");
}

if (!falseArg) {
  println("6. This conditional executes and you will see this text output.");
}

else

The else is a way of extending your choice. The semantics are an extension of what we have already seen:

if test is true then do these statements
if not then do some other statements

In Processing, the proper syntax is:

if (condition) { 
  // statements
} else {
  // other statements
}

Here is an example:

boolean arg = false;

if (arg) {
  println("1. This conditional returns false so you will NOT see this text output.");
} else { 
  println("2. The ELSE executes and you will see this text output.");
}

else if ()

The else if is a way of extending your choice even further. The semantics are:

if test is true then do following statements
else if test is true then do some other statements
else if test is true then do some other statements
else if test is true then do some other statements
...

In Processing, the proper syntax is:

if (condition) { 
  // statements
} else if (other condition) {
  // other statements
}

Here is an example:

boolean firstArg = false;
boolean secondArg = true;

if (firstArg) {
  println("1. This conditional returns false so you will NOT see this text output.");
} else if (secondArg) { 
  println("2. This conditional returns true so you will see this text output.");
}

An important point to remember is that only one of these tests will go through, even if more than one evaluates to true. The test that goes through is determined sequentially; if the first conditional is true, its statements are executed. If the first conditional is false and the second one is true, the second conditional's statements are executed. If the first condition is true and the second one is also true, only the first conditional's statements are executed.

A final else can also be added to the end of an if () / else if () chain, allowing a default option that will be executed if all the previous conditions are false.

relational operators

You can also use the relational operators in your conditionals (and loops). These serve the purpose of combining and comparing conditionals. In Processing the relational operators are:

We will now use these operators to build a sketch that alternates between red and blue, depending on whether the current frame is odd or even. We will do this by creating a conditional on the frameCount variable to alternate between backgrounds:

// if the frame count is even...
if (frameCount%2 == 0) {
  // ...draw a red background
  background(216, 35, 35);
} else {
  // draw a blue background
  background(35, 103, 216);
}

Let's add an exception to the rule: if the frame count ends with 0, draw a yellow background. Note that we need to place the new condition first in the list because any frame count ending with 0 is also necessarily even.

// if the frame count ends with 0...
if (frameCount%10 == 0) {
  // ...draw a yellow background
  background(240, 229, 17);

// if the frame count is even...
} else if (frameCount%2 == 0) {
  // ...draw a red background
  background(216, 35, 35);

} else {
  // draw a blue background
  background(35, 103, 216);
}

We will now slightly modify the rule: the background should only turn red if the mouse is on the left side of the window.

// if the frame count ends with 0...
if (frameCount%10 == 0) {
  // ...draw a yellow background
  background(240, 229, 17);

// if the frame count is even and the mouse is on the left side...
} else if ((frameCount%2 == 0) && (mouseX < width/2)) {
  // ...draw a red background
  background(216, 35, 35);

} else {
  // draw a blue background
  background(35, 103, 216);
}

Processing Workshop

Elie Zananiri
Alberta College of Art + Design
3-5 April 2008