SchoolFreeware's Free Pascal Program Tutorials -
Tutorial 2 Math Calculations. The math
operators that we will use within this
tutorial are as follows: (+ Addition, - Subtraction, * Multiplication, / Division, div Integer Division, mod Remainder).
We will find that
Pascal follows the order operations. This is sometimes taught as please excuse
my dear Aunt Sally within American
Schools
The order of operations is as follows: parentheses
exponents, multiplication, division
addition and subtraction. When we get to
division
will find that the computer puts the output of division
within scientific notation as you see
in this example - Here -
so we would like to remove the scientific
notation from the output
so here we have the same example 10
divided by three
a colon zero
colon two. [Again] 10 divided by 3 with 0
spaces
to the right and 2 decimal places. This
output now is 3.33. The confusing part
about
the number spaces happens to be that
the output is right aligned
so therefore if we want to move the 3.33
over one space we will have to put
a five
with the number spaces because this,
again, is right aligned. We
have
four characters one, two
three, four and then we want to put a space
before
the four characters so therefore we have
to move everything over
five spaces. So,
one, two, three
and then the space will be five. The code used within
this tutorial can be downloaded at:
SchoolFreeware.com
This happens to be the program from the
first tutorial. What I like to do is go to
File
New and select Program under Project
and then click OK. Now we have a new
program
ready for coding. I will scroll down to
where we have begin
and end. I will put in write line (written).
I will start with addition 2 + 5 within parentheses and semicolon.
Of course I have to pause the computer. writeln
writeln     writeln
with press enter to quit
 
and finally readln. OK. So there is our
little program that we start off with. It will do a
simple calculation. I will go ahead and run ---
and we get 7 up there which is
correct.
If I were to point single quotes around
this
will will find the computer printouts verbatim
because this now is no longer a
calculation. It is a string of characters
I'll go back and put 2 + 5
here.
When we go ahead and run this
we just get 7 up there on the screen.
 
We know what's happening here
because we're the programmer,
however, the user who just gets executable program and runs it
doesn't actually see what's going on. What I will like to do is tell user
is that we made a  is simple
calculation of 2 + 5. I'm going to go
ahead and put in a string here. I will start
with a single quote
2 + 5 =
and I'll end with the single quote. So
this is our screen here
and this is our calculation. Now, I have to put in
a spacer
which is a comma. So here we have the
string 2 + 5 = and then
tell a computer here is where the
calculation is. The string
and then the calculation. We
should know what's going on
We see 2 + 5 = 7
We got addition done so lets do some
subtraction. I'll writeln
I'll have 7
- 3 = and then calculation 7 - 3
close parentheses  semicolon
Go ahead run.  Ok, subtraction works.
writeln
do multiplication 2
I could have asterisk (*) 6 as you see here
= , But in this particular case
were just putting a string.
I'll just have an X
for more of a multiplication look - For the
user.
and then of course the computer requires
2 * 6
Then we'll go ahead and run. That
works.
writeln
We're not going to do division at
this moment.
We will go ahead and do example for
the order operations.
2 + 6 within parentheses.
multiplied by 3
=     Here's is the calculation
(2 + 6) * 3
This parentheses works with this one -- here
And, of course, these two
are for the calculation.
I'll go ahead and run. We should get 24.
Which we do.
We have the order of
operations going.
i'll writeln
2 + 6 x 3 =
The calculation of course
will be 2 + 6 * 3
 
We should get
20 when we go ahead and run. Which we do
So the order operations is
confirmed.  writeln
5 mod 3
=         5
mod 3 will be the calculation
and writeln
5 div 3
The "div" is the integer division.
10 div 3. Ok I'll run this.
ok
Everything is looking pretty good.  Now,
this is where the confusion
comes in when we start working with division.
writeln. We will have something like
10 divided by
3 =
10 divided by 3. OK when we go run
we are going to get that scientific notation
which you see here. This is
absolutely correct, however, it  is very
confusing to read especially for people
that are not
familiar with the scientific
notation. I can
tell the computer not to move any items over to the right.
colon 0 and then a colon
and I will have 2 decimal places. So now this should be formatted
to move nothing to the right and two decimal places.
Here we get 3.33
This is what I want. OK let us do one more.
writeln 100 divided by 3
=
100 divided by 3. We will have 0 the right
and 2 decimal places. I'll go ahead run.
Here we have 33.33 which is correct.
Here's one thing that is confusing. If I'm want to line up the
decimal places.
As you see here, these are off by
what we see is two places. If I want
to do this
I'm going to have to do a few calculations
that are confusing. If I want to move
over 2. Guess what going to happen.
It looks like nothing is going to happen.
This is because everything is right
aligned. Therefore if I want to have
this move over what I would say is
two places. I might have to do a little
calculation.
I have 4 characters that I want to
move over. That is automatically 4
and I want these to move over 2 places.
4 characters +
2 places is 6. I'm going to have to
go on and change
this 2 here so it is a 6. The
four characters will move over
2 places. Go ahead run and now
the decimal places are aligned
