Program
A program keyword sets up the global namespace for the program. Program name is any valid identifier. Below is the syntax for defining a Program.
PROGRAM main; { PROGRAM <program_name>;}
Example
PROGRAM helloWorld;BEGINdisplay('Hello World!');END.
Block
There are two blocks in the Programming language. They are given below.
- Declaration Block
- Compound Statement Block
A detailed discussion on each block is given in the next section. Syntax for declaring both the blocks is shown here.
Syntax
PROGRAM block;{ Declaration Block }VARname : STRING;{ All the variables are declared here }{ Compound Statement Block }BEGINname := 'Henry';{ Other code statements comes here }END.
Declaration Block
Variable declaration should be done at the top just after defining the program name. Variable Declaration starts with keyword VAR. Syntax for variable declaration is shown below. This block is optional i.e a program can be written without declaration block if there are no variables used.
VAR<variable_name_1> : <type_1>;<variable_name_2>, <variable_name_3> : <type_2>;
Example
PROGRAM varDecl;VARa : INTEGER;b : REAL;BEGINa := 2;b := 3.00;display('a:', a);display('b:', b)END.
Compound Statement Block
Unlike declaration block compound statement block is not optional. There should be atleast one compound statement block within the program. A coumpound statement block starts with BEGIN keyword and ends with END keyword. A compound statement block can have nested compund statement blocks. Compound statement block can also be empty.
Compound statement block must be written after the declaration block.
Syntax
{ Compound Statement Block }BEGIN{ Code statements ... }END.
Example 1
PROGRAM compoundStatementBlock;{ Declaration Block is optional }{ Compound Statement Block }BEGIN{ Compound statement block can be empty }END.
Example 2
PROGRAM nestedCompoundStatementBlock;{ Declaration Block is optional }{ Compound Statement Block }BEGINBEIGN{ ... }END;END.
Data types
Pascal's Programming language supports for 4 data types.
- Integer
- Real
- String
- Boolean
- Array
1. Integer
An integer can be 0, positive or negative number without decimal point. Below is the example the shows the integer declaration.
number := 22;
2. Real
Any number with decimal point is a real number. Below is the example the shows the real number declaration.
pi := 3.14;
3. String
A string is a group of zero or many characters. String should be wrapped up in single quotes ('). Escape sequence (\) can be used to print single quote(') or backword-slash(\). Below is the example the shows the string declaration.
name := 'R V College';student := 'Student\'s Name'; { Student's Name }slash := '\\'; { \ }
4. Boolean
Boolean can be True or Fasle. Below is the example the shows the Boolean declaration.
isValid := True;isNew := False;
5. Array
Array is collection of values, values can be of any type. Values can be of array type even. Array starts with "[" and ends with "]" and each value in array is seperated by ",". Below is the code that showa the syntax of array declaration.
arr := [1, 'string', 2.00, True, [1,2,3]];
Example
Below is the code example that shows the variable declaration and usage for all the data-types
{ Data types }PROGRAM dataTypes;VARname : STRING;age : INTEGER;salary : REAL;isAdult : BOOLEAN;arr : ARRAY;BEGINname := 'Henry';age := 24;salary := 100000.00;isAdult := True;arr := [name, age, salary, isAdult];display(arr);END.
Operators
There are 5 types of Operators defined in the language syntax
- Assignment Operator
- Arithmentic Operators
- Logical Operators
- Comparision Operators
- Concatenation Operator
1. Assignment Operator
Assignment operator is used to assign a value to a variable. Below code bloack shows the syntax and usage of assignment operator.
name := 'Henry' { 'Henry' string value is assigned to variable 'name'}
2. Arithmentic Operators
Arithmentic Operator Includes:
- Unary Operator
- Addition ( + )
- Subtraction ( - )
- Multiplication ( * )
- Integer Division ( DIV )
- Float Division ( / )
Example
{Arithmetic Operators}PROGRAM arithmeticOperator;VARa,b,c,d,e : INTEGER;x, y, z : REAL;BEGINa := 1;b := 2 + a; { ADDITION }c := b - a; { SUBTRACTION }d := b * 5; { MULTIPLICATION }e := ---++c; { UNARY OPERATOR }x := d DIV b; { INTEGER DIVISION }y := x / c; { REAL DIVISION }z := (a + b) * c -- d DIV x /3; { COMBINATION }display('a:', a, 'b:', b);display('c:', c, 'd:', d);display('x:', x, 'y:', y);display('z:', z);END.
3. Logical Operators
Logical operators returns Boolean. There are 2 logical operators in the language, they are given below:
- Logical AND Operator
- Logical OR Operator
3.1 Logical AND operator
It returns True if both of the operands are of Truthy value. Below is the truth table diagram for the AND logical operator.
FALSE
TRUE
FALSE
FASLE
FALSE
TRUE
FALSE
TRUE
3.2 Logical OR operator
It returns True if any of the operands are of Truthy value. Below is the truth table diagram for the OR logical operator.
FALSE
TRUE
FALSE
FALSE
TRUE
TRUE
TRUE
TRUE
4. Comparision Operator
The result or the final value returned by the comparison operator is a boolean. It can be True or False. Comparision operator Includes:
- Less than Operator ( <)
- Less than or equal Operator( <= )
- Greater than Operator( > )
- Greater than or equal Operator( >= )
- Equal Operator( = )
- Not Equal Operator( != )
Example
{Comparison Operators}PROGRAM comparisonOperator;VARa,b,c,d,e: INTEGER;x,y,z : BOOLEAN;BEGINa := 1;b := 5;c := 2;d := 10;x := a > 2 and 2 < 3 or b > 4;y := b = 5;z := a >= 4 and c <= 3;display('a:', a, 'b:', b);display('c:', c, 'd:', d);display('x:', x);display('y:', y);display('z:', z);END.
5. Concatenation Operator
Concatenation Operator is used to concatenate two strings. $ symbol is used as concatenation operator. Below code example shows the usage of concatenation operator
Example
{ Concatenation Operator}PROGRAM concatenationOperator;VARfirstname, lastname : string;BEGINfirstname := 'RV';lastname := 'COLLEGE';display(firstname $ ' ' $ lastname);END.
Operator Precedence
To evaluate the expressions there is a rule of precedence in language. These precende rules guide the order in which these operations are to be carried out.
The operator precedence in language is listed below in the following table. It is in the descending order (upper group has higher precedence than the lower ones).
If Statements
If statements are used when there is requirement of condition based evaluation. Here first the condtion is checked and then, only its associated code block is executed.
There are 4 varieties of If statements
- if statement
- if else statement
- if elseif elsif ... elseif statement
- if elseif elsif... else statement
Note that all the if statement varieties should be terminated with ENDIF keyword;
Syntax
IF <condition-1> THEN{statements to execute ...}ELSEIF <condition-2> THEN{statements to execute ...}ELSEIF <condition-3> THEN{statements to execute ...}...ELSE{statements to execute ...}ENDIF
Example
{ VOTER }PROGRAM voting;VARage : INTEGER;hasCriminalRecord : BOOLEAN;BEGINage := 19;hasCriminalRecord := False;IF age >= 18 and hasCriminalRecord = False THENdisplay('Eligible for Voting');ELSEdisplay('Not Eligible');ENDIFEND.
Loop
Loop is used to repeatedly execute a group of statements as long as the required condtion is met. Once the condtion fails. If there is else satatement present, then the else block will be executed once. Otherwise the statements after loop will be executed.
Note that else block will be executed even if the loop is terminated with break statement.
Below is the code example for using loop in program.
Syntax
LOOP IF <condition>{statements to execute ...}ELSE{statements to execute ...}ENDLOOP
Example
PROGRAM loopExample;VARa : INTEGER;b : INTEGER;BEGINa := 0;LOOP IF (a < 5)a := a + 1;display('Current value of a:', a);ELSEb := 1;display();display('Out of loop');display('Value of b:', b);ENDLOOPEND.
Example Codes
Below are some of the example codes written in the current programming language.
Example 1: Print Numbers from 1 - 20.
{ Displaying numbers from 1 to 20 }PROGRAM main;VARrange : INTEGER;count : INTEGER;BEGINrange := 20;count := 0;LOOP IF (count < range)count := count + 1;display('Number is :', count);ENDLOOPEND.
Example 2: Factorial of 10
{ Displaying numbers from 1 to 20 }PROGRAM factorial;VARcounter, fact : INTEGER;BEGINcounter := 1;fact := 1;LOOP IF (counter < 10)fact := fact * counter;counter := counter + 1;ENDLOOPdisplay('factorial of 10 :',fact)END.
Example 3: Simple Interest
{ Simple Interest }PROGRAM simpleInterest ;VARprinciple : REAL;rate : REAL;year : INTEGER;amount : REAL;si : REAL;BEGINprinciple := 10000;rate := 5;year := 1;si := principle * year * rate / 100;amount := principle + si;display('Principle :', principle);display('Rate :', rate);display('Time :', year);display('Simple interest :', si);display('Amount :', amount)END.
Example 4: Fibonacci Series
{ Fibonacci Series }PROGRAM fibonacciSeries ;VARterms, count : INTEGER;prevResult, prevPrevResult : INTEGER;currentResult : INTEGER;BEGINterms := 10;count := 2;display('Fibonacci Series:', terms, 'terms');IF (terms = 1) THENdisplay('0');ELSEIF (terms > 1) THENdisplay('0');display('1');prevResult := 1;prevPrevResult := 0;LOOP IF (count < terms)currentResult := prevResult + prevPrevResult;prevPrevResult := prevResult;prevResult := currentResult;count := count + 1;display(currentResult);ENDLOOP;ENDIFEND.