p5 Conditional
p5 Conditional
We've seen the assignment statement and function calls that have a side effect, such as drawing a shape. rect(10, 10, 50, 50); is a function call Iteration statements allow us to repeatedly execute a code block as many times as necessary Conditional statements allow us to execute one code block while possibly skipping others
Conditional Statements
It is often necessary to selectively execute one of a (potentially large) number of code blocks. A code block is a set of statements inside {...} There are several statements which we can use to do this. In this course we'll cover just the if statement.
The simplest form of the if statement is: if (condition) block1 This form executes statements in block1 if the condition is true. Otherwise, it skips them. Execution continues normally with the first line after block1
1-4
A Second if Form
if (condition) block1 else block2 If the condition is true, the statements in block1 are executed and the statements in block2 are skipped over. If the condition is false, the statements in block1 are skipped over and the statements in block2 are executed. Execution continues normally with the first line after the else
More Examples
More Examples
color c = color(10, 58, 100); if (savingsBalance < 1000.0) { float intensity = (red(c) + green(c) + blue(c)) / 3.0; interestRate = 0.05; if (intensity < 128) { } else { fill(0); interestRate = 0.15; } else { } fill(255); savingsBalance = savingsBalance + (savingsBalance * interestRate); }
5-8
Another if Form
To select one from many blocks of code: if (condition1) block1 else if (condition2) block2 else block3 If condition1 is true, statements in block1 are executed and the other blocks are skipped. Otherwise, condition2 is tested and if true, block2 is executed. block3 is skipped. If condition2 is false, then block3 is executed.
There can be more than one else if clause. Each condition is tested in turn until one is found to be true. If none are true, the else clause is executed. However, the else clause is optional.
Logical Operators
Sometimes we want to test two (or more) conditions. For example, how can we ensure that a number is within some range of values? One way is to nest if statements:
if (temperature < 212) { if (tempature > 32) { // do something... } } // less than boiling point of water // but over freezing point
Another possibility is to combine the conditions. We want the temperature to be both less than 212 and more than 32. We can combine the two conditions using the and operator, written && in Processing.
if (temperature < 212 && temperature > 32) { println("Temperature is in liquid range"); } else { println("Temperature is out of range"); }
9-12
Logical Operators
Logical Operators
Other logical operators are || for testing that one or another condition is true, and ! which tests that something is not true.
// true if temp too low (freezing) or too high (boiling!) if (temperature <= 32 || temperature >= 212) {...} // true if temp is less than 212 if (!(temperature >= 212)) {...}
When writing a conditional using a logical operator, you can only apply them to true and false values. Writing an expression like:
if (temperature == 50 || 75) {...}
make no sense - this is trying to connect a true or false value with a number value.
13-14