Course : Simple Pascal Programming / PDF
Course : Simple Pascal Programming / PDF
Sample of the PDF document
A program is a list of
instructions written in predefined language.
A program aims to teach the computer to solve a problem step by step
following a user-designed algorithm.
A. Structure
of a Simple Pascal Program
PROGRAM
FtoC(input,output);............................Program heading
USES WinCrt; Display
results in windows
VAR
fahr, celsius :
Real;...............variable declaration
BEGIN
Writeln('Temperature
Converter');
Writeln('Fahrenheit to Celsius');
Write (‘Temperature in degree Fahrenheit?
’);
Readln (fahr);
celsius:=((fahr
- 32)*5)/9;
Writeln('Equals
', celsius:4:1,' degrees Celsius');
Writeln;
END.
1. Program
Heading
Program heading consists of the keyword PROGRAM, the name of the program,
the program parameter.
2. Variable
Declarations
You should tell
the computer the variables that you would use in your program. Then the
computer will reserve space in RAM to store these variables. Variable
declaration consists of the keyword VAR, the variables and their types.
Multiple variables of the same type can be declared in the same statement.
Data
type for variables: integer, real, char, string
3. BEGIN
and END
BEGIN and END signal respectively
the beginning and the end of a statement block.
4. Input and Output
Statement
Writeln (‘XXXXX’) can output a
statement on the VDU.
Readln(fahr) will read a data
entered by the keyboard into the variable identifier.
5. Assignment Operator
(:=)
Once a variable is declared, you
may assign it a value using the assignment operator (:=). The assigned value
can be a constant, another variable, or an expression. A variable must be put
in the left-hand side of an assignment operator.
a
:= 30;
Celsius
:=((fahr - 32)*5)/9;
Arithmetic signs: +, -, *, /
6.
Output format
Writeln('Equals ', celsius :4 :1,'degrees
Celsius');
|
7. Punctuation
The semicolon ( ; ) is used to separate
statements.
The full stop ( . ) is used to indicate
the end of the program.
* Try to find out the
different in output format of using “writeln” and “write”.
Exercise 1
1.
Write a PASCAL program to
calculate the area and circumstance of circle by inputting the radius of the
circle.
Given:
1.
Variables names: radius, pi, area, circumstance with data type
: real
2.
pi := 3.1416
3.
Sample Output:
Please enter the radius? 8
The area of the circle is 201.1
The circumstance of the circle is
50.3
B. Condition/Selection
Statement
IF condition is true THEN do statement one ELSE do statement two;
I. Logical Operators
=,>,<,>=,<=,<>
, AND, OR
Example 1:
USES
WinCrt;
VAR
num : Integer;
BEGIN
Write('Input a number : ');
Readln(num);
IF num > 0 THEN
Writeln(num,' is positive.');
Writeln('Goodbye.')
END.
Example 2:
USES
WinCrt;
VAR
num : integer;
BEGIN
Write('Input a
number : ');
Readln(num);
IF (num >=
1) AND (num <=10)
THEN Writeln(num,'
is between 1 and 10 inclusively.');
Writeln('Goodbye.')
END.
Example 3:
USES
WinCrt;
VAR
num : Integer;
BEGIN
Write('Input a
number : ');
Readln(num);
IF
num>0 THEN Writeln(num,' is positive.')
ELSE
Writeln(num,' is non-positive. ')
END.
Course :Simple Pascal Programming / PDF
0 commentaires: