Turbo Pascal 2.0 course cover
← Back to Courses
module
7

Sums

Introduction

Computers were built to do sums. But Turbo's whole numbers have edges, and a sum that runs off one does not stop with an error - it quietly gives you the wrong answer. This section is the arithmetic, and the edges.

Printing Numbers

In S4 you printed a sum: Writeln(12 * 12) printed 144. Writeln will print several things at once, separated by commas, one straight after the other:

Writeln('7 + 2 = ', 7 + 2);
7 + 2 = 9

The text in quotes is printed as it is, space and all; the sum is worked out and its answer printed. Numbers are printed with nothing round them - Writeln(7 + 2, 7 - 2, 7 * 2) prints 9514, which is 9, 5 and 14 jammed together. When you print more than one number, put something between them:

Writeln(7 + 2, ' ', 7 - 2, ' ', 7 * 2);

The Operators

OperatorDoesExampleGives
+add7 + 29
-subtract7 - 25
*multiply7 * 214
divdivide, whole number part7 div 23
modthe remainder after div7 mod 21

div and mod are words, not signs, and they need spaces round them like any other word. Between them they answer the question "how many times does 2 go into 7, and what is left over?"

With a negative number, div throws away the fraction towards zero: -7 div 2 is -3, and -7 mod 2 is -1.

There is a sixth: /, the ordinary division sign. It does not give a whole number. Writeln(7 / 2) prints

  3.5000000000E+00

which is 3.5, written the way Turbo writes numbers with fractions. That is a different kind of number altogether, and S9 is about it. Until then, divide whole numbers with div.

Which Comes First

2 + 3 * 4 is 14, not 20: multiplying and dividing - *, /, div, mod - are done before adding and subtracting. Otherwise the work goes from left to right: 20 - 6 - 4 is 10, and 20 div 2 * 5 is 50.

Brackets are done first of all. (2 + 3) * 4 is 20. When you are not sure which way Turbo will take a sum, put brackets in, and the next person to read it - which is usually you - will not have to work it out either.

Lining Numbers Up

Put a colon and a number after something you print, and it is printed right-aligned in that many columns:

Writeln('[', 42:5, ']');
[   42]

Three spaces and two digits make five. It is how you get a column of numbers to line up on the right. If the number needs more room than you gave it, it simply takes it: 42:1 prints 42.

The Edges

Turbo's whole numbers - integers - run from -32768 to 32767. Turbo has a name for the top one, MaxInt: Writeln(MaxInt) prints 32767.

Go past the top and nothing stops you. The number wraps round to the bottom:

Writeln(32767 + 1);
-32768

No error, no warning: just a wrong answer. And it happens in the middle of a sum as well as at the end. 1000 * 100 div 50 ought to be 2000. But 1000 * 100 is 100,000, which is past the top, so it wraps round before the div is ever reached, and the whole sum prints -621.

Turbo does check the numbers you write in the program. Type a number too big to be an integer, such as 40000, and it will not compile:

Error 56: Error in integer constant. Press <ESC>

One oddity: you cannot write -32768 either, though it is an integer - the 32768 is refused, minus sign or no. If you ever need it, -32767 - 1 gets you there.

Dividing By Nothing

7 / 0 stops the program:

Run-time error 02, PC=0045
Program aborted

Searching
  4 lines

Run-time error position found. Press <ESC>

A run-time error is one Turbo could not have seen when it compiled the program, because the sum is only done when the program runs. Press ESC and Turbo puts you in the editor at the statement that failed, just as it does for a compile error.

div and mod by zero are worse: they do not stop at all. 7 div 0 prints -1 and 7 mod 0 prints -7, and the program carries on with those. If a program of yours might divide by something that could be zero, it is up to you to check first - S12 shows how.

The Code

program Sums;
begin
  Writeln('7 + 2 = ', 7 + 2);
  Writeln('7 - 2 = ', 7 - 2);
  Writeln('7 * 2 = ', 7 * 2);
  Writeln('7 div 2 = ', 7 div 2);
  Writeln('7 mod 2 = ', 7 mod 2);
  Writeln('2 + 3 * 4 = ', 2 + 3 * 4);
end.

Starting from

Turbo freshly started, Y, a new work file called SUMS.

What you should see

7 + 2 = 9
7 - 2 = 5
7 * 2 = 14
7 div 2 = 3
7 mod 2 = 1
2 + 3 * 4 = 14

Change One Thing

  • Put brackets round 2 + 3 in the last line, both inside the quotes and outside them. What does it print now?
  • Change the two 7s in the div and mod lines to -7, quotes and all. Which way does the answer go?
  • Change the * line to use / instead. What do you get, and why is it so different from the lines round it?

Exercises

7.1 Print the number of minutes in a day.

7.2 Print the number of seconds in a day. Is the answer right? If not, what has happened to it?

7.3 One hundred sweets are shared among seven children, as many each as will go round equally. Print how many each child gets and how many are left over.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
Numbers printed run into each otherWriteln puts nothing between numbers. Print ' ' between them.
A sum gives a negative answer, or a wildly wrong oneIt went past 32767 - perhaps in the middle of the sum. Do the dividing first, or use smaller steps.
Error 56: Error in integer constantA number in the program is bigger than 32767 - or is -32768.
Run-time error 02Something was divided by zero with /. ESC shows where.
div or mod gives a nonsense answerPossibly a division by zero, which div and mod do not report.

Summary

+, -, *, div and mod work on whole numbers; *, div and mod come before + and -, brackets before everything. Writeln prints several things separated by commas, and :n lines a number up in n columns. Integers go from -32768 to 32767 and wrap round silently past either end; dividing by zero stops a program with / and does not with div or mod.

Next

S8, Names For Numbers - keeping a number in a variable, changing it, and building a result a step at a time.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.