Chapter 3
Chapter 3
ESCF Constantine
Chapter 3:
Pascal Programming
(introduction and Pascal Language)
Semester 1
Level: First preparatory class
1 Introduction
In contemporary times, computers have become indispensable devices due to their versatility.They can
perform a variety of tasks like receiving data, processing it, and producing useful results. However,
being machines, they cannot perform on their own.They need to be instructed to perform even a
simple task or resolve a particular problem like addition of two numbers. Computers operate based on
a predefined set of instructions known as a computer program,which is expressed in a programming
language, providing a precise guide on how to execute a particular task.In this third chapter, we will
present the general notions of programming as well as how to solve a particular problem.
2 General notions
2.1 Programming
Programming, in the context of computer science and software development, refers to the process of
designing, writing, testing, and maintaining a set of instructions (an executable computer program)
that tell a computer how to perform specific tasks. Programming is a creative and problem-solving
activity that requires logical thinking and attention to detail. It is a skill that is used in various fields
for :
• Creating websites and web applications .
• Developing software applications for computers, smartphones, and other devices.
• Designing video games.
• Automating tasks and processes.
• Build robots and other machines.
2.3 Program
A computer program, often referred to simply as a ”program” or ”software,” is a set of instructions
written in a programming language that directs a computer to perform specific tasks or operations.
These instructions are designed to be executed by the computer’s central processing unit (CPU) and
other components, enabling the computer to carry out various functions.Thus, to design a program, a
programmer must determine three basic rudiments:
1
• The instructions io be performed.
• The order in which those instructions are to be performed.
• The data required to perform those instructions.
A collection of programs can be compared with a recipe book, where each recipe can be assumed as a
program. Every recipe has a list of ingredients (fixed data) and a list of instructions detailing exactly
what to do with those ingredients. When you follow a recipe, you are actually executing a program.
Hence, to perform a task using a program, a programmer has to consider various inputs of the program
along with the process, which is required to convert the input into desired output. Suppose we want
to calculate the sum of two numbers, A and B. and store the sum in C. here A and B are the inputs,
addition is the process and C is the output of the program (see Figure 1).
3 Problem-solving steps
Before beginning with the process of writing a program (coding). the programmer has to determine
the problem that needs to be solved.
The process of solving a programming problem involves several key steps as follow:
1. Understand the Problem: In this phase, the problem is analysed precisely and completely.
Based on one’s understanding, the programmer knows about the scope within which the prob-
lem needs to be developed.Therefore, the input data, expected output, and any constraints are
identified.
2. Algorithm Development: After analysing the problem, the programmer needs to develop
various solutions to solve the given problem. An algorithm is developed to depict the basic logic
of the selected solution. Algorithms are one of the most basic tools that are used to develop the
problem-solving logic.
Before converting the algorithms into computer program, it should be checked for accuracy. To
do this, test data need to be ‘walked through’ each step in the algorithm to verify that the
instructions described in the algorithm actually perform the required functions.
3. Code Implementation: After ensuring that the algorithm is ’correct’ and should work for both
normal and unusual data, the programmer begins translating the algorithm into actual code by
following the syntax and conventions of the chosen programming language.
4. Program Compilation: Compilation is the process of translating high-level programming
code written by a programmer into machine code that can be executed directly by a the CPU.
The compiler translates the source code into an object code or (binary code). Furthermore,
compilation primarily focuses on detecting syntax errors rather than semantic errors. Syntax
2
errors involve violations of the language’s grammatical rules, and they are the type of errors
that compilers are designed to catch during the compilation process. Examples of syntax errors
include missing semicolons, mismatched parentheses, or using incorrect keywords.
On the other hand, semantic errors are related to the meaning of the program. These errors
might not be detected by the compiler because the code is syntactically correct, but it may
not behave as intended. The compiler’s role is to generate the executable code based on the
syntactically correct source code, and it may not have the ability to catch all possible semantic
errors.
5. Testing: Testing is a crucial step in the process of solving a programming problem. Below are
general steps for testing the proposed solution:
• Test Cases Identification: the programmer determines a set of test cases that cover a
range of scenarios, including typical cases, edge cases, and any constraints mentioned in the
problem.
• Test Data Preparation : the programmer creates input data for each test case and En-
sures that the data is representative of different situations that the program may encounter.
• Test Cases Execution: the programmer runs the program with the prepared test data
and then Examines the output and compare it with the expected results for each test case.
• Errors Check: Runtime errors or exceptions should be identified and their causes investi-
gated and remedied. Logical errors should also be sought out as they may lead to incorrect
results.
• Boundary Conditions Test : The program should be tested with the minimum and
maximum values allowed by the problem constraints, where applicable.
• Review and refinement: in the end of testing process , the programmer reviews the test
results tests and refines the code as needed. then, he must re-run the tests to ensure that
modifications haven’t introduced new issues.
3
Part 2: Pascal language and Environment
1 Introduction
Programming languages are the languages that a user employs to interact with the computer. They
can be divided into three major categories:
1. machine language: The first language was binary, also known as machine language, which was
used in the earliest computers and machines. Therefore, every instruction and data should be
written using binary codes, that is, 1 and O. Machine language is also known as the ‘native’
language that gives basic instructions to the computer’s CPU.
2. Assembly language: the assembly language provides a mnemonic instruction (usually three
letters long) corresponding to each machine language instruction. The assembly language pro-
gram must be translated into machine code by a separate program called an assembler.
3. high-level language: High-level programming languages are designed to be more user-friendly,
offering a more abstract and readable syntax, and they often provide a higher level of abstraction
from the hardware. Furthermore, they need language translators such as compiler and interpreter
to translate the high-level code into machine code. High-level languages include Pascal, C, C++,
FORTRAN, Java, BASIC, and many more.
In this course, we choose to use PASCAL as a general purpose high level language
that was developed for teaching programming as a systematic discipline and to develop
reliable and efficient programs.
2 Pascal overview
Pascal programming language was originally designed by Niklaus Wirth around 1970 and named after
Blaise Pascal, a famous French Mathematician.
• Easy to learn.
• Structured language that Emphasize the orderly use of conditional and loop control structures.
• It produces transparent, efficient and reliable programs through clear syntax and data structures.
• It can be compiled on a variety of computer platforms.
• Data types: Pascal is a strongly typed language that offers several data types like, Integers, real
numbers, characters, strings, arrays, records, sets, and files.
• Control structures: Conditional statements (if-then-else), loops (for, while, repeat-until), case
statements.
4
• Functions and procedures: It supports structured programming through functions and procedures
allowing Code modularization for reusability and organization.
• Object-oriented features: It supports object oriented programming including object types, meth-
ods, and inheritance.
• High-level: it focuses on logic and problem-solving rather than machine-specific details.
• Recursion: it allows the programmers to define complex structured data types and build dynamic
and recursive data structures such as lists, trees and graphs.
3 Environment
To write a program in a given language, the programmer needs a set of software tools that facilitates
the development, testing, and debugging of its computer programs. This set of software tools is known
as programming environment that provides programmers with a user-friendly interface to write, edit,
compile, and manage their code efficiently.
There are several Pascal programming environment available for general use. Among these are:
Turbo Pascal, Delphi, Free Pascal and GNU Pascal.
In this course, we choose to use Turbo Pascal as a popular programming environment
for Pascal programming language.
Turbo Pascal was Developed by Borland, Turbo Pascal gained widespread popularity in the 1980s
and early 1990s. Here are some key features of the Turbo Pascal environment:
5
• Integrated Development Environment (IDE): Turbo Pascal provided a comprehensive IDE that
included a code editor, compiler, debugger, and other development tools in a single package.
This streamlined development and made it easy for programmers to manage their projects.
• Text-Based Editor: The code editor in Turbo Pascal was primarily text-based, allowing program-
mers to write and edit Pascal code using a simple and efficient interface.
• Compiler: Turbo Pascal included a fast and efficient compiler that translated Pascal source code
into machine code or executable files.
• Unit System: Turbo Pascal introduced a unit system that facilitated modular programming.
Units allowed developers to organize their code into separate, reusable modules.
• Portability: Turbo Pascal was initially developed for MS-DOS, but later versions included sup-
port for other platforms, including Windows. This increased the portability of Pascal programs
across different operating systems.
• Educational Use: Turbo Pascal was widely used in educational settings for teaching program-
ming. Its simplicity and integrated environment made it an excellent choice for introducing
students to programming concepts.
The Turbo Pascal Environment included a text-based code editor. The text editor is the home interface.
It consists of a page intended for writing the program and a menu bar (see Figure 4).
Turbo Pascal’s editor featured basic syntax highlighting. Different elements of the code,
ò such as keywords, comments, and variables, were displayed in distinct colors to improve
readability.
6
1. File Menu: From this menu (see Figure 5), the programmer can:
2. Edit Menu: From this menu (see Figure 6), the programmer can:
• Undo: Undo the last action.
• Cut / Copy / Paste: Cut / Copy / Paste the selected text.
3. Compile Menu : From this menu (see Figure 7), the programmer can:
• Compile: Compile the source code of the current page.
4. Run Menu : From this menu (see Figure 8), the programmer can:
• Run: Run the generated Object code after the last compilation.