pages:51-59 section: control1 : decisions decisions
TRANSCRIPT
![Page 1: PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649ec45503460f94bcee08/html5/thumbnails/1.jpg)
PAGES:51-59SECTION: CONTROL1 : DECISIONS
Decisions
![Page 2: PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649ec45503460f94bcee08/html5/thumbnails/2.jpg)
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” statementWe will look at the “syntax” or “structure” of
the “if” statement and its “semantics” or meaning.
![Page 3: PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649ec45503460f94bcee08/html5/thumbnails/3.jpg)
“if” syntax and semantics
if (test) { statements}Meaning: if test is “true” then execute the statementsIf the test is “false” then skip the statements and go the statement after the “if” statement.“test” is a condition expressed using
relational exapressionsExample: if (x > width) { x = 0;}
![Page 4: PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649ec45503460f94bcee08/html5/thumbnails/4.jpg)
“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; }
![Page 5: PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649ec45503460f94bcee08/html5/thumbnails/5.jpg)
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
![Page 6: PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions](https://reader036.vdocuments.us/reader036/viewer/2022071807/56649ec45503460f94bcee08/html5/thumbnails/6.jpg)
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, mouseYIf x > y stroke color is blackElse stroke color is whiteLet the background be blue.