# 1.2. Worked Example: Basic KnowledgeΒΆ

Subgoals for evaluating an assignment statement

1. Determine resultant data type of expression

2. Update variable for pre-increment or pre-decrement operators (side effect)

3. Evaluate arithmetic expression according to operator precedence

4. If an assignment statement (=), is Left Hand Side (LHS) a variable? Check data type of value against data type of variable.

5. Update variable for post-increment or post-decrement operators (side effect)

You can watch this video or read through the content below it.

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? Note any possible side effects.

```int alpha = 2,
beta = 1,
delta = 3,
eta,
gamma;
double omega = 2.5,
theta = -1.3,
kappa = 3.0,
lambda,
rho;

lambda=alpha + delta;
```

SG1 : Determine resultant data type of expression

The expression is the right-hand-side (RHS) of the statement: `alpha + delta`. In the first two lines, these variables were declared to be `int` type. Their sum will also be an `int` value.

SG2: Update variable for pre-increment or pre-decrement operators (side effect)

NOT USED IN THIS EXAMPLE

SG3: Evaluate arithmetic expression according to operator precedence

In the declarations, alpha was initialized with a value of 2, and delta was initialized with a value of 3.

The RHS of the statement is `alpha + delta`, so we replace those variable names with their actual values `2 + 3`, which results in a sum of `5`.

SG4: If an assignment statement (=), is Left Hand Side (LHS) a variable? Check data type of value against data type of variable.

The LHS is a variable of type `double`, and the RHS is type `int`. This is valid, as Java automatically promotes an `int` value when we assign it to a `double`.

SG5: Update variable for post-increment or post-decrement operators (side effect)

NOT USED IN THIS EXAMPLE

Questions to check understanding

Q1) Is the left-hand-side (LHS) of the statement a variable? What type?

Q2) What is the resulting type after evaluating the right-hand-side (RHS)?

Q3) Can the type of the RHS result be assigned to the LHS variable?

Practice Pages