A conditional statement (sometimes called a selection statement) represents a choice. In Java, the most general conditional statement is an if statement, which tells Java to execute a block of code only if a Boolean condition evaluates to true. If the condition is false, the if statement is exited and the next statement after it is executed.
The form of an if statement in Java is as follows:
if (condition) {
// block of code to be executed if the condition is true
}
//next statement.
Here is an example of the use of an if statement.
String status = "adult";
if (age < 18) {
status = "child";
}
System.out.println(status);
If age has the value 20, the Boolean condition, age < 18, evaluates to false and all the code between the braces { and } is ignored, then the next statement after the if statement is executed, which prints "adult" .
If age has the value 10, the Boolean condition age < 10 evaluates to true and the code between the braces is executed, assigning the String object "child" to status. Once all the code within the braces has been executed, the next statement after the if-statement is executed, which prints "child".
An if statement may have an else block. This tells Java to execute one block of code if the Boolean condition evaluates to true and a different block of code if the Boolean condition evaluates to false. Once the appropriate block of code has been executed, the if statement is exited and the next statement after it is executed.
The form of an if statement with an else block in Java is as follows:
if (condition) {
// block of code to be executed if the condition is true
}
else {
// block of code to be executed if the condition is false
}
//next statement
And here is an example:
if ((age >= 18) && (age < 65)) {
System.out.println("full price");
}
else {
System.out.println("concession");
}
If an if statement has an else block, only one of the blocks is ever executed.
There are two other ways of writing Java conditional statements you will learn about in M250, using the conditional operator (sometimes called the ternary operator) and the switch statement.
The syntax of an if statement in Java is different to that in Python, but the way in which it controls the flow of the execution of the program is the same. The main difference between Java and Python is that Java does not have an equivalent of the elif keyword that Python uses for nested if-else structures. If there are several mutually exclusive options in a Java program, a series of if statements with else blocks must be used. Here is an example in Java, followed by equivalent code in Python:
if (age >= 65) {
System.out.println("elder");
}
else if (age >= 18){
System.out.println("adult");
}
else {
System.out.println("child");
}
Notice that no condition is needed for the final else because the code only reaches this point if neither of the other branches has been executed, which means that age can only be less than 18.
if age >= 65:
print('elder')
elif age >= 18:
print('adult')
else:
print('child')