The previous page, about variables and assignment, talked about how to move values into variables. However, it did not discuss how to get the values in the first place. Values are generated through expressions. An expression is a series of values and operations which, when performed, result in a value.
There are several types of operators, each of which denotes an operation to be performed on values. Generally, these fall into the categories of arithmetic operators, grouping operators, relational operators, logical operators, bitwise operators, and string operators. Each of these is discussed in this section.
Operators also have a number of operands upon which they act. The multiplication operator, for example, multiplies exactly two numbers together. In order to multiply three numbers together, two multiplication operations are necessary.
Operators also have associativity — if several of the same operator appear in a row, associativity determines which is performed first. For example, in the expression a × b × c, the expression a × b will occur first. Most operators have left to right associativity. Generally, if there is any question about the order in which operations will be performed, grouping operators should be used.
Operators also have precedence — if several operators appear in an expression, operators with higher precedence are performed first. For example, in the expression a × b + c, we would expect from the rules of mathematics that the multiplication is to occur before the addition. Operator precedence ensures this is true in computers as well. This occurs both in simple expressions such as a × b + c, and in complex expressions such as a × tan b/π < 1. The less than operator, <, has lower precedence than multiplication.
Finally, operators have both operand and result data types. The less than operator, <, for example, can be used to compare two floating point numbers. The result, however, is a logical value: the expression a < b can evaluate to either True or False, but not to 1.7-59i.
Copyright © 2002 Brian Hetrick
Page last updated 13 January 2002.
Building Blocks I
Control Flow II
A First Program
Data Structures I
Building Blocks II
Data Structures II