|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
The Assignment StatementTo store a value in a variable you can use the assignment operator (:=) as in the following examples: a:= 55; x:= 1.5; y:= 2.3E+02;
In the following program, two integers a and b are declared in the declaration part, then assigned integer values in the program block. The WRITELN statement is then used to evaluate and display the results of different arithmetic operations performed on those variables. { --------------------------- figure 1-8 --------------------------- } PROGRAM Arithmetic(OUTPUT); { Variable Declarations } VAR a, b :INTEGER; { Program block } BEGIN a:= 25; b:= 2; WRITELN('a=',a); WRITELN('b=',b); WRITELN('a+b=',a+b); WRITELN('a-b=',a-b); WRITELN('a*b=',a*b); WRITELN('a/b=',a/b); WRITELN('a div b=',a DIV b); { used with integers only } WRITELN('a mod b=',a MOD b) { used with integers only } END. The output of the program is: a=25 b=2 a+b=27 a-b=23 a*b=50 a/b= 1.2500000000E+01 ----> Real division a div b=12 ----> Integer division a mod b=1 ----> Remainder of integer division You may assign one variable to another thus: x:= y; In this case, the contents of the variable y are copied to the variable x. You may also assign an arithmetic expression to a variable, like this: z:= a + b - 2; GrossPay:= PayRate * HoursWorked; In these statements the value of the expression to the right of the assignment operator is calculated and stored in the variable to the left of the assignment operator (z or GrossPay).
1-5 Named ConstantsData values (in many languages including Pascal) are called constants, as they never change during the program execution. In Pascal there are two types of constants:
Literal constants are data values such as explicit numbers and text strings, while a named constant is a constant variable. The difference between a named constant and a variable is that the value of the named constant does not change during the program. Like variables, a named constant is given a name and has to be declared in the declaration part. Actually, the declaration part is divided into two sections, CONST and VAR; the CONST section comes before the VAR section. Suppose that you would like to use the value 3.14159 (a numerical constant known as Pi) many times in your calculations. It would be more convenient to give it a name and use the name in your code. You can declare named constants as in the following example: CONST Pi = 3.14159; ThisYear = 1997; Department= 'OtoRhinoLaryngology'; Some constants are predefined in Pascal as standard identifiers. One useful predefined named constant is MAXINT, which gives the maximum value an integer can possess. The value depends on the computer used. If you want to know the value of MAXINT in your computer, use the statement: WRITELN(MAXINT); A typical value is 32767 (two bytes). In the following program, the perimeter of a circle is calculated using the named constant Pi. { --------------------------- figure 1-9 --------------------------- } PROGRAM Constants(OUTPUT); { Constant Declarations } CONST Pi = 3.14159; { Variable Declarations } VAR Radius, Perimeter :REAL; { Program block } BEGIN Radius:= 4.9; Perimeter:= 2 * Pi * Radius; WRITELN('Perimeter=', Perimeter) END. The output of this program is: Perimeter= 3.0787582000E+01
1-6 Type Conversion: ROUND, TRUNCYou can assign an integer to a variable of the type REAL, but the opposite is not permitted. The reason for this is because the storage size allocated for an integer is smaller than that allocated for a real number. If this were permitted, data could be lost or corrupted when a large number was moved to a smaller location in which it did not fit. You can, however, perform the conversion with one of the two functions:
where:
Consider these examples: ROUND(8.4) returns 8 ROUND(8.5) returns 9 TRUNC(8.4) returns 8 TRUNC(8.5) returns 8 As you can see in the examples, the two functions may or may not return the same integer value for the same argument. In the following program the two functions are used to get the rounded and the truncated integer values of the real variable Perimeter. { --------------------------- figure 1-10 --------------------------- } PROGRAM Functions1(OUTPUT); { Constant Declarations } CONST Pi = 3.14159; { Variable Declarations } VAR Perimeter, Radius :REAL; RoundedPerimeter, TruncatedPerimeter :INTEGER; { Program block } BEGIN Radius:= 4.9; Perimeter:= 2*Pi*Radius; RoundedPerimeter:= ROUND(Perimeter); TruncatedPerimeter:= TRUNC(Perimeter); WRITELN('Perimeter=', Perimeter); WRITELN('Perimeter (rounded)=', RoundedPerimeter); WRITELN('Perimeter (truncated)=', TruncatedPerimeter) END. The output is: Perimeter= 3.0772000000E+01 ----> The actual result Perimeter (rounded)=31 ----> Rounded result Perimeter (truncated)=30 ----> Truncated result
|
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. |