Decision structures: if, if-else, and switch

IF syntax

There are 4 ways of writing if statements

DescriptionSyntaxExample
if code block
This is the normal way of writing it
If the condition is true,
then do all of the statements in the code block.
if (condition) {
   statement;
   statement;
   ...
}
if (ball.y < 0) {
   xspeed +=5;
   yspeed = -yspeed;
}
one line
if (condition) statement;
if (x > 5) gameOver = true;
if-else
if (condition) {
   statement;
   statement;
   ...
} else {
   statement; 
   ...
}
if (ball.y < 0) {
   xspeed +=5;
   yspeed = -yspeed;
} else {
   ...
}
if-elseif-else
if (condition) {
   statement;
   ...
} else if (condition) {
   statement; 
   ...
} else {
   statement;
}
if (answer.equals("Yes") {
   food +=5;
} else if (answer.equals("No")) {
   //do nothing;
} else {
   //invalid answer
}

Errors with IF (and other control statements)

This common error will change how your program is working and you may not notice why. It can happen to if, for, and while.

This is what we want to have happen: you press the red button , then a message is sent out, the computer sleeps for 60 seconds, then selfdestructs.

if (redButtonPressed) {
    System.out.println("Self destruct activated. T-60 seconds.");
    Timer.sleep(60);	//this actually won't work
    selfDestruct();
}

This is the screwed up version. Can you find the error?

if (redButtonPressed); {
    System.out.println("Self destruct activated. T-60 seconds.");
    Timer.sleep(60);
    selfDestruct();
}

Note the ; after the () in the IF.
This makes the code equivalent to this:

if (redButtonPressed); 
{
    System.out.println("Self destruct activated. T-60 seconds.");
    Timer.sleep(60);
    selfDestruct();
}

which is the same as this

if (redButtonPressed); // do nothing

System.out.println("Self destruct activated. T-60 seconds.");
Timer.sleep(60);
selfDestruct();

What this does is (i) nothing happens if the redbutton is pressed.
(ii) No matter what happens, the selfDestruct code will ALWAYS be executed -- EVEN if you did not press the button.

Logic and IF statements.

An AND condition can be written as two nested IF statements (demonstrate)

An OR condition can be written as two successive IF statements (demonstrate)

When thinking about an ELSE statement, you need to consider how many possible options there are to your condition and if they are mutually exclusive or not.
example: if your mark is > 50, print pass, if > 75 print good, if > 85 print excellent.

An Elseif can always be written as a nested if.

Else-IF versionNested If
if (n > 0) {
   ...   
} elseif (n < 0) {
   ...
} else {
   ...
}
if (n > 0) {
   ...
} else {
   if (n < 0) {
      ...
   } else {
      ...
   }
}
These two are identical

But an elseif can only be written as three separate if statements if the statements are mutually exclusive. IF they are not mutually exclusive, then the order of the conditions is important.

Switch

This is an alternative to IF-ELSEIF-ELSE or a series of independant IF statements

Pros:
Much easier to read,
Fewer logic errors since you can only use one variable (this is the key!)

Cons:
You can only use one variable, so it can't be used for everything
The test conditions are a lot more limited. (I think you can only test if the variable is equal to a literal)
Switch statments can only be used on primitive data types or Strings (Java 7 onwards)

Example: You're playing an adventure game and get input. You split the input up into words and the first word is a verb, the second word is a noun - eg. take lamp, shoot gun, etc.

Example Notes
switch (word1) {
   case "inventory":
      displayInventory();
      break;
   case "take":
   case "pickup":
      getObject(word2);
      break;
   case "fire":
   case "shoot":
      fireObject(word2);
      break;
   case "n": 
   case "s":
   case "e":
   case "w":
      move(word1);
      break;
   default:
      message("invalid command");
}
You can only use one variable. In this case: word1
This replaces this IF structure:
if (word1 == "inventory") displayInventory();
if (word1 == "take") getObject(word2);
if (word1 == "pickup") getObject(word2);
... etc ...
You can handle OR statements with SWITCH:
if (word1 == "take" || word1 == "pickup") getObject(word2);
becomes
case "take":
case "pickup":
   getObject(word2);
   break;
There is no way to handle AND statements.
As seen above, more than one match can go to a certain result;
If you don't use break; it will just continue down through the list
The default: case is for when nothing else matches. Very handy!

Make sure you know: