Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Data in a program (variables and literals)

Programs and algorithms | Algorithms and programming languages | Fundamentals of programming I | Fundamentals of programming II | Write down and run this program on your computer | Write down and run this program on your computer | Introduction to objects | The first program | Types. Primitive types. | Types of variables. Declarations. |


Читайте также:
  1. A friend of yours wants to develop a programme to protect the city where he lives. Give him a piece of advice.
  2. Algorithms and programming languages
  3. Application Form for the Unilever Leadership Internship Program
  4. Basic features of database programs
  5. Calculation of a salary of the programmer
  6. Classes of programming languages
  7. Classification of data types in the programming language Java.

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

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:

 

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

 


Дата добавления: 2015-11-16; просмотров: 68 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Student is obliged to write down and run this program by himself| Operations on data (operators and expressions)

mybiblioteka.su - 2015-2024 год. (0.007 сек.)