PROG
The program serves no useful purpose, other than educational.
PED
To learn a few more things about assignment statements.
CONCEPT
The Form: box_name:=box_name+1 is used to increment a variable.
Use parentheses to guarantee that an expression will happen the way you expect.
If parentheses are not used, multiplication and division happen first.
Click here to view Unit 1.3 Pascal program.
In this unit, we discuss some of the more sophisticated operations one can do with assignment statements to make sure you understand how to do them. The example above doesn't serve any specific function other than providing a method for us to show these techniques.
From our example, line 2, we see that there are six boxes. They are named a,b,c,d,e, and f.
(Let's assume for the sake of argument, that for each read, the user will type in a number equal to the line number. Thus, line 4, we will assume the user types in a 4.
So,
Line 4, box a will be set to contain 4.
Line 5, box b will be set to contain 5.
Line 6 will compute c which will be 9.
Then, we will do another read of b in line 7. Thus b will be 7. The old value of 5 will be gone permanently.
Line 8 will compute d to be 20, 4 + 7 + 9.
Line 9 will copy the contents of c to the screen. We will see a 9 on the output.
Line 10 will copy of the contents of the box called d. We will see a twenty. Next, we will skip a line on the output.
Then we will compute e:=(d*b)+c on line 12. This will compute (20*7)+9. That is 149. e will be 149.
Then line 13, f will be computed as d*(b+c). This computes 20*(7+9)=320. The difference is due to the difference in parentheses. f will be 320.
In line 14, we see the concept of precedence. We didn't use parentheses to tell the computer whether to group d*b or b+c first. Multiplication is considered higher in importance so we will compute it as the equivalent of (d*b)+c. Thus g will be 320 as well.
Line 15 will output the contents of the box called e which is 149. The number 149 will go out to the screen. Likewise, line 16 will cause the number 320 to go out to the screen. And line 17 will cause the number 320 to go out to the screen. Line 18 will cause the next number to appear on a new line.
Then line 19 will cause us to evaluate the expression f+1 which will gives us 321. We then put it into the variable whose name is give on the left hand side. f will now be 321. This form: box_name:=box_name+1 is very important. It's how we increment a variable inside a program. You will see it lots and lots of times when we cover looping.
Line 20 will cause 321 to appear on the output screen.
A couple of words about the integer and real-type variables, and converting between them. Let's review the two types of variables or boxes we have learned about. They are "integer" and "real" Integer variables contain numbers without a decimal point, e.g. 4, 7 297328, 4238, 456, 0, -15, -56239. Real numbers can have decimal points--they can be used for extremely large numbers--when you think of a real number, think of a number in a calculator.
Examples of real numbers are 2.0 , 3.25, 4.78, -5.78, -8.25, -2378.23,
.0000123,
2.34E7 , 5.8E12, 2.243E-04, 4.25E-37
The last four numbers illustrate a real number expressed in scientific notation. The number after the "E" is a power of 10, by which the rest of the number is multiplied. Thus, in general terms, aaaa.aaaaEyy corresponds to aaaa.aaaa X 10yy
For example 2.34E7 corresponds to 2.34 X 107. 5.8E12 corresponds to 5.8 X 1012.
2.243E-04 corresponds to 2.243 X 10-4 or .0002243
4.25E-37 corresponds to 4.25 X 10-37.
In our examples, we always had one kind of a variable or another. That is each program had all its boxes of type integer or all of its boxes were of type real;
However, we can have a mixture of real and integer type boxes.
When we do this, the beginning of the program might look as follows:
program blah;
var a,b,c:real;
d,e,f:integer;
a,b, and, c would be real variables--boxes that can contain a real number. While d, e, and f woudl be boxes that can contain only integers.
There are a couple of little gotcha's.
They have to do with the fact that an integer can't represent as many types of numbers as real variables can. Let's say we have a box declared real such as a.
Let's say we write
d:=a;
a would probably contain a number that won't fit into a. For example, a might contain 2.34. When we assign it to d, PASCAL complains. That's because PaSCAL doesn't know whether to make d contain 2 or 3. Or worse, perhaps you didn't realize that the copying of the number from d to a woudl cause information to be lost.
You must indicate to PASCAL that you realize that the copy from a to d will change the value and how you want it to change. The statement "d:=a" would generate a syntax error message when you attempt to compile your program.
You do this by using the functions, round, truncate.
The expression "round(xxx)" will be an integer which is the result of rounding xxx to the nearest integer. xxx should be a variable of type "real"
You may also write "trunc(xxx)" to simply remove the fractional part or "truncate".
For example, one could write
d:=round(a);
OR
d:=trunc(a);
as needed to get the number out of the real box a and into the integer box d.
A related problem is the use of the two division operators "/" and "div"
Recall that we use "/" between real numbers and "div" between integers.
The problem is as follows:
Let us say that a contained 1 and b contained 3.
If one divided a by b, that is "a/b" the result would be ".333333" which is the decimal fractional equivalent to one third.
Let us contrast the case of two boxes of type integer. Let us say d contained 1 and e contained 3 and f contained 7. When the comptuer divides two integers, it enerates an integer back. However, the result would be truncatged to the integer just below the result. Divising f by e would generate 2, not 2.3333 And more dramatically dividing d by e would generate 0, not 0.3333
PASCAL makes sure that you are aware of this unusual behavior. It does this by insisting that you write the word "div" between two integers instead of the customary slash ("/").