Friday, July 11, 2008

2.5 Assignments and Expressions

Shorthand Arithmetic Assignment Statements

We have already seen how programs can include variable assignments such as number = number + 1;
Since it is often the case that variables are assigned a new value in function of their old value, C++ provides a shorthand notation. Any of the operators "+" (addition), "-" (subtraction), "*" (multiplication), "/" (division) and "%" (modulus) can be prefixed to the assignment operator (=), as in the following examples (mostly copied from
Savitch, page 74):
Example:
number += 1;
total -= discount;
bonus *= 2;
time /= rush_factor;
change %= 100;
amount *= count1 + count2;
Equivalent to:
number = number + 1;
total = total - discount;
bonus = bonus * 2;
time = time / rush_factor;
change = change % 100;
amount = amount * (count1 + count2);
The first of the above examples may written in even shorter form. Using the increment operator "++", we may simply write number++;
The operator "++" may also be used as a prefix operator: ++number;
but care must be taken, since in some contexts the prefix and postfix modes of use have different effects. For example, the program fragment x = 4;
y = x++;
results in "x" having the value 5 and "y" having the value 4, whereas x = 4;
y = ++x;
results in both variables having value 5. This is because "++x" increments the value of "x" before its value is used, whereas "x++" increments the value afterwards. There is also an operator "--", which decrements variables by 1, and which can also be used in prefix or postfix form.
In general, assignment statements have a value equal to the value of the left hand side after the assignment. Hence the following is a legal expression which can be included in a program and which might be either evaluated as true or as false: (y = ++x) == 5
It can be read as the assertion: "after x is incremented and its new value assigned to y, y's value is equal to 5".
Boolean Expressions and Operators

Intuitively, we think of expressions such as "2 <>= 9" as evaluating to "true" or "false" ("!=" means "not equal to"). Such expressions can be combined using the logical operators "&&" ("and"), "" ("or") and "!" ("not"), as in the following examples:
Expression:
(6 <= 6) && (5 < 3)
(6 <= 6) (5 < 3)
(5 != 6)
(5 < 3) && (6 <= 6) (5 != 6)
(5 < 3) && ((6 <= 6) (5 != 6))
!((5 < 3) && ((6 <= 6) (5 != 6)))
True or False:
false
true
true
true
false
true
The fourth of these expressions is true because the operator "&&" has a higher precedence than the operator "". You can check the relative precedence of the different C++ operators in a C++ programming manual or text book (see for example
Savitch, page 976-977). But if in doubt use ( ) parentheses, which in any case often make the program easier to read.
Compound Boolean expressions are typically used as the condition in "if statements" and "for loops". For example: ...
...
if (total_test_score >= 50 && total_test_score < 65)
cout << "You have just scraped through the test.\n";
...
...
Once again, there is an important technical point concerning Boolean expressions. In C++, "true" is represented simply as any non-zero integer, and "false" is represented as the value 0. This can lead to errors. For example, it is quite easy to type "=" instead of "==". Unfortunately, the program fragment ...
...
if (number_of_people = 1)
cout << "There is only one person.\n";
...
...
will always result in the message "There is only one person" being output to the screen, even if the previous value of the variable "number_of_people" was not 1.

0 comments: