Free Pascal Program Tutorial 3 – Variables and Data Types – Lazarus


SchoolFreeware’s Free Pascal Program Tutorials
Tutorial 3 Variables ad Data Types
Basically, variables hold data. The data
held by the variable can change during
the program’s operation.
The datatype tells the computer
what type of data the variable is
capable of holding.
Here are some common data types.
The Integer is a whole number. The size
and the integer that the computer can
hold
varies by system. If the variable’s
number goes too high then you will
experience an overflow.
A real data type is a floating point
number. It, also, varies by system.
In previous tutorials we used strings.
Strings are basically text
or strings of characters. For example the
text
‘hello world’ would be considered a string.
A char
is a single character. For example
the letter a. The boolean data type either holds
true or false All code can be downloaded
at schoolfreeware.com
schoolfreeware.com
I have already
made a new program and have put the code
to make the computer stop before the
program ends down here.
So, let’s go ahead and make a variable.
After uses
I am going to put in var. This is where variables go.
I’m going to make a variable called name
and name will be a string. Here we have
the variable’s name
which happens to be name followed by a colon
and then the data type ending with a semicolon. The name in the variable
cannot be a reserved word like begin,
end, uses, var and so on.
The variable cannot start with the number.
I will make another variable
called Num1. Even though this variable
name has a number in it
it is not starting with the number. Num1 will be an integer.
I will make another variable called Num2.
I will set Num2 to be a real.
Which is a point number and
finally c
will be a char. We are done with the
variables
That is all the variables were going
to use within this tutorial.
I will go down to where we begin.
I’ll start off with the name
We will have name becomes –
since name is a string I will have to put
my information within single quotes. The
name will be SchoolFreeware.
End with a semicolon.
This reads name becomes
SchoolFreeware. You can also say that
name
this holding the information SchoolFreeware. So, SchoolFreeware
and name, right now, are basically the same thing.
I can writeln the information that
name is holding to the screen
by simply putting in name. We should have SchoolFreewere
printed up there on the screen.
OK everything checks out.
Let’s go work with Num1.
I’ll go ahead and set num1 to become
3. I will writeln
num1. Then go ahead and run
3 is printed up there on the screen.
That is good.
OK, I’ll go ahead and change
num1 so it becomes 10
I will writeln the contents of num1
up there on the screen. The
information within the variable has
changed.
We should have 3 then 10
printed up there on the screen.
Which we do.
Moving on – I will set num2
to equal 3.3
Since this is a floating point number. I will writeln
num2
up there on the screen and of course this is a real floating point number.
I’m expecting to see scientific
notation. Which I do.
num2 I am going to set its
character position to 0. Since we
have one decimal place
I will put in a 1 there. Go ahead and run.
That looks good. 3.3
Time for c
becomes, then, within single quotes
I can put on a character I want. I will put in a % in there.
I will writeln to contents of c to the screen.

OK go ahead run. – There it is.
I would like to take num1 and add it to num2.
To show that we can take the variables and make a calculation out of them.
I will writeln num1 + num2
Since I know that num2 is a floating point, I am
going to put :0:1 there so I can prevent the scientific notation from showing up
Go ahead and run. 10 + 3.3 is 13.3
Before we go ahead and quit,
I’m going to show what happens if you try
to put in a data type
that is not what the computer is
expecting.
Here we have name as a string. If I remove the string
and try to put a number 3.
Go ahead and run. We should get an error
immediately. I’m going to
undo this
back to SchoolFreeware
For num1 it is expecting an integer. I am going to try
giving it a floating-point number. Like
so [ 3.33 ]
Let us see what happens. Again, we get an error message
I will put it back to a
integer. I can try to see if
num1 will accept a string.
It doesn’t accept ’10’ even though we
consider ’10’ to be a number
when it is within single quotes the
computer considers that to be a string
Whatever is within single quotes no
matter if it is a number or not
the computer will consider that to be a
string of characters
not really a number. For our
character here basically we’re allowed
one character. I am going to see what
happens if I put in
several characters. Let us see if the computer accepts that.
No it doesn’t. Definitely, make sure that you are using the correct
data type with your variable otherwise
to quickly get an error
and nothing will compile or run. I will run
this once again this to verify that
everything is working out.
Everything is fine. For the next tutorial we
will get user input.

17 Replies to “Free Pascal Program Tutorial 3 – Variables and Data Types – Lazarus”

  1. One tip for you starters: If you have multiple variables of the same type, you dont have to write them all 1 by 1 like a:integer; b:integer; c:integer; etc. You can put them all together like: a,b,c:integer;

  2. Help! Please
    I made a program with dev pascal in my phone, but when i pastel a code to my computer. the program can't be compiled .

Leave a Reply

Your email address will not be published. Required fields are marked *