Académique Documents
Professionnel Documents
Culture Documents
Objectives
Introducing mathematic operator and process Learning how C++ computes data Introducing compound operator Introducing logical operator and the other C++ operator Introducing increment and decrement operator
CHAPTER 4
4.1 Mathematic Operator Symbol + * / % Meaning Addition Subtraction Multiplication Division Modulus 2+3 5-3 4*2 10 / 2.0 5%2 Example 5 2 8 5 1 Result
Order of precedence Also called math hierarchy or order of operators. Determine exactly how C++ computes formulas. C++ always perform multiplication, division and modulus first and then
Order of precedence 1 2 3 4 5 6 7 8 9
Example: 6 + 6 2 + 6 * 6 + 12 3 6 10 4 2 4 / 2 / 2 2
CHAPTER 4
Parentheses Example 2 * 3 + 4 2 * (3 + 4) Used to override the order of precedence Any calculation in parentheses is always calculated before the rest of the line.
+ 10
* 14
4.2 Assignment Statement Equal sign (=) To assign the value on right to the variable on the left.
Expression Can be constants, variable or combination of both using math operator. Example: A=B; Y=2*X+2; 2.3 Multiple Assignment Assign the same value to different variables. Assignment Statement A = 100 B = 100 C = 100 2.4 Compound Assignment Combination of math operators and assignment 3 Multiple Assignment A = B = C = 100;
CHAPTER 4
Used when the same variable appears on both sides of equal sign =. Low in precedence table. Evaluated very late in equations that use them. Symbol += -= *= /= %= normal assignment compound assignment A = A +2 A += 2 B=B3 B -= 3 C=C*D C *= D E = E / 25 E /= 25 F=F%4 F %= 4
2.5
Relational Operator Conditionally controls other statements Used to compare data Symbol == > < >= <= != Meaning equals greater than less than greater than and equal less than and equal not equals
Example: Assume that a program initialise four variables: int a = 5, b = 10, c = 15, d = 5; The following expressions are TRUE: 1. a == d 2. b < c 3. c > a 4. b >= a 5. d <= b 6. b != c The following expressions are FALSE: 1. a == b 2. b > c 3. d < a 4. d > a 5. a != d 6. c <= b
CHAPTER 4
2.6
Logical Operator Sometimes called compound relational operators To combine relational operator into more powerful data testing statements. Always produces a TRUE or FALSE result. Symbol && || ! Truth table Meaning AND OR NOT
Truth table shows how to achieve TRUE/FALSE results from an if statement using logical operators. AND Operator (&&) Logical Operator T && T T && F F && T F && F Example: (AND)
#include <iostream.h> #include <iostream.h> main() main() { {
Result T F F F
T : TRUE
F : FALSE
int a = 5, b = 7, c = 8, d = 10; int a = 5, b = 7, c = 8, d = 10; if ((a < b && c > d)) if ((a < b && c > d)) cout << PALSU; cout << PALSU; else else cout << BENAR; cout << BENAR;
Output:
} }
OR Operator (||) Logical Operator T || T T || F F || T F || F Result T T T F To be TRUE, one or both sides of the operator must be TRUE
CHAPTER 4
Example: (OR)
#include <iostream.h> main() { int a = 5, b = 7, c = 8, d = 10; if ((a < b || c > d)) cout << BENAR; }
Output:
Example : (NOT)
#include <iostream.h> main() { int a = 5, b = 7; if (!(a < b)) cout << PALSU; else cout << BENAR; }
Output:
2.7
Conditional Operator
Syntax:
CHAPTER 4
2.8
Increment Operator Example: Operator ++ ++ Example description Equivalent Statement ++a;Prefix (before) a++;Postfix (after) a = a + 1; a = a + 1; a += 1; a += 1; Add one to an integer variable Can be prefix or postfix increment
2.9
Decrement Operator Subtract one form an integer variable Can be prefix or postfix decrement
Example - - a; a - -;
Equivalent a = a - 1; a = a - 1;
Statement a -= 1; a -= 1;
NOTE if a variable is incremented or decremented with prefix operator, the increment or decrement occurs before the variables value is used in the rest of the expression. If a variable is incremented or decremented with a postfix operator, the increment or decrement occurs after the variables value is used in the rest of the expression.
Example: Prefix Increment m = 5, n = 3 n += ++m n = n + (++m) n = 3 + (1 + 5) n=3+6 n=9 m=6 Postfix Increment m = 5, n = 3 n += m++ n=n+m n=3+5 n=8 m = (5 + 1) m=6 Decrement m = 5, n = 3 n += m-n=n+m n=3+5 n=8 m = (5 - 1) m=4 Decrement m = 5, n = 3 n += --m n = n + (--m) n = 3 + (5 - 1) n=3+4 n=7 m=4