Assignment and datatypes

General information

Assignment is not part of expression evaluation. In an assignment statement, the value of an expression is converted to the datatype of the left-hand variable. In the expression

c = a + b

the datatype of a+b is determined by the datatypes of a and b. Then, the result is converted to the datatype of c.

Overflow on assignment

Even when PocketBuilder performs a calculation at high enough precision to handle the results, assignment to a lower precision variable can cause overflow, producing the wrong result.

Example 1 Consider this code:

integer a = 32000, b = 1000

long d

d = a + b

The final value of d is 33000. The calculation proceeds like this:

Because the variable d is a long, the value 33000 does not cause overflow.

Example 2 In contrast, consider this code with an assignment to an integer variable:

integer a = 32000, b = 1000, c

long e

c = a + b

e = c

The resulting value of c and e is -32536. The calculation proceeds like this:

The assignment to c causes the long result of the addition to be truncated, causing overflow and wrapping. Assigning c to e cannot restore the lost information.