A variable is a unit of memory that stores information and can be manipulated by the computer. In pascal, variables have specific data types that determine the behavior of that variable.
Variable names can consist of digits, letters, and underscores. The names of variables are not case-sensitive in Pascal.
Some of the basic variable types are mentioned below.
Type | Description |
---|---|
Integer | Whole numbers. |
Real | Floating point. It is single precision. |
Character | Continuous 8 bits. This is an integer type. |
String | An array of characters. |
Enumerated | A user-defined list. |
Sub-range | Variables whose values lie within a range. |
Boolean | True or false values. This is an integer type. |
Other variable types include pointers, sets etc.
Before use, the variables need to be declared. The var
keyword precedes variable declaration. The syntax is as follows:
var
variable_name : variable_type;
Multiple variables of the same type can be written in one line, as shown:
var
salary, bonus, tax : integer;
The var
keyword needs to be mentioned just once before the variable declaration of all variables:
var
salary, bonus, tax : integer;
first_name, last_name : string;
check, flag : boolean;
Types can be declared with unique identifiers that can later be used to declare variables. Type declaration is shown below:
type
rainfall, temperature = integer;
city, country = string;
These types can then be used to declare variables:
var
summer_rainfall, winter_rainfall : rainfall;
Lahore, Islamabad : city;
Variables are initialized as follows:
variable_name := value;
Variables aren’t initialized to zero by default. It’s a good coding practice to initialize them along with declaration:
years : integer = 10;
Enumerated variables can be declared as follows:
variable1, variable1 : enum_identifier
After declaring an enumerated type, variables of that type can be declared:
type
days = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
Var
today : days;
D := Monday;
Declaration of sub-range values is shown below:
var
subrange_name : lowerlimit ... uperlimit;
The following code shows the use of different variables in Pascal:
program Personal_Information;constintroduction = 'Please give your information below ';typename = string;Birth_Month = (January, February, March, April, May, June, July, August, September, October, November, December);varfirstname, lastname: name;month : Birth_Month;age: 1 .. 100;beginwriteln(introduction);writeln('Please enter your first name: ');readln(firstname);writeln('Please enter your lastname: ');readln(lastname);writeln( 'Enter your age(1 - 100): ');readln(age);writeln('Birth month: ');month := January;writeln;writeln('Name: ' , firstname, ' ', lastname);writeln('Age: ' , age);writeln('Today is: ', month);end.
When the above code is executed, the following output is expected:
Please give your information below
Please enter your first name:
Sheza
Please enter your lastname:
Naveed
Enter your age(1 - 100):
0
Birth month:
Name:
Age: 0
Today is: January
Free Resources