Presentation is loading. Please wait.

Presentation is loading. Please wait.

PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.

Similar presentations


Presentation on theme: "PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions."— Presentation transcript:

1 PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions

2 Introduction There are situations where you make a decision and select alternative paths in execution. “if” statements in Processing help you implement this in your program “if” is also called “selection” statement We will look at the “syntax” or “structure” of the “if” statement and its “semantics” or meaning.

3 “if” syntax and semantics if (test) { statements} Meaning: if test is “true” then execute the statements If the test is “false” then skip the statements and go the statement after the “if” statement. “test” is a condition expressed using relational exapressions Example: if (x > width) { x = 0;}

4 “if..else” statement Syntax: if (condition) { statements 1 } else { statements 2 } Example: if (x <= width) { x = x + 1;} else // we assume x > width { x = 0; }

5 Conditions How to write the conditions? Conditions evaluate to Boolean value of true or false Operators you can use to write conditions are: > greater than < less than >=, < = == equality != not equal to || logical or && logical and ! Logical not We will learn these as we go along using examples See p.54 for if semantics

6 Lets look at some examples Define x as an integer. Initialize it to a random value. Hmm.. how to generate a random number in Processing? (int)random(range) Let the random number be in the range 0-width of the canvas. Repeat the same for a variable y. But random number is Draw a line between x,y and mouseX, mouseY If x > y stroke color is black Else stroke color is white Let the background be blue.


Download ppt "PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions."

Similar presentations


Ads by Google