The use of data in expressions and in assignment statements is the focus of this section. Expressions are used as arguments in many program statements, so you will see them again in later sections.
An expression returns a numerical value, a boolean value, an enumerated value, a character, or a string. They do not return entire arrays, records, or unions. An expression can be one of:
Form (a) must represent a value from one of the standard data types or an enumeration. The name may represent a constant, an initialized variable, or a function. Form (b) can represent any of the standard types. Forms (c), (d), and (e) allow evaluation of complex arithmetic and boolean expressions.a. name b. literal constant c. expression operator expression d. operator expression e. (expression)
T uses the following operators in arithmetic expressions:
In arithmetic expressions, the order of operations is from left to right for all but exponentiation. Exponentiation has the highest precedence; next is the group: * / mod div and last is the group: + -. For example:+ integer or real addition - integer or real subtraction * integer or real multiplication / integer or real division (result is real) div integer quotient mod integer remainder ^ integer or real exponentiation
The operators and, nand, or, nor, xor, and not may be used in boolean expressions. These are operate as follow on boolean data as follows:4 + 9 div 2*(9 - 11 mod 3^2) 4 + 9 div 2*(9 - 11 mod 9) 4 + 9 div 2*(9 - 2) 4 + 9 div 2*7 4 + 4 * 7 4 + 28 32
In T, the operator not is a unary operator and has higher precedence than the operators and and nand which have higher precedence than or, nor, and xor.x y not x x and y x nand y x or y x nor y x xor y false false true false true false true false false true true false true true false true true false false false true true false true true true false true false true false false
T's comparison operators accept integer, real, character, or string operands and return true if the comparison is satisfied, otherwise they return false:
A comparison is a boolean factor and may be used as an operand in a boolean expression.operator comparison = equal to ~= not equal to > greater than >= greater than or equal to < less than <= less than or equal to
The order of precedence for all operators used in T from highest to lowest is:
^ + - (as unary operators) * / div mod + - = ~= < <= > >= not and nand or nor xor
Assignment statements have the form:
The name on the left hand side of ":=" must be for a variable of standard type or a standard type item of a user defined data type. The expression must be compatible with name, i.e., both sides of the symbol ":=" must have the identical data type except when an integer is assigned to a real number variable.name := expression
The assignment statement is used to assign a new value to a variable. An assignment statement closely resembles an equation. Recall from the "first.t" program that one line contained:
In a computer program, this means that the value of the expression "x + y" is to be assigned to the memory location identified by "sum" which is its name. The assignment operator is the symbol ":=". It causes the memory location identified to the left of it to be assigned the value of the expression to the right.sum := x + y
An assignment statement is not an equality. Consider a statement used frequently in repetitive computer operations:
What happens to the value of "x" when this statement executes? If you are not sure, try adding it to "first.t" and observe.x := x + 1
In T, an integer value may be assigned to a real variable; however, a real value may not be assigned to a variable declared as an integer. This prevents loss of information.
As an exercise, try entering and running the following program:
% "invert2.t" inverts a two by two matrix
program
var x11 : real := 6.0 % matrix to invert
var x12 : real := 3.0
var x21 : real := 2.0
var x22 : real := 4.0
var det : real % determinant
var y11, y12, y21, y22 : real % inverted matrix
% calculate the determinant:
det := x11 * x22 - x12 * x21
% calculate the inverted matrix
y11 := x22 / det
y12 := -x12 / det
y21 := -x21 / det
y22 := x11 / det
put y11, " ", y12
put y21, " ", y22
end program
T supports a large variety of standard numerical functions; additionally, the standard
functions strint and strreal convert strings into numbers and may
be used in expressions.
T supports the boolean data type which allows a variable to take a value of true or false. A boolean value may be assigned only to a boolean variable. Boolean variables are often used in logical statements which control program execution. The following could be added to the invert2.t program:
Later, in the chapter on looping, branching, and jumping we will see how boolean expressions are most frequently used in computer programs.var singular : boolean singular := det = 0.0 % invert if false assert not singular
A character may be assigned only to a character variable. For example, this:
initializes the variable string msg. An individual character in a string may be accessed using an indexed form of the string variable name. The following statements are valid:var msg : string msg(0) := 'H' msg(1) := 'i' msg(2) := '\0' % strings are null terminated
The standard function ord accepts a character and returns its ASCII (American Standard Code for Information Interchange) code as an integer. Its inverse is the function chr which converts an integer into a character.input := name(0) % gets first character msg(4) := one % sets fifth character
T supports string expressions which can use the concatenation operator &. String expressions concatenate a sequence of strings and may be assigned only to string variables of compatible length. As an exercise, try entering and running the following program:
% "welcome.t" welcomes you to T
const msg1 : string := "Hi, what's your name? "
const msg2 : string := "Welcome to T, "
var message : string(80)
var name : string(16)
program
put msg1...
get name
message := msg2 & name & "!"
put message
end program
The functions intstr, realstr, erealstr, and frealstr
convert numbers into formatted strings and may be used in string expressions. Note
that characters may not be concatenated into strings.
Enumerated data is a data type which you can define and use in a program. Except for the assignment, there are no operators for enumerated data. An enumerated value may be assigned only to a variable of the same type you defined. The standard functions pred and succ return enumeration values and may be used in expressions.
For example, the following declarations and statements are valid in a program:
type day : enum(sun, mon, tue, wed, thu, fri, sat) var yesterday, today, tomorrow : day today := day.mon yesterday := pred(today) tomorrow := succ(today)
This section explained how to write numerical, boolean, and string expressions using the operators available in the T language. It also explained the syntax of the assignment statement. Expressions containing these fundamental data types will be important is subsequent sections.