Chapter 5 Conditionals Brief Notes Purpose of chapter

Chapter 5, Conditionals Brief Notes

Purpose of chapter: To run conditional operations to see how program produces different results depending on varying situations.

Boolean Expressions • Boolean – An expression that evaluates to either true or false. > Relational Operators for Booleans < >= <= Notice this one. == !

Syntax for Conditionals A single condition: Multiple conditions: if(Boolean test) { //code to run if true } if(Boolean test 1) { //code to run if true }else if (Boolean test 2) { //code to run if true Two conditions: if(Boolean test) { //code to run if true } else { //code to run if false } }else if (Boolean test 3) { //code to run if true else { //code to run none of //the above is true }

Quick Challenge: Use the starter code below to draw a circle on left side and rectangle on right side. /* Fill in the code to draw red circles on left half and green triangles on right */ void setup(){ size(400, 400) ; } void draw(){ no. Stroke(); } void mouse. Pressed(){ triangle(mouse. X, mouse. Y, mouse. X+20, mouse. Y+40, mouse. X-20, mouse. Y+40); ellipse(mouse. X, mouse. Y, 25) ; /* Purpose: draw red circles on left; and green triangles on right */ } Solution: void setup(){ size(400, 400) ; } void draw(){ no. Stroke(); } void mouse. Pressed(){ if (mouse. X > 200) { fill(0, 200, 0) ; triangle(mouse. X, mouse. Y, mouse. X+20, mouse. Y+40, mouse. X-20, mouse. Y+40); } else { fill(200, 0, 0) ; ellipse(mouse. X, mouse. Y, 25) ; } //end of if/else }

Reminder: This is what we did for our Multiple conditions exercise on Monday. (if/else) The scenario was a scoring system where there are four possible scores – from excellent to poor. Where: 90 and above = “Very Good” 70 -89 = “Good” 60 -69 = “Fair” 0 -59 = “Poor” The grades were provided from a random variable. Shown below. You used the println() function to display the result float score = random(40, 100);

Constrain function limits a value r = constrain(r, 0, 255) This is used on pages 72 -73 instead of a longer if/else statement. The int() function converts any value to its integer representation. Example 1: float grade = int( random(0, 100) ); Example 2: float price = 65. 00; int whole. Price = int(price);

Quick Challenge: - Increase/decrease blue if mouse at bottom half. - Also, use println() to show status. //This is mostly from example 5 -2 /* Quick challenge: Increase/decrease blue based on whether mouse at top or bottom */ float r = 0; float g = 0; float b = 0; void setup() { size(200, 200); } void draw() { background(r, g, b); stroke(0); line(width/2, 0, width/2, height); line(0, height/2, width, height/2); //right half if(mouse. X > width/2) { r = r +2; }else { r = r-2; } //press mouse for green if(mouse. Pressed) { g = g+2; }else{ g = g-2; } //keep all within limit r= constrain(r, 0, 255); g= constrain(g, 0, 255); b= constrain(b, 0, 255); }//end of draw

Logical Operators AND is && OR is || NOT is !

Simple examples of AND and NOT Both conditions must be met in order to draw rectangle. void draw() { background(0); if (mouse. X > width/2 && mouse. Y > height/2) { fill(#ffff 00); rect( 10, 10, 10); } } Draw a “button shape” if mouse is pressed. If it’s not pressed a circle is drawn. void draw() { rect. Mode(CENTER); background(120); if(!mouse. Pressed) { ellipse(50, 50, 50); }else { rect(50, 50, 15); } }

Controlling buttons On your own, examine the sketches from pages 77 -83. They are examples of controlling buttons by hovering, pressing, or clicking.

Exercise 5 -9, pg. 86 These are possibilities for this exercise. //exercise 5_9 possibilities int x= 0; int speed= 3; //sped it up a bit int y = 0; float r = 255; float g = 0; float size = 75; void setup() { size(200, 200); } void draw() { background(0); no. Stroke(); y = y + speed; x = x + speed; // to keep colors r&g between 0 & 255 g = g + 1; r = r - 1; if (g >255 ) { g = 0; } if (r < 0 ) { r =255; } //if it reaches either edge, turn around by changing polarity if ((x > width) || (x < 0) || (y>height) || (y < 0) ){ speed = speed * -1; } //size change using some crazy association if (x < width/2) { size = size - 2; }else{ size = size + 2; } //display circle //stroke(#00 aa 00); fill(r, g, 0 ); ellipse(x, y, size); }

Due to time constraints, this assignment will be set on the next calendar for early October. You’ll have 10 minutes at the end of a class. TBD. Assignment: Do Exercise 5 -1, on page 70. Guide: Use this algorithm for the Exercise. If 90 and above, grade is A If 80 and above, grade is B If 70 and above, grade is C If 60 and above, grade is D otherwise, grade is F Optional: Set random to 40 -100 so that F comes up less frequently. To submit: zip the folder and upload to a dropbox called “Exercise 5 -1, Grades”
- Slides: 13