Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lecture 1.docx
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
704 Кб
Скачать

If (condition) then ... Or if (condition) then ... Else ...

and iteration loops (i.e. repeated execution of fragments of the algorithm)

execute until (condition) ... execute modifying value of the variable i from p to l...

Whereas input and output of data can be expressed by means of words: read, write. Applying symbols +, - i * for expressing operations of addition, subtraction and multiplication, parentheses (like in mathematics) for grouping of operations and special words for expressing actions and decisions, algorithm for calculation of tax may be written down as follows:

read income

if (income > 74048) then tax = 17048.44 + 0.4 * (income - 74048)

else if (income > 37024) then

tax = 6541.24 + 0.3 * (income - 37024)

else tax = 0.19 * income - 493.32

write tax

Such notation of this algorithm demonstrates two problems. Firstly, error in input data changes sequence of steps carried out by the algorithm and causes return to the instruction reading input data. In pseudocode such action is written down as jump to specific fragment of the algorithm (go to ...), denoted by a label (labels are words ended with colon ':'). By the way let's introduce to the pseudocode braces ("{", "}") for grouping of actions; for example:

if (condition) then {   action 1   action 2 }

If condition is fulfilled, actions grouped inside the braces are executed.

dataInput1:

write "Give the price of the CPU"

read CPUprice

if (CPUprice is not a number) then {

write "Wrong data"

go to dataInput1

}

dataInput2:

write "Give0 the price of the motherboard"

read MBprice

if (MBprice is not a number) then {

write "Wrong data"

go to dataInput2

}

...

result = sum of prices

write result

However such notation causes, that algorithms (and programs) are hardly readable, their logic complicated and are subject to errors. That's why most of programming languages do not use the go to (goto) instruction any more. Instead iteration loops are applied. For this reason flow charts have lost popularity too (it turns out, that flow charts are not always translatable into languages without the "goto" instruction). Thus, the algorithm for calculating computer price should be expressed in other form. Let's use iteration loops and introduce boolean (logical) variable named dataRequired with 2 possible values yes and no.

dataRequired = yes

execute until (dataRequired) {

write "Give the price of the CPU"

read CPUprice

if (CPUprice is not a number) then write "Wrong data"

else dataRequired = no

}

  

dataRequired = yes

execute until (dataRequired) {

write "Give the price of the motherboard"

read MBprice

if (MBprice is not a number) then write "Wrong data"

else dataRequired = no

}

...

result = sum of prices

write result

At the beginning, the value of the variable dataRequired is yes and the condition in execute until is fulfilled. Therefore execution of the instructions grouped between the braces starts. If the data read (CPUprice) is not a number, the message "Wrong data" is displayed. The value of the variable dataRequired does not change then and the actions grouped between the braces are executed again (because the condition in execute until is still fulfilled). On the contrary, if CPUprice is a number, the variable dataRequired gets the value of no in which case the condition in execute until is not fulfilled anymore and the actions between the braces are not executed. The algorithm continues execution from the instruction following right brace '}' - it reads the price of the motherboard. Another problem we encounter in this algorithm is repeating similar (almost identical) actions. Consider reading price of CPU, motherboard and other components - all of these operations look similarly. For this reason we might isolate these actions and write them down in form of so called procedure or function. Written down once they may be executed repeatedly for various components of a computer. This means, that problem of calculating the price of a computer is divided into two sub problems:

  • input and verification of data

  • actual calculations

Each of these problems may be solved separately, concentrating on features specific to its context. This way of programming is called structural programming. The following lectures introduce the notion of function and their usage in programs. To sum up:

  • Programs are created to solve problems or realize some tasks.

  • Before writing a program which realizes some task, an algorithm solving the problem must be worked out.

  • An algorithm is a recipe, set of commands executed upon data, description of the transformation of input data into output data.

  • A program is recording of algorithm and data in the given programming language.

  • For a program to be executed, its representation in a programming language has to be translated into the machine language understandable by the CPU. This work is done by specific programs called translators, compilers or interpreters.

Note that programs do not only reflect steps (commands, actions) of algorithms, but also represent processed data. These data might be represented as single separate copies or grouped into sets (tied somehow and/or ordered somehow). In such case we can speak of data structures. Thus, another definition of program can be quoted (by N.Wirth):

PROGRAM is a concrete formulation of an abstract algorithm based on specific representation of data structure.

Both definitions are consistent. The first, quoted at the beginning of the lecture ("program is encoded sequence of instructions for the CPU or other hardware devices") stresses actions executed by a program, the second ("concrete formulation of an abstract algorithm") stresses creation of a program. The text of the program (its source code) is written in a programming language. Each programming language has its alphabet - set of characters (letters and digits) from which symbols of the language (sequences of characters) are built. Syntactic rules define acceptable ways for building symbols and acceptable order of their occurrences in a program. Semantics defines meaning of symbols. For example in a programming language whose alphabet is built from letters, digits and special characters, the names of variables are constructed from letters and digits. Some character sequences denoting language instructions are reserved ("if"). The manner of joining symbols is specified (for example formula "if (a == b) a = 0;" is syntactically correct whereas formula "if a =b a =0" is not) and the meaning of sequences of symbols is defined (for example formula "a = 3" means assigning value of 3 to the variable "a"). There exists many (tens of thousands) programming languages. They may be classified using various criteria. Undoubtedly the most important is logical structure of the language and the art of creating programs using it. Imperative languages require a specification of sequence of steps that realize given task, whereas declarative languages describe relations between data in terms of function (functional languages) or rules (relational languages, logical programming languages), and result of execution of a program is obtained by means of applying to the specified relations special "built in language" algorithms. Object Oriented Programming consists in combining data and operations on these data, what gives an opportunity to create and use in programs new data types better reflecting domain of a given problem. Procedural programming (sometimes associated with imperative programming) separates data and functions, not supplying simple and adequate ways of reflecting domain of a problem in data structures it uses. Examples of procedural languages are ALGOL, FORTRAN, PL/I, C. Object oriented languages are Smalltalk, Java, C++ and C#. The best known functional language is Haskell, and logical programming language is Prolog. Other division concerns methods in which program's source code is transformed into CPU instructions. Two classes can be differentiated here: compiled and interpreted languages.

A compiler translates program's source code into the CPU's instructions, checking its syntactic correctness and informing about errors. Thus, compilation consists in translation of languages and syntax verification.

In compiled languages program's text (the source code) is translated into binary (intermediate) code by a specific program called compiler. Another (in most cases) program, usually called linker generates from intermediate code binary executable of the program (which is ready to run) and stores it on the hard disk as an executable file (for example with "exe" extension or with executable attribute set). This is how "C" or "C++" languages work. In other cases compiler produces symbolic code, which is executed by means of interpretation by the program called interpreter. The Java language behaves like this.

An interpreter executes source code directly. Therefore syntactic verification takes place while program is running. Some interpreted languages compile to intermediate code allowing syntactic verification prior to running.

Source or intermediate code of interpreted languages is read and executed by a specific program called interpreter, which generates (on the fly) the CPU instructions corresponding to the instructions found in the source. Examples of interpreted languages are: REXX, ObjectREXX, Perl, PHP. To sum up: process of programming might be depicted using the following algorithm:

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]