//

# http://www.tutorialspoint.com/pascal/pascal_constants.htm

A constant is an entity that remains unchanged during program execution. Pascal allows only constants of the following types to be declared:

• Ordinal types
• Set types
• Pointer types buttheonlyallowedvalueisNil.
• Real types
• Char
• String

## Declaring Constants

Syntax for declaring constants is as follows:

const
identifier = constant_value;

The following table provides examples of some valid constant declarations:

Constant Type Examples
OrdinalIntegertype constant valid_age = 21;
Set type constant Vowels = set of A,E,I,O,U;
Pointer type constant P = NIL;
Real type constant e = 2.7182818;
velocity_light = 3.0E+10;
Character type constant Operator = ‘+’;
String type constant president = ‘Johnny Depp’;

The following example illustrates the concept:

program const_circle (input,output);
const
PI = 3.141592654;
var
r, d, c : real;   {variable declaration: radius, dia, circumference}
begin
writeln('Enter the radius of the circle');
d := 2 * r;
c :=  PI * d;
writeln('The circumference of the circle is ',c:7:2);
end.

When the above code is compiled and executed, it produces the following result:

Enter the radius of the circle
23
The circumference of the circle is 144.51


Observe the formatting in the output statement of the program. The variable c is to be formatted with total number of digits 7 and 2 digits after the decimal sign. Pascal allows such output formatting with the numerical variables.

# PASCAL – VARIABLE TYPES

http://www.tutorialspoint.com/pascal/pascal_variable_types.htm

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in Pascal has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Pascal is not case-sensitive, so uppercase and lowercase letters mean same here. Based on the basic types explained in previous chapter, there will be following basic variable types:

## Basic Variables in Pascal

Type Description
Character Typically a single octet onebyte. This is an integer type.
Integer The most natural size of integer for the machine.
Real A single-precision floating point value.
Boolean Specifies true or false logical values. This is also an integer type.
Enumerated Specifies a user-defined list.
Subrange Represents variables, whose values lie within a range.
String Stores an array of characters.

Pascal programming language also allows defining various other types of variables, which we will cover in subsequent chapters like Pointer, Array, Records, Sets, and Files, etc. For this chapter, let us study only basic variable types.

## Variable Declaration in Pascal

All variables must be declared before we use them in Pascal program. All variable declarations are followed by the var keyword. A declaration specifies a list of variables, followed by a colon : and the type. Syntax of variable declaration is:

var
variable_list : type;

Here, type must be a valid Pascal data type including character, integer, real, boolean, or any user-defined data type, etc., and variable_list may consist of one or more identifier names separated by commas. Some valid variable declarations are shown here:

var
age, weekdays : integer;
taxrate, net_income: real;
name, surname : string;

In the previous tutorial, we have discussed that Pascal allows declaring a type. A type can be identified by a name or identifier. This type can be used to define variables of that type. For example,

type
days, age = integer;
yes, true = boolean;
name, city = string;
fees, expenses = real;

Now, the types so defined can be used in variable declarations:

var
weekdays, holidays : days;
choice: yes;
student_name, emp_name : name;
capital: city;
cost: expenses;

Please note the difference between type declaration and var declaration. Type declaration indicates the category or class of the types such as integer, real, etc., whereas the variable specification indicates the type of values a variable may take. You can compare type declaration in Pascal with typedef in C. Most importantly, the variable name refers to the memory location where the value of the variable is going to be stored. This is not so with the type declaration.

## Variable Initialization in Pascal

Variables are assigned a value with a colon and the equal sign, followed by a constant expression. The general form of assigning a value is:

variable_name := value;

By default, variables in Pascal are not initialized with zero. They may contain rubbish values. So it is a better practice to initialize variables in a program. Variables can be initialized assignedaninitialvaluein their declaration. The initialization is followed by the var keyword and the syntax of initialization is as follows:

var
variable_name : type = value;

Some examples are:

age: integer = 15;
taxrate: real = 0.5;
name: string = 'John Smith';

Let us look at an example, which makes use of various types of variables discussed so far:

program Greetings;
const
message = ' Welcome to the world of Pascal ';
type
name = string;
var
firstname, surname: name;
begin
writeln;
writeln(message, ' ', firstname, ' ', surname);
end.

When the above code is compiled and executed, it produces the following result:

Please enter your first name:
John
Smith
Welcome to the world of Pascal John Smith


## Enumerated Variables

You have seen how to use simple variable types like integer, real and boolean. Now, let’s see variables of enumerated type, which can be defined as:

var
var1, var2, ...  : enum-identifier;

When you have declared an enumerated type, you can declare variables of that type. For example,

type
months = (January, February, March, April, May, June, July, August, September, October, November, December);
Var
m: months;
...
M := January;

The following example illustrates the concept:

program exEnumeration;
type
beverage = (coffee, tea, milk, water, coke, limejuice);
var
drink:beverage;
begin
writeln('Which drink do you want?');
drink := limejuice;
writeln('You can drink ', drink);
end.

When the above code is compiled and executed, it produces the following result:

Which drink do you want?
You can drink limejuice


## Subrange Variables

Subrange variables are declared as:

var
subrange-name : lowerlim ... uperlim;

Examples of subrange variables are:

var
marks: 1 ... 100;
age: 1 ... 25;

The following program illustrates the concept:

program exSubrange;
var
marks: 1 .. 100;
begin
writeln( 'Enter your marks(1 - 100): ');
end.

When the above code is compiled and executed, it produces the following result:

Enter your marks(1 - 100):
100
A



# PASCAL – OPERATORS

http://www.tutorialspoint.com/pascal/pascal_operators.htm

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Pascal allows the following types of operators:

• Arithmetic operators
• Relational operators
• Boolean operators
• Bit operators
• Set operators
• String operators

Let us discuss the arithmetic, relational, Boolean and bit operators one by one. We will discuss the set operators and string operations later.

## Arithmetic Operators

Following table shows all the arithmetic operators supported by Pascal. Assume variable A holds 10 and variable B holds 20, then:

Show Examples

Operator Description Example
+ Adds two operands A + B will give 30
Subtracts second operand from the first A – B will give -10
* Multiplies both operands A * B will give 200
div Divides numerator by denominator B div A will give 2
mod Modulus Operator and remainder of after an integer division B mod A will give 0

## Relational Operators

Following table shows all the relational operators supported by Pascal. Assume variable A holds 10 and variable B holds 20, then:

Show Examples

Operator Description Example
= Checks if the values of two operands are equal or not, if yes, then condition becomes true. A=B is not true.
<> Checks if the values of two operands are equal or not, if values are not equal, then condition becomes true. A<>B is true.
> Checks if the value of left operand is greater than the value of right operand, if yes, then condition becomes true. A>B is not true.
< Checks if the value of left operand is less than the value of right operand, if yes, then condition becomes true. A<B is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes, then condition becomes true. A>=B is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes, then condition becomes true. A<=B is true.

## Boolean Operators

Following table shows all the Boolean operators supported by Pascal language. All these operators work on Boolean operands and produce Boolean results. Assume variable A holds true and variable B holds false, then:

Show Examples

Operator Description Example
and Called Boolean AND operator. If both the operands are true, then condition becomes true. AandB is false.
and then It is similar to the AND operator, however, it guarantees the order in which the compiler evaluates the logical expression. Left to right and the right operands are evaluated only when necessary. AandthenB is false.
or Called Boolean OR Operator. If any of the two operands is true, then condition becomes true. AorB is true.
or else It is similar to Boolean OR, however, it guarantees the order in which the compiler evaluates the logical expression. Left to right and the right operands are evaluated only when necessary. AorelseB is true.
not Called Boolean NOT Operator. Used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. not AandB is true.

## Bit Operators

Bitwise operators work on bits and perform bit-by-bit operation. All these operators work on integer operands and produces integer results. The truth table for bitwise and &, bitwise or |, and bitwise not  are as follows:

p q p & q p | q ~p ~q
0 0 0 0 1 1
0 1 0 1 1 0
1 1 1 1 0 0
1 0 0 1 0 1

Assume if A = 60; and B = 13; now in binary format they will be as follows:

A = 0011 1100

B = 0000 1101

—————–

A&B = 0000 1100

A^B = 0011 0001

~A  = 1100 0011

The Bitwise operators supported by Pascal are listed in the following table. Assume variable A holds 60 and variable B holds 13, then:

Show Examples

Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. A & B will give 12, which is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand. A|B will give 61, which is 0011 1101
! Binary OR Operator copies a bit if it exists in either operand. Its same as | operator. A!B will give 61, which is 0011 1101
~ Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits.  A will give -61, which is 1100 0011 in 2’s complement form due to a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240, which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15, which is 0000 1111

Please note that different implementations of Pascal differ in bitwise operators. Free Pascal, the compiler we used here, however, supports the following bitwise operators:

Operators Operations
not Bitwise NOT
and Bitwise AND
or Bitwise OR
xor Bitwise exclusive OR
shl Bitwise shift left
shr Bitwise shift right
<< Bitwise shift left
>> Bitwise shift right

## Operators Precedence in Pascal

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator.

For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Show Examples

Operator Precedence
~, not, Highest
*, /, div, mod, and, &
|, !, +, -, or,
=, <>, <, <=, >, >=, in
or else, and then Lowest

Trang thông tin Toán học và Tin học

## Thảo luận

NCT Computer

### Thống kê

• 115,384 lượt xem

### pascalteacher.nct@gmail.com

Trang huấn luyện học sinh giỏi Tin học