1 of 9

Processing

IF STATEMENTS

2 of 9

Preview variable

  • int catLives = 9;
  • float accountBalance = -123.45;
  • String message = "Happy Coding!";

3 of 9

Boolean Values

  • The new boolean type can only hold two values: true or false.
  • boolean isCodingFun = true;

4 of 9

Relational Operators

  • To check for inequalities (less than, greater than, equal to)
  • float score = 95;
  • boolean isGradeA = score >= 90;

  • What’s the result ? (A : isGradeA = true)

5 of 9

Boolean Operators

  • boolean canSwim = true;
  • boolean canFly = true;

  • boolean isDuck = canSwim && canFly;
  • boolean isFishOrBird = canSwim || canFly;
  • boolean sinks = !canSwim;
  • boolean falls = !canFly;

  • Combining operators
  • boolean isMammal = !canSwim && !canFly;

6 of 9

If Statements

  • float score = 95;
  • boolean isGradeA = score >= 90;
  • if(isGradeA){
  • background(0, 255, 0);
  • fill(0);
  • text("Congratulations!", 7, 50);
  • }

  • Since score is 95 (and 95 is greater than 90), then we get the congratulations message:

  • If isGradeA is not true (in other words, if it’s false (in other other words, if score is less than 90)), then the program doesn’t do anything. Try changing score to 85, and you’ll see a blank window:

7 of 9

Else Statements

  • float score = 85;

  • if(score >= 90){
  • background(0, 255, 0);
  • fill(0);
  • text("Congratulations!", 7, 50);
  • }
  • else{
  • background(255, 0, 0);
  • fill(0);
  • text("Study more!", 15, 50);
  • }

8 of 9

Else-If Statements

  • float score = 85;

  • if(score >= 90){
  • background(0, 255, 0);
  • fill(0);
  • text("Congratulations!", 7, 50);
  • }
  • else if(score >= 80){
  • background(0, 0, 255);
  • fill(255);
  • text("Good job!", 18, 50);
  • }

9 of 9

If Else-If Else Combinations

  1. float score = 75;

  • if(score >= 90){
  • background(0, 255, 0);
  • fill(0);
  • text("Congratulations!", 7, 50);
  • }
  • else if(score >= 80){
  • background(0, 0, 255);
  • fill(255);
  • text("Good job!", 18, 50);
  • }
  • else if(score >= 70){
  • background(255, 255, 0);
  • fill(0);
  • text("Just okay.", 16, 50);
  • }
  • else if(score >= 60){
  • background(255, 128, 0);
  • fill(0);
  • text("Not good!", 18, 50);
  • }
  • else{
  • background(255, 0, 0);
  • fill(0);
  • text("Study more!", 15, 50);
  • }