/////////////////////// // If Conditionals ///////////////////// /* Block Structure Boolean Math SPECIAL: Mouse positions! ==> is just mouseX, mouseY Conditions is general: true or false values if ( condition1 ) { } else if ( money > 1000 ) { println("buy red car") } else if ( money > 5 ) { println("solve world hunger") } else { } The first condition the evaluate to TRUE will decide which bock of code gets run Even if multiple conditions are TRUE, the first one is the only one that will execute. ===================== == Boolean Math ===================== True of False ==> T or F 12 > 5 = T 3 + 4 = 7 Boolean Operators: AND, OR, NOT && || ! AND if both TRUE, then result is TRUE, otherwise FALSE OR if either is TRUE, then reult is TRUE, otherwise FALSE NOT makes opposite. NOT ( TRUE ) = FALSE etc Comparisons > < >= <= != == Examples 12 < 100 Common Code usage: mouseX == 300, mouseY != 50 = TRUE 50 != 80 = TRUE 80 == 40 = FALSE Example for Operators T && F = F AND: both must be true to get true, otherwise just false F && F 0 x 0 = F = 0 T && T 1 x 1 = T = 1 T && F 1 x 0 = F = 0 F && T || F 0 x 1 + 0 = F || F = F !(F && T) || (F || T) && T = ( F ) || (F || T) && T = F || T && T = F || T = T */ int bx = 100; int by = 200; int bw = 180; int bh = 30; void setup(){ size(500,500); } void draw(){ //Compares the x coordinate of the mouse with different regions on the screen /*if ( mouseX > width / 2) fill(255,0,0); else if (mouseX < 100) fill(0,255,0); else fill(0,0,255); rect(0,0,width, height); */ //Detects collision of mouse with rectangular area if(mouseX > bx && mouseX < bx + bw && mouseY > by && mouseY < by + bh) fill(0,255,0); else fill(255,0,0); rect(bx,by, bw,bh); } void mousePressed(){ if(mouseX > bx && mouseX < bx + bw && mouseY > by && mouseY < by + bh) println("boo!"); }