0% found this document useful (0 votes)
23 views

p5 Conditional

There are four types of statements covered in the course: assignment, function calls, iteration, and conditional statements. Conditional statements allow selectively executing code blocks using IF statements. There are three main forms of IF statements: IF-THEN (executes block1 if condition is true), IF-THEN-ELSE (executes block1 if true, block2 if false), and IF-ELSE IF-ELSE (tests conditions in order until one is true, executes associated block). Logical operators like AND (&&), OR (||), and NOT (!) can combine conditions to check multiple requirements.

Uploaded by

Ju Matute
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

p5 Conditional

There are four types of statements covered in the course: assignment, function calls, iteration, and conditional statements. Conditional statements allow selectively executing code blocks using IF statements. There are three main forms of IF statements: IF-THEN (executes block1 if condition is true), IF-THEN-ELSE (executes block1 if true, block2 if false), and IF-ELSE IF-ELSE (tests conditions in order until one is true, executes associated block). Logical operators like AND (&&), OR (||), and NOT (!) can combine conditions to check multiple requirements.

Uploaded by

Ju Matute
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Statements in Processing

There are four kinds of statements covered in this course:

CS 101 - Introduction to Computer Science Conditional Statements


Dr. Stephen P. Carl

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

Forms of the IF statement

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

Example using this form of IF

A Second if Form

To select one of two blocks of code:


// if x-coordinate of the mouse is in left half of window if (mouseX < width/2) { // draw this half white fill(255); rect(0, 0, width/2, height); } // next line executes no matter what...

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.

Examples using the else-if form


if (mousePressed) { background(255); } else if (keyPressed) { background(157); } else { background(0); }

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

Checking Ranges using &&

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

You might also like