“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
Pascal Programming Guide for Solving Problems
1. Write programs to solve problems
Pascal Programming
1
By Anutthara Senanayake
Lecturer
Sri Lanka International Buddhist Academy
2. IT Department @ SIBA Campus
Session 01
A problem
Input, Output and Process
Alternative solutions
Algorithm
Control Structures in Algorithms
To display Algorithms : Flowcharts & Pseudocodes
Pascal Programming
Why Pascal is more popular ?
Turbo Pascal
Identifiers
Reserved Words
Data Types
Variables
Constants
Program Structure
Control Structures
2
6. IT Department @ SIBA Campus
Algorithms
Definition
An algorithm is a
procedure or
formula for solving
a problem.
6
7. IT Department @ SIBA Campus
Control Structures in Algorithms
Sequence
Selection
Iteration
7
8. IT Department @ SIBA Campus
To display Algorithms : Flowcharts &
Pseudocodes
8
9. IT Department @ SIBA Campus
Pascal Programming
9
Pascal is a general-purpose, high-level
language that was originally developed by
Niklaus Wirth in the early 1970s.
It was developed for teaching programming
as a systematic discipline and to develop
reliable and efficient programs.
10. IT Department @ SIBA Campus
Why Pascal is more popular ?
Easy to learn.
Structured language.
It produces transparent, efficient and reliable programs.
It can be compiled on a variety of computer platforms.
10
12. IT Department @ SIBA Campus
Additional Notes
How to run turbo Pascal in Dos Box - Type following commands
Mount c C:TP
C:
Cd BIN
turbo.exe
12
13. IT Department @ SIBA Campus
Identifiers
Identifiers are names that allow you to reference stored
values, such as variables and constants.
13
14. IT Department @ SIBA Campus
Exercise 01
Select the valid identifiers from the given list.
1. Myname
2. @me2
3. me2
4. saman
5. my-add
6. my_add
7. $sea_u 14
15. IT Department @ SIBA Campus
Reserved Words
program, input, output, var, real, begin, readline, writeline , end
15
17. IT Department @ SIBA Campus
Variables
17
var myname : string
18. IT Department @ SIBA Campus
Constants
A const is a keyword
applied to a data type
that indicates that the
data is constant (does
not vary).
18
Const pi=22/7;
19. IT Department @ SIBA Campus
Operators
19
Arithmetic Operators
Relational Operators
Logical Operators
20. IT Department @ SIBA Campus
Program Structure
20
Program Name
Library
Main program starting
point
Main program ending
point
Output
21. IT Department @ SIBA Campus
Exercise 02
Write a Pascal program to calculate the sum of two integer
numbers.
21
22. IT Department @ SIBA Campus
Control Structures - IF…THEN…ENDIF
Exercise 03
Write a Pascal program to print the entered, number if it is
only a positive number.
22
23. IT Department @ SIBA Campus
Exercise 04
Write a Pascal program to print “Pass” if the mark is greater
than 50.
23
24. IT Department @ SIBA Campus
Control Structures - IF….THEN….ELSE…..ENDIF
Exercise 05
Write a Pascal program to find the maximum number out of
two given numbers.
24
25. IT Department @ SIBA Campus
Control Structures -Nested IF
Exercise 07
Write a Pascal program to print the correct grade for the
given mark. (mark >= 75 =>A, mark >=65=> B, mark >=50 =>C,
mark =>35=>S, else F)
25
26. IT Department @ SIBA Campus
Exercise 08
Write a Pascal program to display the day of the week when
the day number is provided.
26
27. IT Department @ SIBA Campus
Control Structures - CASE statements
Exercise 09
Write a Pascal program to print the grade according to the
criteria for the given mark.
0-34 => F , 35 – 49 =>S , 50 – 64 =>C, 65 – 74 => B, 75 – 100
=>A
27
28. IT Department @ SIBA Campus
Control Structures - FOR - DO
Exercise 10
Write a Pascal program to print numbers starting from 10 to
1.
28
29. IT Department @ SIBA Campus
Exercise 11
Write a Pascal program to calculate the sum and the average
of given 10 numbers.
29
30. IT Department @ SIBA Campus
Control Structures -WHILE DO
Exercise 12
Develop a computer based solution to receive a set of
positive numbers or 0 from user and display the total value.
Data input will stop when a negative value is entered.
30
31. IT Department @ SIBA Campus
Control Structures - REPEAT UNTIL
Exercise 13
Write a Pascal program to print the word “Pascal” 5 times.
31
32. IT Department @ SIBA Campus
Control Structures - Nested Control Structures
Exercise 14
Write a Pascal program to make a number series according
to the ascending or descending order
32
33. IT Department @ SIBA Campus
Exercise 15
Write a Pascal program to input numbers and calculate the
count of odd numbers and the count of even numbers.
33
35. IT Department @ SIBA Campus
Session 02
Arrays
Declaring Arrays
Assigning Values
Accessing Values
Sub Programs
Functions
Defining a Function
Function Declarations
Calling a Functions
Procedures
Defining a Procedure
Procedure Declarations
Calling a Procedure
35
36. IT Department @ SIBA Campus
Arrays
An array is a highly useful
data structure that stores
variable data having
the same data type
36
37. IT Department @ SIBA Campus
37
An array is said to be a static data structure because, once declared, its original
size that is specified by the programmer will remain the same throughout the
whole program and cannot be changed.
38. IT Department @ SIBA Campus
Declaring Arrays
Syntax
Var
<arrayName> : Array[n..m] of <Data Type>;
38
39. IT Department @ SIBA Campus
Assigning Values
Syntax
<arrayName>[index] := <relevant data>
39
40. IT Department @ SIBA Campus
Accessing Values
By using a for loop we can easily access the array
40
41. IT Department @ SIBA Campus
Exercise 01
Write a Pascal program to find the sum of a given integer array.
41
42. IT Department @ SIBA Campus
Sub Programs
• A subprogram is a program unit/module that performs a particular task.
• These subprograms are combined to form larger programs.
42
43. IT Department @ SIBA Campus
Pascal provides two kinds of subprograms:
Functions: these subprograms return a single value.
Procedures: these subprograms do not return a value
directly.
43
49. IT Department @ SIBA Campus
Procedures
Procedures are subprograms that, instead of returning a single value,
allow to obtain a group of results.
49