> Java Rules 2

· In this document

This document adds onto the original Java rules document with the inclusion of conditional execution. This adds two new statement types to the language. Because the if statement requires boolean expressions we also cover those here.

· Conditional statements

→ IfStatement

if(booleanExpression) statement1 [else statement2]

This is the most basic conditional statement.  If the boolean expression is true, statement1 will execute.  If it is false, either nothing happens or statement2 is executed where the else clause is used.  A common usage might look like the following:

if(a==5) { b=25; }

The following will always set b to 100 by the time b is printed.  Trivial, yes, but it shows the else in action.

public void printOneHundred(int a) {
   int b;
   if(a==10) {
      b = a*a;
   }
   else {
      b = 100;
   }
   System.out.println(b);
}

→ SwitchStatement

switch(intExpression) { case 1: [statements] break; case 2: [statements] break; case 3: [statements] break; ... default: [statements] break; }

The switch statement allows you to branch to one of many options based on the value of an integer expression (can be int or char).  The values don't have to start at 1 as I have shown here.  They can be whatever you want.  'break' statements are also optional, but they are almost always needed.  Break statements can also be used in loops to terminate loops, but I won't ever do that.  The last line, default, happens if none of the other cases are matched.  in this way, it's exactly like else.

public void grader(char grade) {
   switch(grade) {
   case 'A':
      System.out.println("Scored 90% or higher!");
      break;
   case 'B':
      System.out.println("Scored between 89% and 80%.");
      break;
   case 'C':
      System.out.println("Scored between 79% and 70%.");
      break;
   case 'D':
      System.out.println("Scored between 69% and 65%.");
      break;
   default:
      System.out.println("Scored lower than 65%.");
      break;
   }
}

→ Conditional expression operators

SymbolNameDescriptionExample codeExample result
==ComparisonCompares two primitives of the same type.
Returns true if the primitives are equal, false otherwise.
int a=3;
int b=2;
if(a==b) { return true; }
else { return false; }
false
>Greater thanCompares two primitives of the same type.
Returns true if the first is greater than the second.
int a=3;
int b=2;
if(a>b) { return true; }
else { return false; }
true
<Less thanCompares two primitives of the same type.
Returns true if the first is less than the second.
int a=3;
int b=2;
if(a<b) { return true; }
else { return false; }
false
>=Greater than or equal toCompares two primitives of the same type.
Returns true if the first is greater than or equal to the second.
int a=10;
int b=10;
if(a>=b) { return true; }
else { return false; }
true
<=Less than or equal toCompares two primitives of the same type.
Returns true if the first is less than or equal to the second.
int a=10;
int b=10;
if(a<=b) { return true; }
else { return false; }
true
!=InequalityCompares two primitives of the same type.
Returns true if the first is not equal to the second.
int a=3;
int b=2;
if(a!=b) { return true; }
else { return false; }
true
||Boolean ORCompares two expressions.
If either is true, the whole statement returns true.
boolean lie = false;
boolean truth = true;
if(lie||truth) { return true; }
else { return false; }
true
&&Boolean ANDCompares two expressions.
If either is false, the whole statement returns false.
boolean lie = false;
boolean truth = true;
if(lie&&truth) { return true; }
else { return false; }
false
!Negation
(Boolean NOT)
Returns the opposite boolean value of a given expression. boolean lie = false;
boolean truth = !lie;
truth = true
x?y:zTernary
(Simple conditional)
Evaluates 'x'.
If 'x' is true, then 'y' executes.  Otherwise 'z' executes.
boolean lie = false;
boolean truth = true;
(lie&&truth)?true:false;
false
a instanceof cInstance ofEvaluates as true if object-type variable a
is the same type as class c.
Evaluates false otherwise.
Dot d = new Dot();
if(d instanceof Actor) {
  // Print "Dots are Actors!"
}
else { // Print "Not actors!" }
"Dots are Actors!"

· Examples

public class Dot extends Actor {
   public Dot() {
      setImage("blue_dot.png");
   }
   public Dot(int color) {
      if(color == 1) {
         setImage("blue_dot.png");
      }
      else if(color == 2) {
         setImage("green_dot.png");
      }
      else {
         setImage("red_dot.png");
      }
   }

   private Dot d1 = new Dot(); // makes a blue dot.
   private Dot d2 = new Dot(2); // makes a green dot.
   private Dot d3 = new Dot(47); // makes a red dot.
}