Java

TOPIC 15 – IF STATEMENTS - PART 2

 

 

LESSON NOTE

 

 

ADDING MORE STATEMENT BLOCKS

 

There are situations when we need to be able to have more than two different options provided by the if-and-else structure.  To do this, we simply use an else if.

 

ELSE IF

 

Using else if allows us to add more statement blocks inside of an if structure.  Each block has its own condition.  See below:

 

COMPLETE IF-STRUCTURE

 

Below is the syntax for the if-structure in Java.  It can have any number of else if statements.

 

if (condition)

{

      //statement block A

}

else if (condition)

{

      //statement block B

}

.

.

.

else if (condition)

{

      //statement block N

}

else

{

      //default statement block

}

 

DETAILS

Note that only one statement block is ever executed.  It will be the one that is located just under the first condition that is true.  Once a statement block is executed, other conditions are completely skipped over.

 

If there is no true condition, then the statement block under the else will be executed.

There can be any number of else if statements (including zero).

The else statement can be omitted.  In such a situation, it is possible that no statement block will be executed at all.

COMPARING DOUBLES

 

Computers cannot always perfectly store numbers with decimal places.  Therefore, instead of comparing two such numbers to see if they are exactly equal, it is best to check if their absolute difference is very small

 

EXAMPLE
Assume x and y are doubles.

 

BAD METHOD

 

if (x == y)

{

  

}

GOOD METHOD

 

if (Math.abs(x – y) < 0.001)

{

  

}

 

COMPOUND BOOLEAN EXPRESSIONS

 

A boolean expression can consist of many boolean expressions connected together by one or more logical operators.  Here are the symbols used for the operators:

Java

Name

&&

||

!

AND

OR

NOT (negation)

 

Two expressions joined by AND will only give an overall result of true if both expressions are true.

Two expressions joined by OR will give an overall result of true as long as at least one expression is true.

The NOT operator negates the value of the expression.

 

EXAMPLE – State if the following Boolean expressions are true or false

 

a)    (10 < 4) && (6 == 4)

b)    (10 < 4) || (6 > 4)

c)    (21 < 34 / 2 + 5) || (17 – 3) / 2 == 4)

d)    !(10 < 4)

e)    (6 < 5) && ((6 >= 6) || (7 > 18))

 

SOLUTIONS

 

a)    False

b)    True

c)    True

d)    True

e)    False

 

JAVA ONLY COMPUTES WHAT IT NEEDS TO

 

When you have two Boolean expressions combined by an AND, if the first one is false, the overall result will be false.  There is no need to compute the second result.  In such a situation, Java doesn’t even try to compute the second result.

 

PRECEDENCE RULES

 

  • Logical operators also have precedence rules that affect them.  To avoid confusion, always use brackets to specify what you think should be done first.

  • One specific precedence rule that is sometimes an issue is that the AND has a higher precedence than the OR.  Therefore the AND gets done before the OR.