Chapter 5, Conditionals Brief Notes
Relational Operators for Booleans 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 test1) { //code to run if true }else if (Boolean test2) { }else if (Boolean test3) { else { //code to run none of //the above is true } Two conditions: if(Boolean test) { //code to run if true } else { //code to run if false }
Algorithm for Exercise 5-1 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
Constrain functions limits a value r = constrain(r, 0, 255) This is used on pages 72-73 instead of two if statements.
Both conditions must be met in order to draw rectangle void draw() { background(0); if (mouseX > width/2 && mouseY > height/2) { fill(#ffff00); rect( 10,10,10,10); } Draw a different shape if mouse is pressed. If it’s not pressed an ellipse is drawn. void draw() { rectMode(CENTER); background(120); if(!mousePressed) { ellipse(50,50,50,50); }else { rect(50,50,50,50); }
Controlling buttons We will examine a couple of sketched from pages 77-83. They are examples of controlling buttons by hovering, pressing, or clicking.
These are possibilities for this exercise. 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); noStroke(); 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(#00aa00); fill(r, g, 0 ); ellipse(x, y, size, size);