Variables and Expressions

An expression is a statement that causes the computer to perform an action such as a calculation, writing to a file, creating a graphical display, or communicating with a user. Expressions are written in the language of the programming system. We will use the terms expression and statement interchangeably, leaning toward expression when we are thinking computationally and toward statement when we are thinking about operating system actions.

The most basic calculation expressions make use of constants, variables and operators. Suppose that A, B and C are variables that have values currently assigned to them. We can then use them with constants and operators to form new values that can then be assigned to other variables.

An example of a calculation expression is:
D=(A+B)*C

This expression looks like an equation. It makes use of the addition "+" operator, the multiplication "*" operator, the grouping "()" operator and the assignment "=" operator. When they are combined as shown they will calculate the value that you would expect and assign the result to a variable named D.

The above expression does not indicate the data types that were assigned to A, B and C before the computation. You cannot tell from the expression whether they are bytes, integers, long integers, floating point, complex or even if they are numbers at all. In fact, they need not all be of the same type, and often are not. They can be of any mixture of types for which the operations make sense and for which the operations are supported by the programming system. This is a very useful feature of IDL because it enables a fluent expression of the programmers intent using a conceptual form that is consistent and meaningful to people. But, like anything that is powerful and useful, you need to be aware of how it works to avoid having the power misdirected.

In IDL the data type of a variable is determined at the time that a value is assigned to it. The type may be changed if a new assignment is made using a different data type.

Examples
 
a=10 Assign the integer value 10 to the variable a.
b=3.4 Assign the floating point value 3.4 to the variable b.
c=2*a+4 Calculate the integer value 24 and assign the value to c
a=3*a+c Calculate the integer value 54 and assign the value to a
d=a+2*b Calculate the floating point value 16.8 and assign the value to d
The first two lines are examples of the assignment of a numerical value to a variable; the variable's type is determined by the number type. The third line is an expression that uses two operations; the multiplication is done first. Multiplication would also have been done first if the expression had been written d=4+2*a. The order of operations is determined by a hierarchy, which will be discussed below. The fourth line shows that the value of a variable can be changed by an assignment. Here "a" is used in the computation on the right-hand side and then is assigned the value that is produced. This is an example of why it does not make sense to interpret an expression as an equation. Assignment "=" and the use of the = sign in equations have different meanings. The fifth expression is interesting because it combines integer and floating point data types in the same expression. How does the system decide on the proper data type for the result?

The expressions that can be written are determined by the operators and the operator hierarchy.