Conditional Statements

Boolean Expressions

A Boolean expression evaluates to true or false.

int x = 5;
boolean test = (x == 5);
System.out.printf("x is equal to 5: %b\n", test);

int x = 1;
int y = 2;
boolean test2 = (x == y);
System.out.printf("x is equal to y: %b\n", test2);

Relational Operators

  • ==  (equality)
  • !=  (inequality)
  • >
  • <
  • >=
  • <=

Conditional Assignment

int y = (x == 0) ? 0 : 1;
String winner = (scoreA > scoreB) ? "Anne" : "Brie";

Boolean Logical Operators

  • &
  • |
  • ^
  • !

If Statements

if (expr) { 
    ... 
}
if (expr) { 
    ... 
} else { 
    ... 
}
if (expr1) { 
    ... 
} else if (expr2) { 
    ... 
}
if (expr1) { 
    ... 
} else if (expr2) {
    ...
} else { 
    ... 
}

Nested if Statements

if (expr) {
    if (expr) { 
        ....
    }
}

© 2017, Eric. All rights reserved.