Pascal Programming
Pascal is high level and general purpose programming language. It has several advantages such as easy to learn, structured language, can create efficient program. It is platform independent can compiled variety of platforms. It is a non. case sensitive language
Pascal Program Structure
A Pascal program has the following parts. It is not compulsory to have all parts in a program.
{ } is used to have comments where ever needed to improve maintainability. compiler ignore the comments.
program test; {name of the program} uses crt; {libraries to use} const {declare global constant } pi=22/7; var {declare global variable} x: integer; function {Declare functions} { local variables } begin ... end; procedure {declare procedures} var{ local variables } begin ... end; begin { main program} ... end. {end of main}
Keywords
Following are the reserved words available in Pascal.
and | array | begin | case | const |
div | do | downto | else | end |
file | for | function | goto | if |
in | label | mod | nil | not |
of | or | packed | procedure | program |
record | repeat | set | then | to |
type | until | var | while | with |
Pascal identifiers
Pascal program has several entities such as variables, constants, types, functions, procedures, records, etc. The entities have a name called identifier.
Pascal identifiers |
---|
* Start with alphabet |
* Without Spaces |
* Underscore not as first letter |
* Special characters are not allowed (&, $, %, *, = , + ...) |
Correct identifiers | Wrong identifiers |
---|---|
ABC, Abx, A_123, Tot_Price | 123, 1ABC, abc xyz, _123, tot& |
Pascal Program to Display Messages
The below programs display the messages in the screen. The command to display the messages in the screen is writeln or write.
program FirstPascal ; begin writeln('My first Program'); writeln('Pascal'); readln; end. Output My first Program Pascal Program second; begin write('10+20='); writeln(10+20); readln; end. Output 10+20=30
Variables in Pascal
A variable is a name given to a storage area that a program accesses to manipulate. Each variable has a specific type and each type has its own size which is allocated in the Memory.
Data types in Pascal | Literals |
---|---|
integer | 10,20.50,150 |
char | 'A','B','C' |
real | 5.60, 150.75 |
string | 'Pascal' |
boolean | true, false |
program PasVariables; var num1: integer; num2:integer; begin num1:=15; num2:=20; write('num1+num2='); writeln(num1+num2); readln; end. Output num1+num2=35
User Input
Pascal command read/readln is used to get input form the user.
program UserDatain; var num1: integer; num2:integer; begin write('Enter a number?'); readln(num1); write('Enter another number?'); readln(num2); write('num1+num2='); writeln(num1+num2); readln; end. Output Enter a number?150 Enter another number?200 num1+num2=350
Arithmetic operators in Pascal
Arithmetic operator | Description |
---|---|
+ | Addition |
- | Subtraction |
/ | Division |
* | Multiplication |
mod | Remainder |
div | Whole integer when divide |
program PascalArithmetic; var num1: integer; num2:integer; begin write('Enter a number?'); readln(num1); write('Enter another number?'); readln(num2); writeln('Addition=' , num1+num2); writeln('Deduction=' , num1-num2); writeln('Multiplication=' , num1*num2); writeln('Division=' , num1/num2:10:2); writeln('Mod=' , num1 mod num2); writeln('Div=' , num1 div num2); readln; end. Output Enter a number?100 Enter another number?6 Addition=106 Deduction=94 Multiplication=600 Division= 16.67 Mod=4 Div=16
Relational Operators
Relational Operators are used to compare two values and evaluate it. The final result is true or false.
Logical Operator | Description |
---|---|
>= | Greater than or Equal |
>= | Greater than or Equal |
< | Less Than |
<= | Less than or Equal |
<> | Not Equal |
program PasRelationalOp; var num1:integer; num2:integer; begin write('Enter a number?'); readln(num1); write('Enter another number?'); readln(num2); writeln(num1,' > ',num2 , num1>num2); writeln(num1,'>=',num2 , num1>=num2); writeln(num1,'=',num2 , num1=num2); writeln(num1,' <>',num2 , num1 <>num2); writeln(num1,'<',num2 , num1 < num2); writeln(num1,'<=',num2 , num1 <= num2); readln; end. Output Enter a number?10 Enter another number?2 10 > 2TRUE 10 >=2TRUE 10=2FALSE 10 <>2TRUE 10 <2FALSE 10 <=2FALSE
Logical Operators
Logical Operator |
---|
not |
and |
or |
program PasLogicalOp; var num1:integer; num2:integer; begin write('Enter a number?'); readln(num1); write('Enter another number?'); readln(num2); writeln('> ' , (num1>50) and (num2>50)); writeln('>= ' , (num1>50) or (num2>50)); writeln('= ' , not (num1>50) ); readln; end. Output Enter a number?10 Enter another number?5 > FALSE >= FALSE = TRUE
Control Structure in Pascal
There are three types of control structure in any programming. They are
- Sequence
- Selection
- Repetition
Selection Control Structure (If .... then ..... else)
program PasSelection; var num1:integer; num2:integer; begin write('Enter a number?'); readln(num1); write('Enter another number?'); readln(num2); if num1>num2 then writeln('Number 1 > number 2') else write('Number 1 <= number 2'); readln; end. Output Enter a number?100 Enter another number?150 Number 1 <= number 2 program PasSelOddEven; var x:integer; begin write('Enter a number?'); readln(x); if x mod 2 = 1 then writeln('the number is odd....') else writeln('The nuber is even...'); readln; end. Output Enter a number?11 the number is odd.... program mark;{nested if} var m:integer; g:char; begin write('Enter your marks'); readln(m); if m>=75 then g:='A' else if m >65 then g:='B' else if m >=50 then g:='C' else if mgt;=35 then g:='S' else g:='F' ; writeln('your Marks is ',m); writeln('Your Grade is ',g); readln(); end. Output Enter your marks69 your Marks is 69 Your Grade is B
Selection Control Struture (case ... of)
program PasCase; var g:char; begin write('Enter your grade(A,B,C,S,F) ?'); readln(g); case g of 'A' : writeln('Excellent'); 'B' : writeln('Good'); 'C' : writeln('Great'); 'S' : writeln('Bad'); 'F' : writeln('Very Bad'); end; readln; end. Output Enter your grade(A,B,C,S,F) ?B Good
Control Structure Repetition/Loop/Iteration
Repetition (While)
program PasWhile; var x:integer; begin x:=1; while xlt;=10 do begin writeln(x); x:=x+1; end; readln; end. Output 1 2 3 4 5 6 7 8 9 10
Repetition (for)
program PasFor; var x:integer; begin for x:=1 to 10 do writeln(x); readln; end. Output 1 2 3 4 5 6 7 8 9 10 program PasFor; var x:integer; begin for x:=10 downto 1 do writeln(x); readln; end. Output 10 9 8 7 6 5 4 3 2 1
Repetition (repeat ... until)
program PasRepeatUntil; var x:integer; begin x:=1; repeat writeln(x); x:=x+1; until x >10; readln end. Output 1 2 3 4 5 6 7 8 9 10
Subroutine in Pascal
The subroutine perform a specific task which need to be done more than once at various points in the main program.
Advantage of subroutine
- * Easy to Test
- * Easy to maintain
- * Reuse
- * Less lines of code
- * Task can be shared
Procedure in Pascal without Parameter
program PasSub; procedure star(); begin writeln('*****'); end; procedure dash(); begin writeln('-----'); end; begin star(); dash(); star(); dash(); dash(); readln; end. Output ***** ----- ***** ----- -----
Procedure in Pascal with Parameter
procedure Mypat(x:char; y:integer); var i:integer; begin for i:=1 to y do begin write(x); end; writeln(); end; begin mypat('=',10); mypat('-',20); mypat('$',30); readln; end. Output ========== -------------------- $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Functions in Pascal
function AreaCircle(r:real):real; const pi=22/7; begin AreaCircle:=pi*sqr(r); end; begin; writeln(AreaCircle(10):8:2); writeln(AreaCircle(30):8:2); writeln(AreaCircle(100):8:2); readln(); end. Output 314.29 2828.57 31428.57
Array
An array is a data structure which stores elements of the same data type in a contiguous memory location with a name (array name). Its elements are accessed by using the index with the array name.
Arrays in Pascal
program PasArr; const L=2 ; var name: array[0..L]of string; marks1: array[0..L]of integer; marks2: array[0..L]of integer; i:integer; begin for i:=0 to L do begin writeln('Enter name,maths,science?'); readln(name[i]); readln(marks1[i]); readln(marks2[i]); end; writeln(); Writeln('Marks and Names are'); for i:=0 to L do begin write(name[i],' - '); write(marks1[i],' - '); write(marks2[i]); writeln; end; readln; end. Output Enter name,maths,science? Ganesh 90 88 Enter name,maths,science? Peter 100 90 Marks and Names are Ganesh - 90 - 88 Peter - 100 - 90
Note for Programmer
When you write program, follow certain disciplines. It will help to keep program maintainability. Some of them are:
- * Use meaningful variables and identifier names
- * Use indentation
- * Where ever Possible comment
- * Avoid goto statement
- * Limit Lines of Code (use modular approach)
No comments:
Post a Comment