This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.


?: (conditional)

int s = 0;
for (int i = 5; i < 100; i += 5) {
  s = (i < 50) ? 0 : 255;
  line(30, i, 80, i);
Description A shortcut for writing an if and else structure. The conditional operator, ?: is sometimes called the ternary operator, an operator that takes three arguments. If the test evaluates to true, expression1 is evaluated and returned. If the condition evaluates to false, expression2 is evaluated and returned.

The following conditional expression:
result = test ? expression1 : expression2

is equivalent to this structure:
if (test) {
result = expression1
} else {
result = expression2
test ? expression1 : expression2
test any valid expression which evaluates to true or false
expression1 any valid expression
expression2 any valid expression
Updated on June 6, 2020 10:05:53pm EDT