Читайте также:
|
|
Program's data are subject to operations. There are many kinds of such operations:
Operations are expressed by means of operators of the language.
An operator is a special symbol of the language designed to carry out operations on data.
Each language has its own set of operators (symbols of operations). Common to most languages operators are depicted in the following table:
* | multiplication |
/ | division |
+ | addition |
- | subtraction |
< | less than |
<= | less than or equal |
>= | greater than or equal |
> | greater |
= | assignment |
The values upon which operation is carried out are called operands or arguments of the operator.
For operation of addition of value 1 to the variable x they are:
Left argument | Operator | Right argument |
x | + |
Expressions are made up of variables, literals and function calls with the help of operators and parentheses.
During execution of a program each expression is evaluated, i.e. its value is calculated (each expression has some value).
Assume that the x variable has a value of 100 and the a variable has a value of 2
Expression | Expression's value |
x | Value of the x variable (100) |
Value of the 1 literal (1) | |
x + 1 | |
a * 2 + 3 |
Parentheses should be used to determine the order of operators' application, especially when precedence of operators is not obviously clear.
Each operator is characterized by precedence. Precedence defines the order of the application of operators during evaluation of an expression. Those of higher precedence are applied first. This order may be changed with parentheses.
For example: * operator has higher precedence than + operator, therefore evaluation of a*2+3 expression gives 7 as result (with a = 2). To change the order of evaluation we must use parentheses:
a*(2+3)
This expression is evaluated the following way. The "*" operator has two operands: left consists of expression "a" (its calculated value is equal to 2), right is the expression "2+3" (its calculated value is equal to 5). The value of the left argument is multiplied by the value of the right argument. As result we get 10.
One of the most important operators is assignment operator. It allows setting values of variables.
For example:
cBase = 500; /* value of 500 is assigned to the cBase variable */
cProc = cBase; /* to the cProc variable is assigned the value of cBase variable (500) */
cProc = cBase - 100; /* cProc variable is set to the value of cBase minus 100 */
cProc = cProc + 200; /* cProc gets incremented by 200 */
here cProc = 600
Now the source of the program calculating the computer price should be clear. We find application of arithmetic operators there (addition and division) and also assignments. However, some statements might still be unclear:
say "Share of the CPU price: " cProc/cKomp;
Not only the division takes place here, but also concatenation of strings.
Student shouldn't get accustomed to REXX's concatenation operators, because string concatenation in Java is much simpler and a bit different, although there are some similarities. In particular Java has the + operator, which behaves exactly the same way as the || operator in REXX. Also - though Java is a strongly typed language - variables of string type can be concatenated with variables of any other type producing string as a result.
Concatenation of strings in REXX can be realized in three ways:
In REXX, by concatenation, all data (including numbers) are treated as strings. see additional explanation
If, for example numeric variables a and b have values 100 and 50, and aString variable stores the text "abc", then:
REXX expression | Result | Java code |
"cat" a/b | cat 2 | "cat" + " " + a/b |
aString||a | abc100 | aString + a |
a||b | "" + a + b | |
"cat"aString | catabc | "cat" + aString |
Continuing explanation of instruction
say "Share of CPU price: " cProc/cKomp;
we can state, that "say" instruction has a form
say expression;
where expression is any expression, which is evaluated and its value (treated as a string) is displayed at the command prompt.
In the example under consideration, to the string enclosed in quotation marks the space character is appended and after that - the result of division of the value of variable cProc by the value of variable cKomp.
Comparison operators (relational) have a special property. The result of applying such operator can take one of two values: true or false. Different languages use different conventions to denote these values. In some cases (as in Java) they are values of special type denoted by the keywords true and false. In other cases (as in REXX) they are denoted by numbers: 1 (true) and 0 (false).
Application of relational operators may be associated with the answer to the question: is the condition described by the operator fulfilled or not?
Example:
x > 5
question: is the value of the x variable greater than 5?
answer: yes/no given as result 1/0 or true/false
x <= y
question: is the value of the x variable less than or equal to the value of the y variable?
answer: yes/no given as result 1/0 or true/false
equality operators are a bit embarrassing. The natural notation used for such operator is the '=' character, as in REXX.
So we write a = b, to check whether the value of the a variable equals to the value of the b variable. But the '=' character means also assignment! How to differ the two possible meanings?
In REXX it depends on the context of occurrence of the symbol.
If we write it at the beginning of the line, or after the semicolon:
a = b;
then it is treated as an assignment, not as an expression that has some value.
If, on the other hand, the same notation is used in place where value of some expression is expected:
if (a = b)
say a = b
then equality sign is treated as equality operator (not assignment), and the expression "a = b" gives 1 or 0 as the result (the answer to the question: is a equal to b?).
In other languages (C, C++, Java and others) there exists strong distinction between assignment and equality operators. The latter has a form: == (two equality signs). Owing to that the language semantics is (in this context) uniform: assignment is an expression as well and has a value (of the right hand side of the assignment operator).
Unfortunately it has some drawbacks too (see about side effects).
Equality operator | |
In REXX | In Java |
= | == |
Example: a = b Meaning: is a equal to b? result: 1 (yes), 0 (no) | Example: a == b Meaning: is a equal to b? result: true (yes), false (no) |
negated equality operator is obtained by putting negation sign in front of equality sign '='. In REXX the role of negation sign plays '\' (backslash) character. In C, C++ and Java this is '!' character (exclamation mark).
Example:
REXX: a \= b (result: 1 if a is not equal to b, 0 in other case)
Java: a!= b (result: true if a is not equal to b, false in other case)
Negation sign is a logical operator specifying logical negation.
Logical operators are designed to build logical expressions with the help of conjunction and alternation.
In REXX the logical conjunction operator is denoted by &, whereas alternation BY | (in C, C++ and Java the following sequences of characters are used instead:&& and ||).
Detailed treatment of this subject is deferred to the lecture discussing Java operators and expressions.
Summary
This lecture presents (using simplified and modified version of the REXX language):
The student should pay special attention to the following important notions:
Having read this lecture, the student should acquire the ability to write simple programs in modified REXX, which for example perform arithmetical operations on some data.
Exercises
All exercises should be done as programs written in modified REXX.
Lecture 3
Дата добавления: 2015-11-16; просмотров: 59 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Data in a program (variables and literals) | | | Fundamentals of programming II |