Java Selection Statements: if statements
The if statement is the most simple decision making statement in Java. It works by executing a statement or block of statements if, and only if, a condition is true. The general syntax is:
if(expression) {
// statement(s) to execute if
// condition is true
//also known as the true condition
}
Once the expression has been evaluated it will be either true or false. If it is true then the statement(s) within the code block (the areas contained within the curly brackets or braces { } ) will be executed. If the expression is false then the code block will be ignored and Java will skip to the code blocks closing curly bracket and continue executing the remainder of the program.
Let’s have a look at a simple if statement that will output a message if the value of a variable exceed a certain size:
int cashValue = 50; if(cashValue > 20) { System.out.println("Sorry, that amount is too large."); }
When we run this code, the expression is evaluated as true since the variable cashValue is larger that 20 which will allow the statements within the code block to execute and we should get this output:
run: Sorry, that amount is too large. BUILD SUCCESSFUL (total time: 0 seconds)
There is no limit to the number of statements you can have within an if statements code block. Let’s have a look at another if statement that executes a number of statements within its code block:
int studentGrade = 49; if(studentGrade <= 50){ System.out.println("The students grade is: " + studentGrade); System.out.println("The student did not achieve a passing grade."); System.out.println("A resit is required."); }
When we run this code, the expression is evaluated as true since the variable cashValue is larger that 20 which will allow the statements within the code block to execute and we should get this output:
run: The students grade is: 49 The student did not achieve a passing grade. A resit is required. BUILD SUCCESSFUL (total time: 0 seconds)
As you can, all statements within the code block have been executed.
if statement without curly brackets {}
It is not mandatory to have curly braces when using an if statement. If curly braces are not provided, Java will consider the very next statement AND ONLY THAT STATEMENT (immediately after the expression to be evaluated) to be inside its code block (even though visually there appears to be no code block with the absence of curly braces). The general syntax for this scenario is:
if(condition)
statement1; //will execute if the condition is true
statement2; //will ALWAYS execute, it is outside the if statement
Here, statement1 will only be executed if the condition is true, but statement2 and any subsequent statements will always be executed since they are not within the if statements code block. let’s see that in action:
int maxStudents = 20; int studentNumber = 30; if(studentNumber >= maxStudents) System.out.println("There are too many students."); System.out.println("This is the second message."); System.out.println("This is the third message.");
Since the condition is true, Java printed out the first statement after the condition as well as all subsequent statements:
run: There are too many students. This is the second message. This is the third message. BUILD SUCCESSFUL (total time: 0 seconds)
An if statement without a set of curly brackets is not common but worth pointing out that it is possible so if you come across this technique you will know how it works.
Multiple if Statements
Your program is not limited to a single if statement. You can insert as many if statements as you like however care needs to be taken to avoid unnecessary or over complicated code, and as you explore . Let’s have a look at how multiple if statements might be useful in a program.
int x = 4; if (x < 5) { System.out.println("X is less than 5"); } if (x > 5) { System.out.println("X is greater than 5"); } if (x == 5) { System.out.println("X is actually 5"); }
Here, we have a number of if statements that test that value of x. In this example there are three options: less than 5, greater than 5 or equal to 5. For x = 4 this would be the output:
run: X is less than 5 BUILD SUCCESSFUL (total time: 0 seconds)
Try this yourself and change the value of x to see if you get an expected result.
Multiple if statements can be useful but there may be a more efficient way to solve a problem and we will explore more options for if statements later.