Lesson Two: Everything You Need to Know

Slides:



Advertisements
Similar presentations
Creative Computing. \\ aims By the end of the session you will be able to: 1.Move objects around 2.Write simple interactive programs 3.Use the mouse position.
Advertisements

Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Lesson One: The Beginning Chapter 3: Interaction Learning Processing: by Daniel Shiffman Presentation by Donald W. Smith Graphics from text.
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
Lesson Four: More of the Same
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Making Decisions In Python
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Java Programming Khan Academy and Mrs. Beth Cueni.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Conditionals & boolean operators
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Flow of Control Part 1: Selection
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Copyright 2003 Scott/Jones Publishing Making Decisions.
1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 02b: Tutorial for Programming in Processing Jarek Rossignac.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Lesson Two: Everything You Need to Know
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Lecture 01b: C++ review Topics: if's and switch's while and for loops.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
1 SIC / CoC / Georgia Tech MAGIC Lab Rossignac Processing  Install Processing  Learn how to edit, run, save, export,
Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT.
Review Random numbers mouseX, mouseY setup() & draw() frameRate(), loop(), noLoop() Mouse and Keyboard interaction Arcs, curves, bézier curves, custom.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:
If/Else Statements.
p5.js mouse, keyboard and if’s
Chapter 4: Making Decisions.
Lesson Two: Everything You Need to Know
Topics The if Statement The if-else Statement Comparing Strings
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
Computation as an Expressive Medium
Topics The if Statement The if-else Statement Comparing Strings
Mouse Inputs in Processing
Chapter 5, Conditionals Brief Notes
Pages:51-59 Section: Control1 : decisions
Lecture 6: Conditionals AP Computer Science Principles
Boolean Expressions to Make Comparisons
Pages:51-59 Section: Control1 : decisions
IAT 800 Foundations of Computational Art and Design
Presentation transcript:

Lesson Two: Everything You Need to Know Chapter 5: Conditionals Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from text

Lesson Two: Everything You Need to Know 4: Variables 5: Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if, else Boolean variables 6: Loops Learning Processing: Slides by Don Smith

Three categories of operators Numerical (mathematical) operators: + - * / % Operates on numbers, the result is a number Relational operators: > < >= <= == != Operates mainly on numbers, the result is a logical value: true/false Logical operators: && || &&: and ||: or Examples. Learning Processing: Slides by Don Smith

What is a Boolean Expression? AND Something that resolves to either true or false (yes or no) Not maybe… Computers think in 1’s and 0’s Remember truth tables 1 = ON = True 0 = OFF = False Usually based on a comparison Are you 21 years old? Is changeCount less than 5? Is myScore between 80 and 89? Is lastName ‘Smith’? A B Output 1 OR A B Output 1 Learning Processing: Slides by Don Smith

Boolean Comparison Operators Similar to Algebra > greater than < less than >= greater than or equal to <= less than or equal to == eqality (equal to) Note: ‘=‘ is the ‘assignment’ operator: x = 5; != inequality (not equal to) Learning Processing: Slides by Don Smith

Boolean Expressions and if What is a Boolean Expression? A comparison that results in either a true or a false Where do I use it? Usually in parenthesis, after an if: if (age >= 21) // True Action if (mouseX < width/2) Only do ‘Action’ if condition is True Condition True False Action Learning Processing: Slides by Don Smith

Two-way conditionals: if else Use else for ‘false’ actions after an if test: ‘Binds’ with the closest if: if (age >= 21) // True Action else // age < 21 // False Action Take one of the two paths: True or False Good idea to use curly braces: if (age >= 21){ } else { } Condition True False False Action True Action Learning Processing: Slides by Don Smith

Examples and exercises Find the larger number of two numbers. Two versions: using if or using if…else… Moving rectangles with color changing. Three versions: Using if, using constrain, or using modulo. Develop a program to simulate a stopping car. Learning Processing: Slides by Don Smith

Multiple Actions What if I have more than one thing to do if true? Make a ‘block’ of code after the if: if (age >= 21) { // True Action 1 // True Action 2 } else // False Action Indentation is for humans if (age >= 21) Without the curly braces: Only the first statement after a conditional is executed True Action 2 is executed no matter what age is! And don’t forget to ‘match’ your curly braces! Condition True False False Action True Action 1 True Action 2 Learning Processing: Slides by Don Smith

Multiple-Way Branching: else if What if you want more than two paths? Use else if: if (age >= 21) // First True Action else if (age > 18) // Second True Action else if (age > 5) // Third True Action Only one action done Then go to the ‘bottom’ First Condition True First True Action False Second Condition True Second True Action False Third Condition True Third True Action False Learning Processing: Slides by Don Smith

else if with else at the end Two ‘true’ paths and one ‘neither’ path? Use else if: if (age >= 21) // First True Action else if (age > 18) // Second True Action else // Both False Action First Condition True First True Action False Second Condition True Second True Action False Both False Action Learning Processing: Slides by Don Smith

Example of Multi-way Branching Where the mouse is determines the background color if (mouseX < width/3) { background(255); } else if (mouseX < 2*width/3) { background(127); } else { background(0); } Learning Processing: Slides by Don Smith

Multi-way Branching: Which to test first? Learning Processing: Slides by Don Smith

Gradebook Application Determine the letter grade for a number 0-100 90 – 100: A 80 – 89.999 B 70 – 79.999 C 60 – 69.999 D Below 60: F How would you plan/code a solution? What would you test for first? What second? How many tests do you need? Learning Processing: Slides by Don Smith

Numeric Range testing You often have to determine if a number is in a specific range (min to max) Example: Which range is a number in? 0-25: Print “Young” 26-50: Print “Mid-Age” >50: Print “Mature” How would you plan/code a solution? What would you test for first? What second? Can this be done with only two tests? Learning Processing: Slides by Don Smith

if else Examples else ‘binds’ with closest if float r = 150; // variables float g = 0; float b = 0; void setup() { size(200,200); } void draw() { background(r,g,b); stroke(255); // Line down center line(width/2, 0, width/2, height); if (mouseX > width/2) // If right r = r + 1; // more red else // Else left r = r - 1; // less red if (r > 255) // Range Check r r = 255; if (r < 0) // Range Check r r = 0; else ‘binds’ with closest if You can use if with no else clause! Learning Processing: Slides by Don Smith

Range check with constrain( ) float r = 150; // variables float g = 0; float b = 0; void setup() { size(200,200); } void draw() { background(r,g,b); stroke(255); // Line down center line(width/2, 0, width/2, height); if (mouseX > width/2) // If right r = r + 1; // more red else // Else left r = r - 1; // less red r = constrain(r,0,255); // Range Check r Learning Processing: Slides by Don Smith

Three-way branching float r = 150; // variables float g = 0; float b = 0; void setup() { size(200,200); } void draw() { background(r,g,b); stroke(255); line(width * 2/3, 0, width * 2/3, height); line(width * 1/3, 0, width * 1/3, height); if (mouseX > (width * 2/3)) // right 3rd r = r + 1; else if (mouseX < (width * 1/3)) // left 3rd r = r -1; else // center 3rd ellipse(mouseX, mouseY, 30,30); r = constrain(r,0,255); // Range Check r Learning Processing: Slides by Don Smith

Exercise 5-3: Move a rectangle… but stop! float x = 0; void setup() { size(200,200); } void draw() { background(255); fill(0); rect(x,100,20,20); x = x + 1; // Keep x in left half // Conditional version: // constrain version: Learning Processing: Slides by Don Smith

Logical Operators: AND Sometimes two (or more) things need to be true before you want to do something Example: If age >= 16 AND permit == 1 Print “OK to drive” How do we spell ‘AND’? && ‘Nested ifs:’ One if, compound condition A B Output 1 int age = 17; int permit = 1; if (age >= 16) if (permit == 1) print(“OK to Drive”); else print(“Ride the bus”); int age = 17; int permit= 1; if (age >= 16 && permit == 1) print(“OK to Drive”); else print(“Ride the bus”); Remember: else ‘binds’ with closest if (without an else) Learning Processing: Slides by Don Smith

Logical Operators: OR Sometimes one of (two or more) things is enough to decide Example: If age >= 18 OR (age >= 16 AND permit == 1) Print “OK to drive” How do we spell ‘OR’? || (two vertical bars) A B Output 1 int age = 17; int permit = 1; if (age >= 18 || (age >= 16 && permit == 1)) print(“OK to Drive”); else print(“Ride the bus”); Learning Processing: Slides by Don Smith

Logical Operators: OR Sometimes one of (two or more) things is enough to decide Example: If age >= 18 OR (age >= 16 AND permit == 1) Print “OK to drive” How do we spell ‘OR’? || (two vertical bars) int age = 17; int permit = 1; if (age >= 18 || (age >= 16 && permit == 1)) print(“OK to Drive”); else print(“Ride the bus”); Note the use of parenthesis to ‘connect’ the AND clause Learning Processing: Slides by Don Smith

Exercise 5-5: Simple Rollover int x = 50; int y = 50; int w = 100; int h = 75; void setup() { size(200,200); } void draw() { background(255); stroke(255); // test if mouse is over the rectangle if ( mouseX.. && mouseY.. && ??? // Change the color of the rectangle rect(x,y,w,h); Learning Processing: Slides by Don Smith

Second half of Chapter 5 5.5: Multiple Rollovers 5.6: Boolean Variables 5.7: A bouncing ball 5.8: Physics 101 Learning Processing: Slides by Don Smith

Multiple Rollover areas Steps: Draw a white background Draw horizontal and vertical lines down the center If mouse is in top left, draw black rectangle in that quadrant If mouse is in top right, draw black rectangle in that quadrant If mouse is in bottom left, draw black rectangle in that quadrant If mouse is in bottom right, draw black rectangle in that quadrant But how can we tell which quadrant it is in? Upper Left: x = 0 to 99, y = 0 to 99 Upper right: x = 100 to 199, y = 0 to 99 … Learning Processing: Slides by Don Smith

boolean variables You may want to ‘remember’ if something is true or false and store it in a variable Then you can compare it to true or false Example: If age >= 16 AND permit == true Print “OK to drive” int age = 17; boolean permit = true; if (age >= 16 && permit == true) print(“OK to Drive”); else print(“Ride the bus”); Learning Processing: Slides by Don Smith

Using a boolean variable for Buttons A button is just a rollover area that senses mouse clicks. Testing if (mouse is in the area of the object) AND mouse clicked can be quite a number of comparisons! boolean button = false; int ulx = 50, uly = 50, w = 100, h = 75; // rect variables … if (mouseX > ulx && mouseY > uly && mouseX < ulx + w && mouseY < uly + h && mousePressed) button = true; else button = false; // Later in code if (button == true) print(“I’ve been clicked!”); Learning Processing: Slides by Don Smith

Using a boolean variable for Buttons A button is just a rollover area that senses mouse clicks. Testing if (mouse is in the area of the object) AND mouse clicked can be quite a number of comparisons! boolean button = false; int ulx = 50, uly = 50, w = 100, h = 75; // rect variables … if (mouseX > ulx && mouseY > uly && mouseX < ulx + w && mouseY < uly + h && mousePressed) button = true; else button = false; // Later in code if (button == true) print(“I’ve been clicked!”); if(button) // Does the same thing as if (button == true) Learning Processing: Slides by Don Smith

Using a boolean variable for Buttons A button is just a rollover area that senses mouse clicks. Testing if (mouse is in the area of the object) AND mouse clicked can be quite a number of comparisons! boolean button = false; int ulx = 50, uly = 50, w = 100, h = 75; // rect variables … if (mouseX > ulx && mouseY > uly && mouseX < ulx + w && mouseY < uly + h && mousePressed) button = true; else button = false; // Later in code if (button == true) print(“I’ve been clicked!”); Do you like the way the tests are lined up on multiple lines? if(button) // Does the same thing as if (button == true) Learning Processing: Slides by Don Smith

Example 5.5: A Button as a switch Test where the mouse is in the mousePressed() method. Then set a boolean variable to true or false boolean button = false; int ulx = 50, uly = 50, w = 100, h = 75; // rect variables … void draw() { if (button) // actions if button pressed } void mousePressed() { if (mouseX > ulx && mouseY > uly && mouseX < ulx + w && mouseY < uly + h) if (button) // alternate on every click button = false; else button = true; Learning Processing: Slides by Don Smith

Example 5.5: A Button as a switch Test where the mouse is in the mousePressed() method. Then set a boolean variable to true or false boolean button = false; int ulx = 50, uly = 50, w = 100, h = 75; // rect variables … void draw() { if (button) // actions if button pressed } void mousePressed() { if (mouseX > ulx && mouseY > uly && mouseX < ulx + w && mouseY < uly + h) if (button) // alternate on every click button = false; else button = true; button = !button; // quite a bit shorter, eh? } Learning Processing: Slides by Don Smith

Exercise 5-8: Click to start a moving ‘ball’ // Declare a boolean, set to false int circleX = 0; int circleY = 100; void setup() { size(200,200); } void draw() { background(100); stroke(255); fill(0); ellipse(circleX, circleY, 50, 50); // Move the circle only after a click void mousePressed() { // react to the mouse being pressed // Location of the mouse doesn’t matter Use a boolean variable to represent if we are moving or not. Change the variable each time the mouse pressed. Learning Processing: Slides by Don Smith

Example 5-6: A bouncing ‘ball’ int circleX = 0; int speed = 1; void setup() { size(200,200); smooth(); } void draw() { background(255); circleX = circleX + speed; if ( circleX > width || circleX < 0) speed = speed * -1; // reverse course // Display the circle at x location stroke(0); fill(175); ellipse(circleX, 100, 32, 32); Use a variable speed which can be positive or negative. Change speed to negative if we bounce off the right edge. // What will this do? void mousePressed() { speed = speed + 1; } Learning Processing: Slides by Don Smith

A note about debugging… You can print the variable speed to see what it is doing. Use println(speed) if you want one number per line. int circleX = 0; int speed = 1; void setup() { size(200,200); smooth(); } void draw() { background(255); circleX = circleX + speed; if ( circleX > width || circleX < 0) speed = speed * -1; // reverse course // Display the circle at x location stroke(0); fill(175); ellipse(circleX, 100, 32, 32); void mousePressed() { speed = speed + 1; println(speed); } Learning Processing: Slides by Don Smith

Summary Conditionals allow you to control the flow if, else, else if allow many options Boolean expressions are used inside if(… ) tests Resolve to either true or false Boolean expressions use comparison operators: >, <, >=, <=, ==, != Boolean variables can be set to true or false, and also used in conditional expressions AND and OR (&& and ||) are used to combine conditionals You can use print() and println()to help debug your programs while you are testing them Learning Processing: Slides by Don Smith