# Subgoals

## Section2.26Worked Example: Boolean Relations

### Subgoals for evaluating an assignment statement.

1. Determine resultant data type of expression
2. Update variable for pre operators based on side effect
3. Solve arithmetic equation, operator precedence
4. Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.
5. Update variable for post operators based on side effect

### Subsection2.26.1

Given the following code snippet, evaluate the final statement (the last line). If invalid, give the reason. If valid, what value is assigned to the variable?
int alpha = 42;
int beta = 1;
int gamma = 5;
boolean result;

result = !(beta <= gamma) && gamma <= alpha;


### Subsection2.26.2SG1 : Determine resultant data type of expression

First, note that alpha, beta, and gamma are all integers.
The logical operators in Java are:
• && - AND, if both operands are true the result is true, otherwise the result is false.
• || - OR, if both operands are false the result is false, otherwise the result is true.
• ! - NOT, the opposite logical value (For example, !true is false).
In the final statement, the <= operator is valid to compare primitive types, and produces boolean results, which are then used with the ! and && operators to produce one final boolean.
(You may wish to keep a precedence and associativity reference handy, until you have memorized the order of operations for boolean operators. In this example the relational operators are evaluated before the logical operators and the ! is evaluated before the &&.)

### Subsection2.26.3SG2: Update variables for any pre-increment or pre-decrement operators (side effects)

NOT USED IN THIS EXAMPLE

### Subsection2.26.4SG3: Evaluate arithmetic expression according to operator precedence

Substitute the values for the variables on the RHS and evaluate according to the order of operations.
!(beta <= gamma) && gamma <= alpha
!(1 <= 5) && 5 <= 42
!(true) && true
false && true
false

### Subsection2.26.5SG4: Is the Left Hand Side (LHS) of the assignment statement a variable? Check the data type of the value on right hand side (RHS) against data type of LHS variable.

The LHS is a variable of type boolean, and the RHS is type boolean. This is valid.

### Subsection2.26.6SG5: Update variable for post-increment or post-decrement operators (side effect)

NOT USED IN THIS EXAMPLE
result is true.