In this tutorial we'll learn about variables, constants, and data types in Pascal. These three are the heart of programming, and they're what we use to manipulate data in memory.
Data Types in Pascal
What a Data Type Is
Pascal has 2 kinds of data types: standard types and user-defined types. This tutorial only covers the standard ones.
A data type is a set that defines which operations you can perform on a piece of data. Confused? Think back to the states of matter. There are 3: solid, liquid, and gas. You remember that, surely. Now imagine those states as data types, where each state takes different treatment.
We can stack bricks (solid), but not water or gas. We can swim in water, but not in bricks. We can breathe oxygen (gas), but not bricks or water. Who would want to breathe bricks and water anyway.
Those treatments are what I mean by the operations on a data type.
The Kinds of Data Types in Pascal
Standard data types in Pascal come in 5 kinds: integer, real, boolean, char,
and string. You can read a description of each on the Wikipedia
Pascal page, or briefly below.
| Data Type | Description |
|---|---|
integer |
Whole numbers (positive, negative, and the digit 0) |
real |
Decimal or fractional numbers (0.1, 4.1, 99.76, etc.) |
boolean |
Logical values true(1) and false(0) |
char |
Characters in the ASCII table |
string |
Words or sentences |
Data types matter when you declare a variable. Every type takes different treatment, so you have to pick the right one for each variable. The operations themselves come in the next tutorial.
Variables and Constants in Pascal
Now that you know what a data type is, it's time to use one. So what is a data type for? What does it have to do with variables and constants?
You've met variables and constants in math class. The equation of a straight line, for example. Still remember it?
ax + b = y, where a and b are constants.
From that equation we get a straight line as output. Just by changing x we get a value for y. So x and y are variables, since their values change with the conditions. Meanwhile a and b are constants, since their values can't change unless we change them ourselves.
In math, the values of a, b, x and y are always numbers. In programming, the values of a, b, x and y can be anything: numbers, letters, words, sentences, images, and more.
So the data type identifies the type of a variable or constant, so the computer can perform operations. Rather than puzzling over it, let's look at the code.
Oh, and if you want to type the code below into Free Pascal, you can skip the comments (no need to write them).
program Lingkaran;
uses crt;
// constant declaration
const
PI=3.14;
// variable declaration
var
nama: string; // variable nama is a string (word or sentence)
r : integer; // variable r is an integer (whole number)
luas: real; // variable luas is real (decimal or fraction)
// main program
begin
clrscr;
// assign values to the variables
nama := 'Budi'; // variable nama holds 'budi'
r := 7; // variable r (radius) is 7
luas := PI*r*r; // variable luas holds the circle area (PI*r*r)
// output
writeln(nama, ' punya lingkaran');
writeln('jari jari=', r);
writeln('luas lingkaran ', nama, ' adalah ', luas:2:2);
readln;
end.
Once you finish typing, run it. The output looks roughly like this.
Budi punya lingkaran
jari jari=7
luas lingkaran Budi adalah 153.86
Hmm. Why does it come out like that? Let's go through it.
Declaring Variables and Constants
First, the start of the program: the declaration section. You remember which part that is. If not, read the tutorial on the parts and structure of Pascal first.
Think of variables and constants here as the materials for the main program. A constant
declaration is marked by the const keyword, a variable declaration by the var keyword.
....
// constant declaration
const
PI=3.14;
// variable declaration
var
nama: string; // variable nama is a string (word or sentence)
r : integer; // variable r is an integer (whole number)
luas: real; // variable luas is real (decimal or fraction)
....
Declaring Constants (const)
A constant declaration comes after the const keyword. In the example we have one
constant, PI, worth 3.14. You write a constant by putting the constant name on the
left, then an equals sign, then the value you want.
[constant name] = [value]
Quote marks are needed when you declare a constant holding letters, a word, or a sentence. Constants that hold numbers need no quotes. For example:
const
PI = 3.14; // PI holds a number
nama = 'budi'; // nama holds words
Remember that a constant has a fixed value that can't change in the main program (constant means constant, which means fixed).
Declaring Variables (var)
Next comes the variable declaration, which sits below the var keyword. The difference
between a constant and a variable declaration is the value.
With a constant you give the value up front. With a variable you only give the data type, because you can change the value later in the main program.
In the example we declare 3 variables: nama, r, and luas. All three have
different types. The type sits after a colon (:). The format is variable name, then a
colon, then the data type.
[variable name] : [data type]
or several at once
[variable name], [variable name] : [data type]
For the type, fill in whatever the start of this post lists.
Assigning Values to Variables
We've declared the variables, so now they need values. Assigning a value, usually called
assignment in Pascal, uses the := sign.
In the example we fill the 3 declared variables. The variable r is 7, nama is
'budi', and luas holds the result of the area calculation.
....
// assign values to the variables
nama := 'Budi'; // variable nama holds 'budi'
r := 7; // variable r (radius) is 7
luas := PI*r*r; // variable luas holds the circle area (PI*r*r)
....
You're free to give r and nama any value, as long as it matches the declared type.
Change r to 10 and the program still runs, since 10 is still an integer. Fill it with
9.7 and the program errors out, because 9.7 is a decimal of type real.
Remember that you must write a decimal number with a period (.), not a comma (,). So 0.5, not 0,5.
For string and char variables, put the value between single quotes ('), like
'Budi' above.
A variable doesn't only hold fixed numbers. It can hold the result of an operation too, like
luas in the example. We'll cover operations in Pascal in the next tutorial.
Printing Variable Values
Now the part that decides a program's success: the output
system. Printing a variable in Pascal is very
simple. Here we'll use the writeln command.
....
// output
writeln(nama, ' punya lingkaran');
writeln('jari jari=', r);
writeln('luas lingkaran ', nama, ' adalah ', luas:2:2);
....
In the example we print a variable by calling it. You call a variable by writing its name without quotes. Add quotes and Pascal reads it as ordinary words.
To separate a variable from words, just use a comma (,). In the first writeln we call the
variable nama, which holds 'Budi', then continue with the words ' punya lingkaran.' So
the output is 'Budi punya lingkaran'. And so on to the last writeln.
One benefit of variables is efficiency. In the example above, nama is called 2 times. Try changing nama to your own name and run the program. The output is no longer 'budi punya lingkaran', but '['your name] punya lingkaran'.
....
nama := 'Adi'; // change nama from 'budi' to 'adi'
r := 10; // change r from 7 to 10
luas := PI*r*r;
writeln(nama, ' punya lingkaran');
writeln('jari jari=', r);
writeln('luas lingkaran ', nama, ' adalah ', luas:2:2);
....
Run that program and the result looks like this.
Adi punya lingkaran
jari jari=10
luas lingkaran Adi adalah 314.00
What's Next
That's it for this one, hope it helps 👋
Next we'll cover arithmetic operations in Pascal.