|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Evaluation of Arithmetic ExpressionsWhen you build more complicated arithmetic expressions, you have to watch the priority of each operator involved in the expression. Take a look at these two expressions: 2 + 10 / 2 (2 + 10) / 2 Although the two expressions use the same numbers and operators, the first expression is evaluated as 7, while the second is evaluated as 6. This is because in the first expression the division is evaluated before the addition, while in the second expression the parentheses are used to change the order of evaluation, in which case the expression inside the parentheses is evaluated first. In general, the arithmetic operators in Pascal have two levels of precedence: high and low. The + and - have low precedence, while all other operators have high precedence. If an expression contains two operators of the same precedence level, they are evaluated from left to right. Consider this example: 5 + 3 * 2 - 6 DIV 2 The first operation to be performed is the multiplication: 5 + 6 - 6 DIV 2 The second operation, of next highest priority, is the division: 5 + 6 - 3 This leaves two operations of equal priority. They are evaluated from left to right giving: 8 When parentheses are used to alter the order of evaluation, they form subexpressions which are evaluated first. If parentheses are nested, the innermost subexpressions are evaluated first. Consider the same example with nested parentheses: ((5 + 3) * 2 - 6) DIV 2 This expression is evaluated according to the following steps: (8 * 2 - 6) DIV 2 (16 - 6) DIV 2 10 DIV 2 5 Arithmetic operators are summarized in Table 1-1, along with their precedence and properties. The + and - signs are also used as unary operators (to signify positive and negative). The unary operators are of the same low priority as the binary operators + and -. If a binary operator precedes the unary operator such as 5 * 4, you must enclose the unary operator and its number in parentheses: 5 * (-4). The first form may be accepted by some compilers, but do not try it.
1-4 Using VariablesData are stored in the memory locations at specific addresses. Programmers, however, refer to these locations using variables. When variables are used in a program, they are associated with the specific memory locations. The value of a variable is actually the contents of its memory location. As data are processed by the program, the contents of any location may change, and so does the value of the associated variable. Variables are given names (identifiers) according to the rules mentioned before. Variable DeclarationBefore using a variable in a Pascal program, its name and type must be declared in a special part of the program called the declaration part. This part starts with the keyword VAR, as in the following example: VAR a :INTEGER; x :REAL; The variable a is of the type INTEGER, which means that it can hold only integer numbers such as 4, 556, and 32145. The variable x is declared as of the type REAL and can hold real numbers such as 3.14, 44.567, and 3.5E+02. If you want to declare more than one variable of the same type, you may declare each on a separate line: VAR a :INTEGER; b :INTEGER; c :INTEGER; x :REAL; y :REAL; or, you may also declare all variables of the same type as a list like this: VAR a, b, c :INTEGER; x, y :REAL; The keywords INTEGER and REAL are classified as standard identifiers, which are predefined in Pascal. The standard identifiers can be redefined by the programmer, but this is strongly recommended against. Standard identifiers are listed in Appendix B. In the following program three variables are declared: a and b are integers, while x is real. The contents of each are displayed using the WRITELN statement. { --------------------------- figure 1-7 --------------------------- } PROGRAM Variables(OUTPUT); { Variable Declarations } VAR a, b :INTEGER; x :REAL; { Program Block } BEGIN WRITELN('Contents of a=',a); WRITELN('Contents of b=',b); WRITELN('Contents of x=',x) END. The output of the program is something like the following: Contents of a=0 Contents of b=631 Contents of x= 2.7216107254E-26 Note that the contents of a and b are displayed as integers while the contents of x are displayed in real format. However, the output numbers are just garbage because no values were actually stored in those variables. Unless you store data values in your variables, they will contain whatever was last left in those memory locations.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |