Sunteți pe pagina 1din 20

CSEC IT WORKSHOP

Syllabus concern areas & Pascal programming


Part A

August 4-6, 2014


ABOUT PASCAL
Pascal has grown in popularity in the teaching and academics
arena for various reasons:
 Easy to learn.
 Structured language.
 Produces transparent, efficient and reliable programs.
 Can be compiled on a variety of computer platforms.

Pascal has the following features:


 Pascal is a strongly typed language.
 Offers extensive error checking.
 Has several data types like arrays, records, files and sets.
 Includes a variety of programming structures.
 Supports structured programming – functions / procedures.
 Supports object oriented programming.
PROGRAM STRUCTURE
A Pascal program basically consists of the following parts:
 Program name
 Uses command
 Constants declarations
 Variables declarations
 Main program block
 Statements and Expressions within each block – generally separated by
semicolon (;)
 Comments

Program {name of the program}


Uses {comma delimited names of libraries you use}

Const {global constant declaration block}


Var {global variable declaration block}

Begin { main program block starts}


...
End. { the end of main program block }
SIMPLE PROGRAM
standard
Program test; library

Uses crt; Programming style…


• Upper/lower case
• Indentation
• Comments
Begin
Writeln(‘Good Afternoon!’);
End.
RESERVERD WORDS

These are the common reserved


words of Pascal program. You
cannot use this as a variable names.

begin end program var string


if then else for to downto
while do repeat until
procedure function in
DATA TYPES

These are the common data type of Pascal program.


Additional
Type Explanation Example
explanation

integer Whole numbers 3, 104, 0, -9

string String variable (Text) 'Hello World', '456,4'


char Character (One character) 'b', 'A', '7'
Can only be
boolean Boolean variable True, False
True or False
(Floating point
real Real numbers 4.0, -0.08, 48.6, 2.0E4
numbers)
COMPARING VB WITH PASCAL

VB: Dim i as integer


Pascal: var i : integer;

VB: i = 10
Pascal: i := 10;

VB: ‘comment
Pascal: {Comment}/(*Comment*)
DECLARING VARIABLES
Program test;
Uses crt;

Var i : integer;
s : string;
c : char;
b : boolean;
r : real; Output?
Begin
i := 0;
i := (i * 3) + 5;
Writeln(i);
End.
USING VARIABLES
Program test;
Uses crt;

Var i : integer;
j : integer;

Begin
i := 7;
j := 3;
i := i + j;
Writeln(i);
End.
SIMPLE PROBLEM EXAMPLE Pascal
Fees are charged to attend the cinema as
follows: Balcony - $8.00, House - $7.00.
Input the number of persons sitting in the
balcony and the number sitting in the house.
Calculate and print the total revenue Declare all
variables, group by
collected. type, separate with
commas

BASIC

• Use of semicolons
• Write vs Writeln (Read)
APPOINTMENT CLOCK
1. A customer pays a monthly fee of $37.50 for the telephone
service and $3.50 per minute for long distance calls.
Input the number of minutes for long distance calls.
Calculate and print the total bill.
2. Input the length and width of a room. Calculate and
print the cost of carpeting the room. One square meter of
carpet costs $45.00.

 3 o'clock appointment
Skip
 Discuss, share, solve

 Pascal working solution – include comments

 Present to audience
DECISION MAKING
if - then statem An if - then statement consists of a boolean expression
ent followed by one or more statements.

If condition Then S

Where condition is a Boolean


or relational condition and S is
a simple or compound
statement.

If (a <= 20) Then


c:= c+1;
DECISION MAKING
If-then-else sta An if - then statement can be
tement followed by an optional else
statement, which executes when the
boolean expression is false.

If condition Then S1 Else S2;

If color = ‘red’ Then


Writeln('You have chosen a red car')
Else
Writeln('Please choose a color for your car');
COMPARING VB WITH PASCAL
Pascal:
VB:
Uses crt;
Dim j as integer
Var j : integer;
j = 10
If j = 10 then
Begin
print “J = 10”
j := 10;
Else
If j = 10 Then
print “J <> 10”
Writeln(‘J = 10’)
End If
Else
Writeln(‘J <> 10’);
End.
Program ifelseChecking; IF-ELSE EXAMPLE
Var
{ local variable definition }
a : integer;

Begin
a := 100;
(* check the boolean condition *)
If( a < 20 ) Then
(* if condition is true then print the following *)
Writeln('a is less than 20' )
Else
(* if condition is false then print the following *)
Writeln('a is not less than 20' );
Writeln('value of a is : ', a);
Readln;
End.
Program ifelse_ifelseChecking;
Var
{ local variable definition } IF-ELSE-IF EXAMPLE
a : integer;
Begin
a := 40;
(* check the boolean condition *)
If (a = 10) Then
(* if condition is true then print the following *)
Writeln('Value of a is 10' )
Else If ( a = 20 ) Then
(* else if next condition is true *)
Writeln('Value of a is 20' )
Else If( a = 30 ) Then
(* else if next condition is true *)
Writeln('Value of a is 30' )
Else
(* if none of the conditions is true *)
Writeln('None of the values is matching' );
Writeln('Exact value of a is: ', a );
Realn;
End.
WORD OF CAUTION…
When using more than one
statement in TRUE or FALSE
If i = 10 Then part of IF construct
If j = 10 Then • Enclose statements inside
Writeln(‘i = 10 and j = 10’) Begin-End block and use
semicolons as normal
Else
Writeln(‘i = 10 and j <> If condition Then
10’); Begin
Else statement 1;
Writeln(‘i <> 10 and j <> 10’); statement 2;
End;
Wrong semicolon
Else
(Syntax Error)
Begin
statement 1;
statement 2;
End;
CONDITIONAL PROBLEM EXAMPLE
A technician charges fees as follows: 2 hours or less at $90.00 per hour, up to 6 hours at
$75.00 per hour and over 6 hours at $60.00 an hour. Read the number of hours worked
then calculate and print the fee charged.

Analysis
o 2 scenarios
• Use one If with Else If – Else
• Use three If

o hours <= 2 use rate of $90


o hours >= 6 use rate of $60
o otherwise use rate of $75

OR

o hours <= 2 use rate of $90


o (hours > 2) AND (hours < 6) use rate of $75
o hours >= 6 use rate of $60
Pascal
CONDITIONAL
PROBLEM
EXAMPLE

A technician charges fees as


follows: 2 hours or less at
$90.00 per hour, up to 6 hours
at $75.00 per hour and over 6
hours at $60.00 an hour. Read
the number of hours worked
then calculate and print the
fee charged.

Formatting
APPOINTMENT CLOCK
1. A bonus of $150.00 is given to employees who were absent for less
than 4 days in a year. The bonus is added to the salary to arrive
at the total income. If the total income is greater than $2,000.00, a
tax of 5% is deducted. Input the days absent and the salary.
Output the total income, the tax deducted and the income less tax.
2. A customer gets a 10% discount if the total spent is greater than
or equal to $1,000.00. Otherwise, the discount amount is 5% of
the total. Input the price and quantity of items purchased.
Calculate and print the total amount due, the amount of
deduction and the final amount after the deduction.

 9 o'clock appointment
 Discuss, share, solve

 Pascal working solution – include comments

 Present to audience

S-ar putea să vă placă și