25
Operaciones y Variables Cap2 , Cap4

Operaciones y Variables Cap2, Cap4. Operation Operator Addition + Subtraction - Multiplication * Division / Modulus Division % Theses operators are binary,

Embed Size (px)

Citation preview

Operaciones y Variables

Cap2 , Cap4

Operation Operator

• Addition +

• Subtraction -

• Multiplication *

• Division /

• Modulus Division % Theses operators are binary, meaning

they require two operands (literals or literal identifiers).

• The syntax template for connecting two operands in a simple arithmetic binary expression takes the form: – operand operator operand.

• For example, the expression, 3 + 5, is a syntactically correct implementation of the addition operator.

• The cout object can accommodate both the evaluation and display of arithmetic expressions. Refer back to the example immediately above. We may, if we choose, include interactive dialog and an arithmetic expression in one line. See sample code below:

• cout << “The sum of 3 and 5 is “ << (3 + 5) << endl;

the endl object

• The action of the endl object first causes a newline character ('\n') to be inserted into the display and then forces all of the current insertions to be displayed immediately.

Expression Types

• An expression is any combination of operators and operands that can be evaluated to yield a value.

• The operands in an integer expression consist exclusively of integers.

• The operands in a floating-point (or real) expression consist exclusively of floating-point values.

• Mixed-mode (integers and floating-point values) evaluate to double precision values.

Integer Division

• Integer division may be a little unfamiliar to beginning programming students. The whole number part is entirely consistent with elementary arithmetic.

• However, Although it may come as no surprise that 9/3 is 3,

the fractional part (remainder) obtained when two integers are divided is always dropped (truncated).

it may seem a little disconcerting to some that 10/3 is also 3 (given the constraints of integer division).

modulus

• C++ does capture the remainder value of integer division with a modulus (remainder) operator: %.

• Thus, 10%3 is 1.

Negation

• Negation of a number is accomplished by prefixing a value with the negative sign (-). Thus, -10 represents a negative integer. The negation operator is one of a category of C++ unary (single value) operators.

Operator Precedence and Associativity

• Operator precedence rules define an operator hierarchy. There are three levels of precedence:

• P1—All negations are done first. • P2—Multiplication, division, and modulus operations are

computed next. • P3—Addition and subtraction are computed last. • Associativity rules define the sequence of operations on

any given level of the hierarchy. Level P1 associates from right to left. Levels P2 and P3 associate from left to right.

There are four other issues related to operator placement and parentheses which impact order of operations

• 1. Two binary arithmetic operator symbols must never be placed side by side.

• 2. Parentheses may be used to form groupings; all expressions enclosed within parentheses are evaluated first.

• 3. Parentheses may be nested, in which case operations are performed from the inner to outer parenthesis.

• 4. Parentheses cannot be used to indicate multiplication; rather, the multiplication operator, *, must be used.

For example,

• the expression 75/25 + (65 – 20) % 4 * 8

evaluates to 11.

Assignment Operations

• Assignment statements are necessary for both performing computations and storing values in variables. The syntax template may be viewed below:

• variable = expression; • As previously noted, the assignment operator

instructs the CPU to evaluate the expression on the right for subsequent storage in the memory cell referenced by the variable on the left. Assignment operations may be performed repeatedly against the same variable.

Assignment Operations

• For example, consider the following initialization:

• float total = 345.55;

• The variable total is like a container that may be emptied and refilled repeatedly by any number of subsequent assignment statements, such as

• total = 232.77;

• Given the same precedence and right to left associativity, the evaluation of a multiple assignment such as

• a = b = c = 25;

• proceeds as

• a = (b = (c = 25));

• .

• Finally, as we have witnessed in previous code samples, when a variable name is sent to cout, the value stored in the variable is placed on the output stream and displayed.

Assignment Operations

• Assignment statements perform computations• Assignment statements store values in variables• Syntax template and examples:

– variable = expression; – total = subTotalOne + subTotalTwo– cylinderVolume = PI* pow (radius, 2)* height ;

• Precedence: lower than arithmetic operators• Associativity: right to left

Coercion• Coercion: conversion across assignment operators• Cause: Data-type difference around = operator• Action: Type on right converted to type on left• Examples:

– someFloat = someInt + 5.6;– someInt = someFloat + 10;

• Analysis – First example: no loss of data– Second example: data may be truncated

Assignment Variations

• Assignment operators are not algebraic equal signs• rvalue: value of expression to right of = operator• lvalue: address of variable to left of = operator • rvalue and lvalue of same variable may be used

– Example1: sum = sum + 10; – Example2: sum += 10;

• Variations: += = *= /= %=

#include <iostream>using namespace std;

int main(){ int sum;

sum = 25; cout << "The number stored in sum is " << sum << endl; sum = sum + 10; cout << "The number now stored in sum is " << sum << endl; system("PAUSE"); return 0;}

Assignment Variations

• To further underscore the differences between the differences between algebraic equal signs and C++ assignment operators, we demonstrate the manipulation of lvalues and rvalues in a single statement. Consider the following pair of statements:

int sum = 40; sum = sum + 15; • The second statement adds 15 to the variable contents of sum, the rvalue,

and then stores the result in sum’s lvalue, or memory address. The new value (rvalue) in sum is 55. Assignment expressions like sum = sum + 15, which use the same variable on both sides of the assignment operator, can be written using the following shortcut assignment operators:

– += != *= /= %= • To further condense our assignment variation above, we may write: sum +=

15 (sum “plus equals” 15).

Accumulating The assignment variation, sum = sum + 15, is known as an accumulator. It may be used in a series of statements or within a repetition structure to add (accumulate) subtotals. The syntax template of the accumulating statement has the form: variable = variable + newValue;

For example, consider the effect of the three following statements: int sum = 0; sum = sum + 15; sum = sum + 45; The last value of sum is 60.

Ver demo

#include <iostream>using namespace std;

int main(){ int sum;

sum = 0; cout << "The value of sum is initially set to " << sum << endl; sum = sum + 96; cout << " sum is now " << sum << endl; sum = sum + 70; cout << " sum is now " << sum << endl; sum = sum + 85; cout << " sum is now " << sum << endl; sum = sum + 60; cout << " The final sum is " << sum << endl; return 0;}

#include <iostream>using namespace std;

int main(){ int sum;

sum = 0; sum = sum + 96; sum = sum + 70; sum = sum + 85; sum = sum + 60; cout << "The value of sum is initially set to " << sum << endl; cout << " sum is now " << sum << endl; cout << " sum is now " << sum << endl; cout << " sum is now " << sum << endl; cout << " The final sum is " << sum << endl; return 0;}

Counting

• Counting statements, similar to accumulating statements have the form: • variable = variable + fixedNumber; • A common counting procedure involves adding 1 to an index. Consider the following pair of

statements: • int index = 1; • index = index + 1; • For the special case in which a variable is either increased or decreased by 1, C++ provides two

unary operators. Using the increment operator, ++, the expression variable = variable + 1 can be replaced by either the expression variable++ or ++variable. When the ++ operator appears before a variable, it is called a prefix increment operator; when it appears after a variable, it is called a postfix increment operator. Decrement operators are also built into C++. They are represented by double negative signs, !!. They may be analyzed along the lines of the increment operator (with the obvious difference in operation and sign values).

• Let us examine a set of statements illustrating how increment operators modify the counting statement above:

• ++index; • index++; • The prefix increment (or decrement) operates before any other action takes place in an

assignment statement. A postfix increment (or decrement) operates after the assignment is completed. In the code fragments shown, the effect is not noticeable for both indexes have just been increased by one. However, the diverse operations could very well impact performance in the context of a larger program.