Pascal Basics

02.34 Edit This 0 Comments »
Pascal Basics

Pascal is a high-level languages ​​are oriented at any destination, designed by prof. Niklaus Wirth from the Technical University in Zurich, Switzerland. The name pascal was taken as a tribute to Blaise Pascal, the famous mathematician and The philosophy of 17th century France.
Pascal 1.2 Program Structure
Structure of a Pascal program consists of a program title and a block of program or agency programs. Block program is further divided into two parts, namely: the declaration and the statement. In summary, the structure of a Pascal program can consist of:
1. Title programs
2. Block program
a. Section declaration
- Declaration label
- Constant declaration
- Type declaration
- Variable declaration
- Declaration procedure
- Function declarations
b. Part statement
The most simple pascal program is a program that consisted only of a part of the statement only.
Begin
End.
The general form of the statement are:
Begin
Statement;
End.
The basic concept of programming pascal
Algorithm & Programmers 2A
Example:
Begin
Writeln ('I pascal');
Writeln ('--------------')
End.
Output: I pascal
---------------
Pascal program does not know the rules of writing in a particular column, so it should be written from any column. Writing-statement statement in the example program that juts into several columns have no effect processed, only intended to facilitate the reading program, so it will be more visible parts, and good for documentation.
The title of the program is optional and not significant in the program. If written can be used to name the program and list of parameters about the communication program with its surroundings that are as documentation only. The title of the program if written must be located at the beginning of the program and end with a semicolon.

Example:

Program example;
Begin
Writeln ('Gunadarma');
Writeln ('---------------');
End.

Declaration
Declaration section is used when in the course use the identifier (identifier) ​​which may include labels, constants, types, variables, procedures and functions.
Constant Declaration
Definition of constants Const begins with the word backup followed by a set of identifier that is given a constant value. Constant data value is determined and certain, can not be changed within the program.
The basic concept of programming pascal
Algorithm & Programmers 2A
Example:
Contoh_konstanta Program;
Const
Discount = 0.2;
Salary = 25,000;
Namaperusahaan = 'PT ABC';
Begin
Writeln ('snippet =', piece);
Writeln ('Salary =', salary);
Writeln ('Name =', Namaperusahaan);
End.

Variable Declaration

Variables are identifiers that contain data that can be changed in value in the program. Using the word reserve title in the Var as variable declaration and is followed by one or more comma separated identifiers, followed by a colon and the type of data is terminated with a semicolon.

Example:
Var
Total, Salaries, Allowances: real;
Jumlahanak: integer;
Description: string [25];
Begin
Salary: = 500 000;
Jumlahanak: = 3;
Benefit: = 12:25 * Salary + Jumlahanak * 30 000;
Total: = Salary + Benefits;
Caption: = 'Employee Example';
Writeln ('monthly salary = Rp.', Salary);
Writeln ('Allowances = Rp.', Allowances);
Writeln ('Total wages = Rp.', Total);
Writeln ('Description = Rp.', Description);
End.

Type Declaration
Pascal provides several types of data:
1. simple data types, consisting of:
a. standard data types: integer, real, char, string, Boolean.
The basic concept of programming pascal
Algorithm & Programmers 2A
b. User defined data types: enumerated or scalar, subrange
2. Structured data types: arrays, records, files, sets
3. Pointer data type
Example:
Type
Fractions = real;
Round = integer;
Letter = string [25];
Begin
... ... ..
... ... ..
... ... ..
End.
Label Declaration

If the program using the GOTO statement to jump to a specific statement that it places a label on the statement is addressed and the label must be declared prior to the declaration. Using the word followed by a collection of backup label identifier label separated by a comma and terminated with a semicolon.

Example:

Label
Output: Language
Pascal
100, finished;
Begin
Writeln ('Language');
Goto 100;
Writeln ('Cobol');
100:
Writeln ('Pascal');
Goto done;
Writeln ('Fortran');
Finish:
End.

Declaration Procedures
The procedure is a separate part of the program and can be activated anywhere in the program. The procedure itself is made when the program will be divided into several blocks of the module. The procedure is made in the program by way of declarations section declare the procedure. Using the word backup procedure.
The basic concept of programming pascal
Algorithm & Programmers 2A
Example:
Procedure add (x, y: integer; var result: integer);
Begin
Result: = x + y;
End;
Output:
2 + 3 = 5
{Main program}
var
z: integer;
Begin
Add (2, 3, z);
Writeln ('2 + 3 =', z);
End.

Function Declaration

The function is also a separate part of the program similar to the procedure, but there are some differences. The word used backup function.

Example:

Function Add (x, y: integer): integer;
Begin
Add a: = x + y;
End;
{Main program}
Begin
Writeln ('2 + 3 =', Add (2, 3));
End.

Units

A unit is a collection of constants, data types, variables, procedures and functions. Each unit looks like a separate Pascal program. Unit standards is already a machine code (already compiled), rather than Pascal source code again and already placed in memory at a time using pascal. To use a unit, must be placed a block early Uses clause of the program, followed by a list of units used.

System Unit
Actually is a library of Turbo Pascal runtime that supports all the processes needed at runtime. System unit contains all the procedures and functions of the standard turbo pascal. The unit of this system will automatically be used in the program, so that may not mentioned in the Uses clause.
The basic concept of programming pascal
Algorithm & Programmers 2A

Crt Unit

Used to manipulate text layer (windowing, putting the cursor on the screen, color for text, code extanded keyboard and others). Crt standard unit can only be used by computer programs that use the IBM PC, IBM AT, IBM PS / 2 or compatible with it.
Example:
Uses crt;
Begin
Clrscr;
Writeln ('Hello');
Writeln ('-------');
End.
The standard procedure that uses crt unit are:
AssignCrt
HighVideo
Sound
ClrEol
InsLine
TextBackground
ClrScr
LowVideo
TextColor
Delay
NormVideo
Textmode
DelLine
Nosound
Window
GotoXY
Standard function that uses Crt unit are:
KeyPressed
ReadKey
WhereX
WhereY
1.4.3 Printer Unit
Represents a very small unit designed to use the printer in the program.
Example:
Uses printer;
Begin
Writeln (LST, 'Hello');
Writeln (LST,'-------');
End.
The basic concept of programming pascal
Algorithm & Programmers 2A

Unit Dos
Used when going to use the procedures and functions associated with the standard DOS calls, such DetTime, SetTime, DiskSize, DiskFree and others.
Example:
Uses Dos;
Begin
Writeln (DiskFree (0), 'bytes remaining contents of the disk');
End.

Unit Graph
provides a sophisticated set of regular graphs, so it can use it for graphing purposes.
For example:
Uses graph;
Var
DriveGrafik, ModeGrafik: integer;
I, x, y: integer;
Begin
DriveGrafik: = detect;
... ....
... ....
End.

0 komentar: