A/L 2011_revision. PASCAL PROGRAMMING page 1 of 8 Pascal is a high level programming language developed by Niklaus Wirth in 1970 based on the ALGOL programming language. It was named in honor of the French mathematician and philosopher Blaise Pascal. Pascal programs saved as *.pas. Variables store values/data Constants stay the same Control Flow change directions Procedures sub routines Functions sub routines returning a value Comments notes Structure of a Pascal program PROGRAM ProgramName (FileList); uses crt; (*importing libraries) const (* Constant declarations *) type (* Type declarations *) var (* Variable declarations *) (* Subprogram definitions *) begin (* Main Program statements *) end. program FirstProg; begin Writeln('Hello World!'); end.
22
Embed
A/L 2011 revision. PASCAL PROGRAMMING fileA/L 2011_revision. PASCAL PROGRAMMING page 1 of 8 Pascal is a high level programming language developed by Niklaus Wirth in 1970 based on
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
A/L 2011_revision. PASCAL PROGRAMMING
page 1 of 8
Pascal is a high level programming language developed by Niklaus Wirth in 1970 based on the ALGOL programming language. It was named in honor of the French mathematician and philosopher Blaise Pascal. Pascal programs saved as *.pas.
Variables store values/data
Constants stay the same
Control Flow change directions
Procedures sub routines
Functions sub routines returning a value
Comments notes
Structure of a Pascal program
PROGRAM ProgramName (FileList);uses crt; (*importing libraries)
const (* Constant declarations *)type
(* Type declarations *)var
(* Variable declarations *)(* Subprogram definitions *)begin (* Main Program statements *)end.
program FirstProg;
begin
Writeln('Hello World!');
end.
A/L 2011_revision. PASCAL PROGRAMMING
page 2 of 8
VARIABLES
Must begin with a letter
Can include alphanumeric characters and underscore (_).
vara, b : string;begin (* main *) read(a); readln(b); writeln(a) writeln(a)end.
A/L 2011_revision. PASCAL PROGRAMMING
page 6 of 8
CONDITIONALS RELATIONAL OPERATORS:
<>=
<=>=<>
less than greater than
equal to less than or equal to
greater than or equal to not equal to
5 < 72 > 14=3+12<=310>=5“A” <>”B”
IF … THEN
if BooleanExpression then
Statement1;
if A = 30 then Writeln('A is equal to 30');
IF … THEN …. ELSE
if BooleanExpression then StatementIfTrue
else
StatementIfFalse;
if A = 30 then
Writeln('A is equal to 30')
else
Writeln('A is not equal to 30');
A/L 2011_revision. PASCAL PROGRAMMING
page 7 of 8
NESTED IF
if Condition1 then
Statement1
else
if Condition2 then
Statement2
else
Statement3;
if A=30 then
Writeln('A is equal to 30');
else
if A=50 then
Writeln('A is equal to 50');
else
Writeln('A is not 30 or 50');
CASE
case selector of
List1: Statement1;
List2: Statement2;
...
Listn: Statementn;
otherwise Statement
end;
Choice := ReadKey;
case Choice of 'a': Writeln('You like apples'); 'b': Writeln('You like bananas'); 'c': Writeln('You like carrots');else Writeln('Your choice is not a, b or c');
A/L 2011_revision. PASCAL PROGRAMMING
page 8 of 8
LOOPINGFOR NEXT
for index := Low to High do statements;
sum := 0;
for count := 1 to 100 do sum := sum + count;
WHILE .. DO
while BooleanExpression do
begin statements;
end.
a := 5;
while a < 6 do
writeln (a);
a := a + 1
REPEAT … UNTIL
repeat statement1;
until BooleanExpression;
a := 5;
repeat a := a + 1; writeln (a);until a > 10
GOTO
LABEL 10; statements;
GOTO 10;
A/L 2011_revision. PASCAL PROGRAMMING
page 9 of 8
BREAK and CONTINUE
The Break command will exit a loop at any time.
program breakTest; var i: Integer;begin i := 0; repeat i := i + 1; break; Writeln(i);until i = 10;end.
program breakTest; var i: Integer;begin i := 0; repeat i := i + 1; continue; Writeln(i);until i = 10;end.
Program 1 will not print anything because break it exits the loop before it gets there.
In Program 2 continuecommand will jump back to the top of a loop
PROCEDURES & FUNCTIONSProcedure has the same basic format as a program:
procedure Name;
const (* Constants *)
Var(* Variables *)
Begin (* Statements *)end;
Parameters to the procedure passed in the heading
procedure PrintParameters (a, b : integer; c, d : real);
begin a := 10; b := 2; writeln (a, b, c, d)end;
FUNCTIONS
function functionName (parameter_list) : return_type; statements;
function add1toParameter(a) : integer;begin add1toParameter:= a + 1end.
BUILT IN FUNCTIONS
abs returns absolute valuearctan returns arctan in radianscos returns cosine of a radian measuresin returns sin of a radian measuresqr returns square (power 2) sqrt returns root (power 1/2)chr character with given ASCII value round returns round to nearest integer
A/L 2011_revision. PASCAL PROGRAMMING
page 11 of 8
MATH FUNTIONSABS(-6) the absolute value of -6; gives 6SQR(3) the square of 3; gives 9SQRT(9) the square root of 9; gives 3.0SIN(2) the sine of 2 radians measureCOS(2) the cosine of 2 radians measureLN(9) the natural logarithm of 9; gives loge 9ROUND(9.2) rounds a number; gives 9
Functions are used to do repetitive tasks to reduce repeating code.
program AddEmUpAgain;
function AddEmUp( a, b, c: integer ) : integer;
begin AddEmUp := a + b + c; end;
procedure PrintData( a, b, c: integer );
begin Writeln('The sum of a, b, and c is ', AddEmUp(a, b, c), '.'); end;
begin
PrintData(2, 3, 4);
end.
A/L 2011_revision. PASCAL PROGRAMMING
page 12 of 8
SAMPLE PROGRAMS
Find the sum and average of five numbers
program SumAverage;
const
NumberOfIntegers = 5;
var
A, B, C, D, E : integer; Sum : integer; Average : real;
begin (* Main *)
A := 45; B := 7; C := 68; D := 2; E := 34; Sum := A + B + C + D + E;
A, B, C, D, E : integer; Sum : integer; Average : real;
begin (* Main *)
write ('Enter the first number: '); readln (A); write ('Enter the second number: '); readln (B); write ('Enter the third number: '); readln (C); write ('Enter the fourth number: '); readln (D); write ('Enter the fifth number: '); readln (E);
Reads values from a fileWrite values to a fileOpens a file for reading Opens a file for writingAssigns a filename to a variableReturns TRUE when end of line is reachedReturns TRUE when end of file is reached
procedure DoTowers (NumDiscs, OrigPeg, NewPeg, TempPeg : integer);(* Explanation of variables Number of discs -- number of discs on OrigPeg OrigPeg -- peg number of the tower NewPeg -- peg number to move the tower to TempPeg -- peg to use for temporary storage*)
begin (* Take care of the base case -- one disc *)
if NumDiscs = 1 then writeln (OrigPeg, ' ---> ', NewPeg) (* Take care of all other cases *)else begin (* First, move all discs except the bottom disc to TempPeg, using NewPeg as the temporary peg for this transfer *)
DoTowers (NumDiscs-1, OrigPeg, TempPeg, NewPeg);
(* Now, move the bottom most disc from OrigPeg to NewPeg *)
writeln (OrigPeg, ' ---> ', NewPeg);
(* Finally, move the discs currently on TempPeg to NewPeg, use OrigPeg as the temporary peg for this transfer *)