|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
1-3 Crunching NumbersThe easiest task for any program is to crunch numbers. The statement WRITELN (or WRITE) can be used both to display numbers and evaluate numerical expressions. You can build up arithmetic expressions using the following arithmetic operators:
Take a look at these examples: WRITELN(123); WRITELN(1.23 * 4); The first example displays the number that came between the parentheses (123). The second example performs multiplication of two numbers and displays the result. Notice that for numeric values, unlike text strings, you dont use quotes. You may use WRITELN to display text and numbers in the same statement by using the comma as a separator like this: WRITELN('The result is=', 125 * 1.75); The following program is used to evaluate two numeric expressions (multiplication and division) and display the results preceded by the proper text. { --------------------------- figure 1-6 ---------------------------} PROGRAM CrunchNumbers(OUTPUT); BEGIN WRITELN('I can easily crunch numbers.'); WRITELN('Here is multiplication of 50x4:',50*4); WRITELN('..and here is division of 2400/8:',2400/8) END. The output of this program is: I can easily crunch numbers. Here is multiplication of 50x4:200 ..and here is division of 2400/8: 3.0000000000E+02 The multiplication is done as expected. The two operands (50 and 4) were integers (whole numbers) and the result (200) was an integer too. The division result, however, came out in a format that needs some explanation. Integers and Real NumbersThe division performed with the operator / is called real division and always produces as its result a real number. Real numbers may be written in fixed-point notation (such as 300.0) or in scientific (exponential) notation (such as 3.0E+02), but in Pascal, real number output will always be represented in scientific notation by default. A number written in scientific notation is made up of two parts divided by the letter E (or e). The left part is called the mantissa and indicates the significant digits, while the right part is called the exponent. The exponent is a power of ten that determines the position of the decimal point. So, in this example the number:
is the same as the number:
The same number, when expressed in fixed-point format, becomes:
If the exponent is preceded by a minus sign as in:
then the decimal point is shifted two positions to the left. This number, then, is the same as:
If the number is negative, the minus sign should precede the mantissa:
If the number is positive, you may omit the sign for either the mantissa or the exponent:
The division operator (/) is called the real division operator, because the result always appears as a real number regardless of the type of the operands. For integer division use the operator DIV as in the example: WRITELN(2400 DIV 8); This will produce the output 300. With integer division, any fraction in the result will be truncated, as in this example: WRITELN(9 DIV 4); produces the output 2. Another important operator, MOD, is used to get the remainder of integer division (modulo), as in these examples: WRITELN(9 MOD 4); produces the output 1. WRITELN(3 MOD 4); produces the output 3. The operators DIV and MOD take only integer operands and produce integer output. For the other operators (+, -, and *), if either one of the operands is real, the result will be real.
|
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. |