home/ atoms/ processing-if-else-conditionals

Processing's if-else structures let programs branch based on relational expressions

Control flow in Processing is changed with conditional structures. A relational expression compares two values with operators (>, <, >=, <=, ==, !=) and evaluates to either true or false. The if block runs when the expression is true; the else block (optional) runs when false. Combining else with if allows multiple exclusive branches. Logical operators (&&, ||, !) combine relational expressions: && requires both to be true, || requires at least one, and ! inverts. Conditionals can be nested to test secondary conditions inside a primary one.

Examples

if (mouseX < 50) { fill(255); } else { fill(0); }

Assessment

Write an if-else-if chain that draws a circle when mouseX < 33, a square when mouseX is 33-66, and a triangle when mouseX > 66.

“Code elements called relational expressions evaluate to true and false. A relational expression is made up of two values that are compared with a relational operator.”
corpus · processing-handbook-no-login-mirror-pdf-reas-and-fry · chunk 19