A Conditional: The “if” Statement#
Sometimes we don’t want the execution to flow simply from top to bottom. A conditional is used when we want the computer to determine whether to do a statement or not. An if
statement is one kind of conditional in Java.
Syntax of if Statement#
The syntax of the if
statement is:
if (<Boolean expression>) {
\(\;\;\;\;\;\) <statements to do, i.e. the body>
}
The brackets {}
are not technically required, if there is only a single statement to do. However, more often, we will have multiple statements so it is a good habit to use the brackets.
Semantics of if Statement#
The statements in the body are done only if the Boolean expression is true. If the Boolean expression is false, execution skips the body and starts after the closing bracket.
Sometimes we want to do something when a condition is true and something else when a condition is false. This is done with Java’s else clause, which can be part of any if statement. Here is that syntax:
if (<Boolean expression>) {
\(\;\;\;\;\;\) <statements to do, i.e. the body>
}
else {
\(\;\;\;\;\;\) <statements to do, i.e. the body>
}
When we start building more complex conditional statements, it is especially important to use the brackets even for bodies with a single statement. Here is an example to prove that point (this is called dangling-else confusion):
int z = 18;
if (z > 10)
if (z > 30)
System.out.println("Here 1");
else
System.out.println("Here 2");
System.out.println("Here 3");
This could be intended to mean either one of the following, properly bracketed and indented versions, which each produce different output:
int z = 18;
if (z > 10){
if (z > 30){
System.out.println("Here 1");
}
}
else{
System.out.println("Here 2");
}
System.out.println("Here 3");
Here 3
2.
int z = 18;
if (z > 10){
if (z > 30){
System.out.println("Here 1");
}
else{
System.out.println("Here 2");
}
}
System.out.println("Here 3");
Here 2
Here 3
Unambiguous
All computer instructions do one and only one thing.
As you will hopefully recall, instructions for a computer must be unambiguous. This means that even if the programmer does not properly indent or bracket, there is a rule and that rule states a dangling else must be attached to the last open if
. Thus, without bracketing, the computer will produce the results of printing both Here 2
and Here 3
, shown in 2, because the else
would become part of the if (z > 30)
control structure.
Small Change or Big Change?
A small change in the syntax of a program can be a big change in the meaning of the program.
Practice Questions#
Which of the following are Boolean expressions?
a.
5 + 6
b.
true
c.
x < 5
d.
payRate >= 48.63
e.
x = 5
f.
month == 6 && payRate >= 48.6
g.
x == 5
h.
false
i.
name[1] == 'C' ‖ name[1] == 'D'
Trace the following programs, showing the execution path as a sequence of line numbers and providing a memory diagram and the output:
a.1. int[] n = {10, 11, 15, 13}; 2. if n[2] > n[3]{ 3. System.out.println(n[0]); 4. } 5. System.out.println(n[1]);
b.
1. int[] n = {10, 11, 15, 13}; 2. if n[2] > n[3]{ 3. System.out.println(n[0]); 4. } 5. else{ 6. System.out.println(n[1]); 7. }