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

2.3. Data in a program (variables and literals)

A program processes data. Therefore the data must be delivered to the program somehow. There are two ways for representing data in a program:

  • using literals

  • using variables

A literal is a string representing value of a data directly.

There are numeric literals, character literals and string literals. Numeric literal is direct notation of some number.

Remark 1 Notation of real numbers uses dot character as decimal separator (not comma). Leading zeros may be skipped. Remark 2 Many programming languages allow octal, hexadecimal or even binary notation of numbers besides decimal. Remark 3 Some programming languages permit additional symbols in numeric literals specifying number type (for example long integer or double precision real number). Sometimes scientific notation for real numbers is used (for example 3.1e-12 denotes 3.1 multiplied by 10 to -12 power.

Examples: 3 10 101.3 10356789.12796 0.3 .3 Character literal directly specifies one character, which must be enclosed in apostrophe. Examples: 'a' 'b' 'z' 'X' ''' '5' String literals are sequences of characters (strings).They are treated as text data, not language elements. Such sequences are enclosed in quotation marks. Remark about the REXX Examples "John has a dog" "say" Why characters and sequences of characters must be enclosed in apostrophes/quotation marks? The string say written down directly in a program is recognized as an instruction. Thus, wishing it to be treated as text data, one has to enclose it in quotation marks. For example, the following line displays the string "say" at the command prompt:

say "say";

Generally, strings not enclosed with the quotation marks and characters (in fact only letters) not enclosed with the apostrophes are recognized as variables or keywords (denoting statements or other language elements). The instruction:

a = b;

is recognized as assignment to variable a the value of variable b. To assign the character b to the variable a, we have to write:

a = 'b';

The following

x = if;

generates an error whether "if" is one of reserved words.

The keywords of a language - are the words having special meaning (for example denoting statements). Keywords might be reserved which protects them from using in other contexts than defined by the language syntax. (see more about reserved words)

Of course, literals are not enough to write non trivial programs. We need variables.

A variable is a symbol in program, specifying memory cell that stores some data. Thus:

  • a variable has a name

  • this name is used to access data bounded to the variable

  • the contents of the memory cell (variable's value) may vary during program's execution

Let's recall the problem of calculating of a computer price. In very simple form (imitating use of spreadsheet somehow) its solution could have been written as a single instruction displaying the sum of literally encoded in the program's source prices of its components. Let's assume that we use the following data: CPU price, motherboard price, memory modules price, hard disk drive price, monitor price, prices of other components and their sum.

say "The price of the computer is:";

say 700 + 500 + 350 + 440 + 1100 + 400;

As the result we obtain:

The price of the computer is: 3490

Student should write down and run this program by himself. This is not the proper way of programming. Changing prices of components (for some other configuration) we must remember where in the program these prices are encoded. Moreover, if we would like to count contribution of each component (per cent) to the final sum, we would have to encode each price twice and calculate the sum for the second time. Thus, even in such simple program one can't avoid using variables. Improved version of this program might look like this:

/* Calculation of the computer price */

cProc = 700;

cPly = 500;

cPam = 300;

cDysk = 400;

cMon = 1100;

cInn = 500;

cKomp = cProc + cPly + cPam + cDysk + cMon + cInn;

say "Initial computer price is :" cKomp;

say "Share of CPU price :" cProc/cKomp;

cProc = 300;

cPly = 400;

cKomp = cProc + cPly + cPam + cDysk + cMon + cInn;

say "After change of CPU and motherboard prices";

say "The price of the computer is :" cKomp;

say "Share of CPU price :" cProc/cKomp;

Student should write down and run this program by himself.

Picking a name for a variable one should:

  • consider the language restrictions

  • know, whether small and capital letters are distinguished

  • choose names, that are readable and have some content-related meaning

All prices are assigned to variables. Their names are chosen by the programmer, but each language imposes some restrictions in this matter (for example the name of a variable can't start with a digit and can consist of alphanumeric characters only; its length might be bounded). Some languages don't distinguish small and capital letters in names of variables (the name x specifies the same variable as the name X), other (Java) do (x is other variable then X). Let's go to semantics of our program. The notation: cProc = 500; means, the cProc variable obtains value literally expressed as number 500. This is called "assignment of a value to a variable". Since that moment cProc denotes the same value as 500. Instruction "say cProc" results in displaying the value of cProc variable, i.e. 500. This value can be modified in arbitrary moment in the program by assigning new value of CPU price: cProc = 300;. But what does the notation "cKomp = cProc + cPly + cPam + cDysk + cMon +cInn" mean? Why can we write "say cProc" and how is this interpreted? What does (apparently weird) notation "cProc/cKomp" mean? The next part answers the above questions.

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