[go: up one dir, main page]

0% found this document useful (0 votes)
24 views8 pages

Chapter 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

Chapter 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

People’s Democratic Republic of Algeria

Ministry of Higher Education and Scientific Research

ESCF Constantine

Chapter 3:

Pascal Programming
(introduction and Pascal Language)

Semester 1
Level: First preparatory class

Prepared by Doctor: M.MAATOUGUI ESMA


University year : 2024/2025
Part 1: Introduction to Programming

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.2 Programming language


A programming language is a formal system consisting of a set of rules and symbols used to create
computer programs. These languages enable humans to communicate instructions to computers, al-
lowing them to perform specific tasks or solve particular problems. Programming languages serve as
an intermediary between human understanding and the machine’s ability to execute instructions.
Each programming languages have a specific syntax rules governing how statements and expressions
should be structured and Semantics that define the meaning behind these structures.
There are several programming languages, each can be used to manage problems in a specific field:
multi media problems, mathematical problems, data management, creation and management of web
pages, etc.The choice of programming language depends on factors like the task at hand, performance
requirements, ease of development, and the specific features provided by the language. Programmers
often select languages based on the problem they are solving and their personal or organizational
preferences.

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).

Figure 1: Program performing a task.

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.

An algorithm is defined as a finite sequence of explicit instructions that when


ò provided with a set of input values produces an output and then terminates. To
be an algorithm, the steps must be unambiguous and after a finite number of
steps, the solution of the problem should be achieved. However, algorithms can
have steps that repeat (iterate) or require decisions (logic and comparison) until
the task is completed. Note that algorithms are not computer programs, as they
cannot be executed by a computer.

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.

Figure 2: Problem Solving Steps

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.

2.1 Why to use Pascal?


Pascal provides a teaching language that highlights concepts common to all computer languages. it
has grown in popularity in the teaching and academics arena for various reasons:

• 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.

2.2 Key Features of the Pascal programming Language


Pascal programming language has the following Key features:

• 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.

2.3 Translating High-level Language to Machine Language


Since computers understand only machine language, it is necessary to convert the high-level language
programs into machine language codes. This is achieved by using language translators or language
processors, generally known as compilers, interpreters or other routines that accept statements in one
language and produces equivalent statements in machine language.
A Pascal compiler is a software tool that translates source code written in the Pascal programming
language into machine code or an intermediate code that can be executed by a computer. The primary
purpose of a compiler is to convert human-readable source code into a format that the computer’s
hardware can understand and execute.
Compilation in Pascal primarily focuses on detecting syntax errors rather than semantic errors.
Nevertheless, Pascal compilers perform various levels of error checking, including syntax errors, type
errors, and other semantic issues. The goal is to catch potential problems in the code before it is
executed. Furthermore, since the compilation is a multi-stage process, the compiler typically goes
through several phases, including lexical analysis (breaking code into tokens), syntax analysis (parsing
the code structure), semantic analysis (checking for meaningfulness) and code generation (producing
machine code or intermediate code). If there is any error, the compiler generates an error message,
which is usually displayed on the screen. In case of errors, the compiler will not create the object code
until all the errors are rectified.

Figure 3: Working of a Compiler.

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.

The main menus are: File, Edit, Compile and Run.

Figure 4: Turbo Pascal Text Editor.

6
1. File Menu: From this menu (see Figure 5), the programmer can:

• New: Open a new editing page


• Open: Open an existing file
• Save and Save as: Save the content of the current page. The files created
with Pascal editor are typically named with the extension .pas.

• Exit: Leave the environment

Figure 5. File Menu.

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.

Figure 6. Edit Menu.

3. Compile Menu : From this menu (see Figure 7), the programmer can:
• Compile: Compile the source code of the current page.

Figure 7. Compile Menu.

4. Run Menu : From this menu (see Figure 8), the programmer can:
• Run: Run the generated Object code after the last compilation.

Figure 8. Run Menu.


It should be noted that most of the actions mentioned above can be performed via the menus or using
the keyboard shortcuts displayed on these menus. For example :
• Save → F2
• Compile → Alt +F9
• Run → Ctrl +F9

You might also like