|
|
|
Lecture 3/4 |
|
|
|
Variables, Keyboards, Screens & Arithmetic |
|
|
|
|
|
|
A variable is a temporary place to retain
information. We use variables every
day but do not call them by this name.
Your name can be thought of as a variable and the information which
is held there may be ‘John Doe’. |
|
|
|
|
Other examples of variables and the information
they could hold are: |
|
|
|
|
We use variables in Pascal to hold
information. We can then use this
information later during the execution of a program. |
|
The information that a variable holds can be
defined as a specific type of data. |
|
|
|
|
There are a number of data types. Below is a list of the major ones. |
|
|
|
|
|
In order to use a variable in Pascal you must
define it in a specific place called the Var section. This area is only used to define
variables. |
|
|
|
Program ProgramName; |
|
Uses RequiredUnits; |
|
Var |
|
Variables declared here |
|
Begin |
|
End. |
|
|
|
|
|
We must define a variable using a specific
syntax which will be understood by the computer. This syntax is: |
|
VariableName : DataType ; |
|
Which must only appear within the Var
section. Note the use of colon and
semi-colon. This has been
deliberate. |
|
|
|
|
|
We may have the following within a Var section. |
|
Var |
|
surname : String ; |
|
initial : Char ; |
|
price : Real ; |
|
age : Integer ; |
|
Alive_now : Boolean ; |
|
This would now allow us to place information
within these variables and manipulate it during the program’s execution. |
|
|
|
|
|
|
|
When you have more than one variable with the
same data type, you can define them all on the same line. |
|
Var |
|
surname , forename : String ; |
|
price , totalprice : Real ; |
|
There is no significant benefit from doing so
apart from grouping data types together. |
|
|
|
|
|
|
We can put information into variables using a
number of methods. The easiest
method is the assignment method.
The syntax is: |
|
|
|
VariableName := Value ; |
|
|
|
This method is used within the main program code
area. |
|
|
|
|
|
We can assign values of information to
variables. Below are a number of
examples. |
|
|
|
Begin |
|
surname := ‘Doe’ ; |
|
initial := ‘J’ ; |
|
price := 120.99 ; |
|
age := 21 ; |
|
Alive_now := TRUE ; |
|
End. |
|
|
|
Note that a Char and String value has single
quote marks encapsulating the values. |
|
|
|
|
|
We can use the keyboard to assign values of
information to variables. The
syntax would be: |
|
|
|
Readln( VariableName ) ; |
|
|
|
Here you will type in any value, and on pressing
the Enter key that value will be held within the variable used with the READLN
command. Note the use of ( and )
brackets. |
|
|
|
|
|
An example might be: |
|
|
|
Readln( Surname ) ; |
|
|
|
Imagine you typed Doe using the keyboard and
then pressed the Enter key. That
value would now be held in the Surname variable. |
|
|
|
|
|
You can send information to the screen for the
user of the program to read. There
are two methods to do this. The
first one uses the Writeln command. |
|
The syntax for this method is shown below: |
|
|
|
Writeln( Information ) ; |
|
|
|
Here you will send values to the screen and then
take a new line so that the cursor is on the next row down. |
|
|
|
|
|
The Information that can be sent to the screen
can be quite varied. You can send
simple words or phrases to the screen as long as they are enclosed by
single quotes. |
|
|
|
Writeln( ‘This is a single line of text.’ ) ; |
|
Writeln( ‘This is the second line of words.’ ) ; |
|
Writeln( ‘Hello!’ ) ; |
|
|
|
This is the simplest way to send text to the
screen. |
|
|
|
|
|
You can send the values held in variables to the
screen for the program user to see them. |
|
|
|
Writeln( surname ) ; |
|
Writeln( initial ) ; |
|
Writeln( age ) ; |
|
|
|
The user would see the following on screen. |
|
|
|
|
|
When sending a value from a variable that has a
data type of Real, you must define the number of places before and after
the decimal point that you wish to display. The syntax is: |
|
|
|
Writeln( VariableName : before : after ) ; |
|
|
|
|
|
|
|
For example the Price is 120.99 and to show this
we may decide that there are three places before and two places after the
decimal point. |
|
|
|
Writeln( price : 3 : 2 ) ; |
|
|
|
The user would see the following on screen. |
|
|
|
|
|
You can also send combinations of text and
values from variables to the screen.
You simply place a comma between these combinations. |
|
Writeln( ‘The price is £’,price : 3 : 2 ) ; |
|
Writeln( ‘You are ‘,age,’ years old.’ ) ; |
|
The user would see the following on screen. |
|
|
|
|
|
You can also use the Write method which operates
in the same way as Writeln but does not take a new line. |
|
|
|
Write( ‘The price is £’,price : 3 : 2 ) ; |
|
Write( ‘ which is too expensive.’ ) ; |
|
|
|
The user would see the following on screen. |
|
|
|
|
|
You can use Readln, Write & Writeln together
to great effect. Below is an
example. |
|
Write( ‘Please enter the price £’ ) ; |
|
Readln ( price ) ; |
|
Write( ‘ The price you entered was £’,price:3:2
) ; |
|
Writeln( ‘ but thats still too much.‘ ) ; |
|
The user would see the following on screen. |
|
|
|
|
|
A very powerful feature of programs is the
ability to carry out arithmetic. |
|
The arithmetic |
|
symbols we |
|
will use are: |
|
|
|
|
|
|
|
When doing ‘sums’ we can use combinations of the
symbols to calculate answers. These
answers can be held within variables for use later on. |
|
|
|
VariableName := Calculation ; |
|
|
|
As you can see we can also assign the result of
a calculation to a variable. |
|
|
|
|
|
|
|
We can carry out calculations on values such as: |
|
|
|
total := 10 + 2 + 34 - 1 ; |
|
|
|
The result of 45 would be held in the total
variable. |
|
|
|
|
|
|
|
We can carry out calculations on values held by
variables such as: |
|
|
|
subtotal := 99 ; |
|
total := 100 + subtotal ; |
|
|
|
The result of 199 would be held in the total
variable. |
|
|
|
|
|
|
|
When doing ‘sums’ a predetermined order has been
established so that calculations are correct. |
|
This order is based |
|
on the BDMAS rule. |
|
|
|
answer := 12 + 4 * 2; |
|
(left to right = 32) |
|
(bodmas rule
= 20) |
|
|
|
A calculator uses this rule. |
|
|
|
|
|
|
|
|
|
When dealing with integer arithmetic we must use
DIV to carry out division and MOD to find the remainder of a division. |
|
For example to divide 2 into 11 and get 5 using
integer variables we can say: |
|
answer := 11 DIV 2; |
|
You can find the remainder of 1 by saying: |
|
answer := 11 MOD 2; |
|
|
|