Forgetting necessary braces, ending an if statement in the wrong place, mistaking == for =, and dangling else clauses are common errors in selection statements. Duplicated statements in if-else statements and testing equality of double values are common pitfalls.
Common Error 1: Forgetting Necessary Braces
The braces can be omitted if the block contains a single statement. However, forgetting the braces when they are needed for grouping multiple statements is a common programming error. If you modify the code by adding new statements in an if statement without braces, you will have to insert the braces. For example, the following code in (a) is wrong. It should be written with braces to group multiple statements, as shown in (b).
Common Error 2: Wrong Semicolon at the if Line
Adding a semicolon at the end of an if line, as shown in (a) below, is a common mistake. This mistake is hard to find, because it is neither a compile error nor a runtime error; it is a logic error. The code in (a) is equivalent to that in (b) with an empty block. This error often occurs when you use the next-line block style. Using the end-of-line block style can help prevent this error.
Common Error 3: Redundant Testing of Boolean Values
To test whether a boolean variable is true or false in a test condition, it is redundant to use the equality testing operator like the code in (a):
Instead, it is better to test the boolean variable directly, as shown in (b). Another good reason for doing this is to avoid errors that are difficult to detect. Using the = operator instead of the == operator to compare the equality of two items in a test condition is a common error. It could lead to the following erroneous statement:
if (even = true)
System.out.println("It is even.");
This statement does not have compile errors. It assigns true to even, so that even is always true.
Common Error 4: Dangling else Ambiguity
The code in (a) below has two if clauses and one else clause. Which if clause is matched by the else clause? The indentation indicates that the else clause matches the first if clause. However, the else clause actually matches the second if clause. This situation is known as the dangling else ambiguity. The else clause always matches the most recent unmatched if clause in the same block. So, the statement in (a) is equivalent to the code in (b).
Since (i > j) is false, nothing is displayed from the statements in (a) and (b). To force the else clause to match the first if clause, you must add a pair of braces:
int i = 1, j = 2, k = 3;
if (i > j) {
if (i > k)
System.out.println("A");
}
else
System.out.println("B");
This statement displays B.
Common Error 5: Equality Test of Two Floating-Point Values
Floating-point numbers have a limited precision and calculations; involving floating-point numbers can introduce round-off errors. So, equality test of two floating-point values is not reliable. For example, you expect the following code to display true, but surprisingly it displays false.
double x = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1;
System.out.println(x == 0.5);
Here, x is not exactly 0.5, but is 0.5000000000000001. You cannot reliably test equality of two floating-point values. However, you can compare whether they are close enough by testing whether the difference of the two numbers is less than some threshold. That is, two numbers x and y are very close if |x−y| < e for a very small value, e. e, a Greek letter pronounced epsilon, is commonly used to denote a very small value. Normally, you set e to 10^-14 for comparing two values of the double type and to 10^-7 for comparing two values of the float type. For example, the following code
final double EPSILON = 1E-14;
double x = 1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1;
if (Math.abs(x - 0.5) < EPSILON)
System.out.println(x + " is approximately 0.5");
will display that
0.5000000000000001 is approximately 0.5
The Math.abs(a) method can be used to return the absolute value of a.
Common Pitfall 1: Simplifying Boolean Variable Assignment
Often, new programmers write the code that assigns a test condition to a boolean variable like the code in (a):
This is not an error, but it should be better written as shown in (b).
Common Pitfall 2: Avoiding Duplicate Code in Different Cases
Often, new programmers write the duplicate code in different cases that should be combined in one place. For example, the highlighted code in the following statement is duplicated.
if (inState) {
tuition = 5000;
System.out.println("The tuition is " + tuition);
}
else {
tuition = 15000;
System.out.println("The tuition is " + tuition);
}
This is not an error, but it should be better written as follows:
if (inState) {
tuition = 5000;
}
else {
tuition = 15000;
}
System.out.println("The tuition is " + tuition);
The new code removes the duplication and makes the code easy to maintain, because you only need to change in one place if the print statement is modified.
Top comments (0)