3.3 If Else Statements

Purpose of Else Statements

Else statements: Handles what happens when the if condition is false. Structure of If-Else:

  • If statement with a condition.
  • Else statement without a condition.
  • Both parts have code blocks surrounded by {}.

  • if there is only one instruction in the if statement technically brackets aren’t mandatory, but its good to practice using them because generally there is more in the statements.
// example of a simple if else loop: 
// things to keep in mind: 
// Always put a semicolon after a parenthesis 
// make sure you use double quotes are your message 

int number = 2; // change this number to see what happens when its odd

if (number % 2 == 0) {
    System.out.println("Number is even");
} else {
    System.out.println("Number is odd");
}
// slightly more complex loop because there are 2 messages 

int myAge = 17; // adjust the age too see the difference in what it prints out

System.out.println("Current age: " + myAge);

if (myAge >= 16) {
    System.out.println("You can learn to drive a car.");
    System.out.println("Drive safe!");
} else {
    System.out.println("You are not old enough for a license yet.");
    System.out.println("Try again next year");
}

popcorn hack 1

image

  1. Based on this code, if you were younger than 16 what would it print out?
  2. Write your own if else statement
// popcorn hack 2 
// Figure out what is wrong with this code and fix the error. (2 errors)

int number = 2; 

if (number % 2 == 0) {
    System.out.println('Number is even');
} else {
    System.out.println("Number is odd")
}