C++ for Scientists - Technische Universität Dresden
C++ for Scientists - Technische Universität Dresden C++ for Scientists - Technische Universität Dresden
26 CHAPTER 2. C++ BASICS Operator Action − subtraction, also unary minus + addition ∗ multiplication / division % modulus −− decrement ++ increment The modulus operator yields the remainder of the integer division. The ++ operator adds one to its operand and −− subtracts one. Both can precede or follow the operand. When they precede the operand, the corresponding operation will be performed before using the operand’s value to evaluate the rest of the expression. If the operator follows its operand, C ++ will use the operand’s value before incrementing or decrementing it. Consider the following example: x = 1; y = ++x; x = 1; z = x++; As a result of executing these four lines of code, y will be set to 2, x will be set to 2 and z will be set to 1. The priority and associativity of binary arithmetic operators is the same as we know it from math: multiplication and division precedes addition and subtraction. Thus, x + y ∗ z is evaluated as x + (y ∗ z). Operations of the same priority are left-associative, i.e. x / y ∗ z is equivalent to (x / y) ∗ z. Unary operators have precedence over binary: x ∗ y++ / −z means (x ∗ (y++)) / (−z). Nevertheless, as long as you are still learning C ++ and not entirely sure about the precedences, you might want to add redundant parenthesis instead of wasting hours debugging your program. With these operators we can write our first numeric program: #include int main () { float r1 = 3.5, r2 = 7.3, pi = 3.14159; float area1 = pi ∗ r1∗r1; std::cout ≪ ”A circle of radius ” ≪ r1 ≪ ” has area ” ≪ area1 ≪ ”.” ≪ std::endl; std::cout ≪ ”The average of ” ≪ r1 ≪ ” and ” ≪ r2 ≪ ” is ” ≪ (r1+r2)/2 ≪ ”.” ≪ std::endl; return 0 ; } 2.3.2 Boolean operators Boolean operators are logical and relational operators. Both return boolean values, therefore the name. Operators and their significations are:
2.3. OPERATORS 27 Operator Meaning > greater than >= greater than or equal to < less than = 1 + 7 is evaluated as if it were written 4 >= (1 + 7). Advise Integer values can be treated in C ++ as boolean. For the sake of clarity it is always better to use bool for all logical expression. This is a legacy of C where bool does not exist. Almost all techniques from C work also in C ++— as the language name suggests — but using the new features of C ++ allows you to write programs with better structure. For instance, if you want to store the result of a comparison do not use an integer variable but a bool. bool out of bound = x < min || x > max; 2.3.3 Bitwise operators Bitwise operators allow you to test or change the bits of integers. 5 There are the following operations: Operator Action & AND | OR ˆ exclusive OR ∼ one’s complement (NOT) ≫ shift right ≪ shift left The shift operators bitwise shift the value on their left by the number of bits on their right: • ≪ shifts left and adds zeros at the right end. • ≫ shifts right and adds either 0s, if value is an unsigned type, or extends the top bit (to preserve the sign) if it is a signed type. 5 The bitwise operators work also on bool but it is favorable to use the logical operators from the previous section. Especially the shift operators are rather silly for bool.
- Page 1 and 2: Technische Universität Dresden Fak
- Page 3 and 4: Contents I Understanding C++ 7 Intr
- Page 5 and 6: CONTENTS 5 10.5 Unix and Linux . .
- Page 7: Part I Understanding C ++ 7
- Page 10 and 11: 10 C ++ was not a reliable computer
- Page 12 and 13: 12 CHAPTER 1. GOOD AND BAD SCIENTIF
- Page 14 and 15: 14 CHAPTER 1. GOOD AND BAD SCIENTIF
- Page 16 and 17: 16 CHAPTER 1. GOOD AND BAD SCIENTIF
- Page 18 and 19: 18 CHAPTER 1. GOOD AND BAD SCIENTIF
- Page 20 and 21: 20 CHAPTER 2. C++ BASICS • std::c
- Page 22 and 23: 22 CHAPTER 2. C++ BASICS In the fir
- Page 24 and 25: 24 CHAPTER 2. C++ BASICS int main (
- Page 28 and 29: 28 CHAPTER 2. C++ BASICS The bitwis
- Page 30 and 31: 30 CHAPTER 2. C++ BASICS cast (type
- Page 32 and 33: 32 CHAPTER 2. C++ BASICS complicate
- Page 34 and 35: 34 CHAPTER 2. C++ BASICS } else if
- Page 36 and 37: 36 CHAPTER 2. C++ BASICS eps/= 2.0;
- Page 38 and 39: 38 CHAPTER 2. C++ BASICS for (...;
- Page 40 and 41: 40 CHAPTER 2. C++ BASICS 2.6.1 Inli
- Page 42 and 43: 42 CHAPTER 2. C++ BASICS To make su
- Page 44 and 45: 44 CHAPTER 2. C++ BASICS float divi
- Page 46 and 47: 46 CHAPTER 2. C++ BASICS The first
- Page 48 and 49: 48 CHAPTER 2. C++ BASICS #ifndef at
- Page 50 and 51: 50 CHAPTER 2. C++ BASICS float A[7]
- Page 52 and 53: 52 CHAPTER 2. C++ BASICS Encapsulat
- Page 54 and 55: 54 CHAPTER 2. C++ BASICS As a pract
- Page 56 and 57: 56 CHAPTER 2. C++ BASICS A function
- Page 58 and 59: 58 CHAPTER 2. C++ BASICS Now that t
- Page 60 and 61: 60 CHAPTER 2. C++ BASICS we need sm
- Page 62 and 63: 62 CHAPTER 2. C++ BASICS 2.12 Exerc
- Page 64 and 65: 64 CHAPTER 2. C++ BASICS 2.13 Opera
- Page 66 and 67: 66 CHAPTER 3. CLASSES apply symm bl
- Page 68 and 69: 68 CHAPTER 3. CLASSES int main() {
- Page 70 and 71: 70 CHAPTER 3. CLASSES class solver
- Page 72 and 73: 72 CHAPTER 3. CLASSES • Compilers
- Page 74 and 75: 74 CHAPTER 3. CLASSES a real number
2.3. OPERATORS 27<br />
Operator Meaning<br />
> greater than<br />
>= greater than or equal to<br />
< less than<br />
= 1 + 7 is evaluated as if it were written 4 >= (1 + 7).<br />
Advise<br />
Integer values can be treated in C ++ as boolean. For the sake of clarity it is<br />
always better to use bool <strong>for</strong> all logical expression.<br />
This is a legacy of C where bool does not exist. Almost all techniques from C work also in<br />
C ++— as the language name suggests — but using the new features of C ++ allows you to write<br />
programs with better structure. For instance, if you want to store the result of a comparison<br />
do not use an integer variable but a bool.<br />
bool out of bound = x < min || x > max;<br />
2.3.3 Bitwise operators<br />
Bitwise operators allow you to test or change the bits of integers. 5 There are the following<br />
operations:<br />
Operator Action<br />
& AND<br />
| OR<br />
ˆ exclusive OR<br />
∼ one’s complement (NOT)<br />
≫ shift right<br />
≪ shift left<br />
The shift operators bitwise shift the value on their left by the number of bits on their right:<br />
• ≪ shifts left and adds zeros at the right end.<br />
• ≫ shifts right and adds either 0s, if value is an unsigned type, or extends the top bit (to<br />
preserve the sign) if it is a signed type.<br />
5 The bitwise operators work also on bool but it is favorable to use the logical operators from the previous<br />
section. Especially the shift operators are rather silly <strong>for</strong> bool.