Review:
the syntax rule for
the statement inside
then-part
and
else-part
in the if-statement
-
Syntax of the
two-way
if-statement:
if ( boolean-expression )
then-statement ; // statement executed if boolean-expr is true
else
else-statement ; // statement executed if boolean-expr is false
or:
if ( boolean-expression )
{ // then-part
then-statement1; // statements executed if boolean-expr is true
...
}
else
{ // else-part
else-statement1; // statements executed if boolean-expr is false
...
}
|
- If there is
no braces
(
{ ... })
after
the
keywords
if and
else, then:
- The
Java compiler will
assume there is
one statement in the
then-part
or
else-part
|
|
Common mistake 1:
omitting the braces
{
...
}
in the
if-statement
Because radius = -20,
both statements in the
then-part will be
skipped
Common mistake 1:
omitting the braces
{
...
}
in the
if-statement
DEMO:
demo/03-selections/04-common-mistakes/CommonMistake1.java
(step with
BlueJ)
Common mistake 2:
having a
bogus ";"
after the
if-condition
- Background information: the
"empty" statement
- The
"empty" statement
(i.e.: no
statement) is
written as
follows:
|
- The statement in
the then-part
(or else-part)
can be an
"empty" statement:
Or written
as follows:
|
Common mistake 2:
having a
bogus ";"
after the
if-condition
-
Correct program:
radius = -20;
if ( radius >= 0 )
{
area = 3.14159 * radius * radius; // Both statements are
System.out.println(area); // inside the THEN part
}
|
-
Incorrect program:
radius = -20;
if ( radius >= 0 ) ;
{
area = 3.14159 * radius * radius; // Both statements are
System.out.println(area); // outside the THEN part
}
|
|
DEMO:
demo/03-selections/04-common-mistakes/CommonMistake2.java
(step with
BlueJ)
Common mistake 3:
the dangling-else
ambiguity
DEMO:
demo/03-selections/04-common-mistakes/CommonMistake3.java
(step with
BlueJ)
Common mistake 3:
the dangling-else
ambiguity
How to fix
the dangling-else
ambiguity
- What if
we
want
the else keyword
to
pair up to the
1st
if
keyword:
double radius = -20;
if ( radius >= 0 )
if ( radius < 10 )
System.out.println( 3.14159*radius*radius);
else
System.out.println("Illegal radius");
|
-
Solution:
use
braces
to
force the
association
(i.e.: use
syntax rules)
double radius = -20;
if ( radius >= 0 )
{
if ( radius < 10 )
System.out.println( 3.14159*radius*radius);
}
else
System.out.println("Illegal radius");
|
|
Common mistake 4:
testing floating point value
for equality
- Background information:
-
Calculations with
double and
float can have
very small
(rounding) errors
|
Example:
public class ShowFloatErr
{
public static void main(String[] args)
{
double x, y;
x = 2.2 + 2.2 + 2.2 + 2.2 + 2.2 +
2.2 + 2.2 + 2.2 + 2.2 + 2.2;
y = 22.0;
System.out.println(x); // Prints: 21.999999999999996
System.out.println(y); // Prints: 22.0
}
}
|
- The computation
(rounding) errors will
cause the
== and
!= test
to
fail
|
Common mistake 4:
testing floating point value
for equality
-
Wrong way to
test
x == y:
public class TestFloatEq1
{
public static void main(String[] args)
{
double x, y;
x = 2.2 + 2.2 + 2.2 + 2.2 + 2.2 +
2.2 + 2.2 + 2.2 + 2.2 + 2.2; // x = 21.999999999999996
y = 22.0; // y = 22.0
if ( x == y )
System.out.println("x is equal to y");
else
System.out.println("x is NOT equal to y");
}
}
|
-
Program will
print
"x is NOT equal to y"...
|
DEMO:
demo/03-selections/04-common-mistakes/TestFloatEq1.java
Common mistake 4:
testing floating point value
for equality
-
Correct way to
test
x == y:
public class TestFloatEq1
{
public static void main(String[] args)
{
double x, y;
x = 2.2 + 2.2 + 2.2 + 2.2 + 2.2 +
2.2 + 2.2 + 2.2 + 2.2 + 2.2; // x = 21.999999999999996
y = 22.0; // y = 22.0
if ( Math.abs(x - y) < 0.0000001 )
System.out.println("x is equal to y");
else
System.out.println("x is NOT equal to y");
}
}
|
- The method
Math.abs( )
(in Java's
Math class)
returns the
absolute value of
its input.
|
DEMO:
demo/03-selections/04-common-mistakes/TestFloatEq2.java
❮
❯