1. If statements, Else, Else If
If statements
First example
If statements are best introduced by example:
public class MyProgram { public static void main(String[] args) { int speedMph = 150; if( speedMph > 55 ) { System.out.println("You are speeding"); } //An if statement is of the form //if(BOOLEAN) { // //code that runs if BOOLEAN is true //} } }
In the previous example, if speedMph is greater than 55, then the program will output "You are speeding", and otherwise it will output nothing
Specifically, an if statement is of the form if(CONDITION){ STATEMENTS }
. If the condition, which must be a boolean, is true, then the statements are run.
If else
The previous if statement can be extended to include an "else" clause, which will run when the condition is false:
public class MyProgram { public static void main(String[] args) { int speedMph = 150; if( speedMph > 55 ) { System.out.println("You are speeding"); } else { System.out.println("The bear eats you"); } //if(BOOLEAN){ STATEMENTS } else { STATEMENTS } } }
This else section is optional. It could be replaced by another if statement that checks the inverse of the condition, but an else is usually a more elegant solution.
If,else if,else if, else if,else
Rather than an else, we can also have else ifs after the first if. These will check secondary conditions. Only one of the possibilities ("branches") will be run. If no conditions are true then an else (assuming it exists) will be run:
public class MyProgram { public static void main(String[] args) { int speedMph = 150; if( speedMph > 55 ) { System.out.println("You are speeding"); } else if( speedMph < 0) { System.out.println("Are you going faster than the speed of light?!"); } else if( speedMph > 30 ) { System.out.println("Good job citizen"); } else { System.out.println("The bear eats you"); } } }
If this first if statement is true, then it will output "You are speeding" and end the if statement. Otherwise, the if statement will check whether speedMph is less than 0, possibly ending there. We can have an arbitrarily large number of branches (hundreds of else ifs is possible).
A note about scope
If you create a variable within an if statement's curly brackets, it ceases to exist after the close curly. In fact, this will apply to many things in Java that use curly brackets:
public class MyProgram { public static void main(String[] args) { if(true) { int abc = 42; System.out.println(abc); //This compiles } System.out.println(abc); //This is an error //We can no longer use abc here, since the "scope" around abc has ended. //If we wanted to use abc here, we should have declared it above the if statement } }
We will discuss scope more in a later section.
Inline If statement
In Java, you can omit the curly brackets for an if statement when there is only one statement:
public class MyProgram { public static void main(String[] args) { if(5 > 3) { System.out.println("Example 1 is true"); } if(5 > 3) System.out.println("This will also run"); } }
Note the lack of curly brackets in the second if. You can even put a new line between the close parenthesis and the println. However be very careful when dropping curly brackets. This is a common error:
public class MyProgram { public static void main(String[] args) { int d = 0; if( d == 5 ) System.out.println("This will never run if d is 0"); System.out.println("This will always run even though it is indented"); } }
Because Java ignores whitespace, this is the same as:
public class MyProgram { public static void main(String[] args) { int d = 0; if( d == 5 ) { System.out.println("This will never run if d is 0"); } System.out.println("This will always run even though it is indented"); } }