pages:51-59 section: control1 : decisions decisions

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

Upload: evelyn-potter

Post on 01-Jan-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

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

PAGES:51-59SECTION: CONTROL1 : DECISIONS

Decisions

Page 2: PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions

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

“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

“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

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

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.