200 likes | 326 Views
LP104: Object-Oriented Programming. C++ Basics (Part 2). Outlines. Operators Arithmetic Operators Operator Precedence and Associativity Expressions Different Forms of Assignment Operators Increment and Decrement Operators Swapping Values of Two Variables. 1. Operators.
E N D
LP104: Object-Oriented Programming C++ Basics (Part 2)
Outlines • Operators • Arithmetic Operators • Operator Precedence and Associativity • Expressions • Different Forms of Assignment Operators • Increment and Decrement Operators • Swapping Values of Two Variables
1. Operators • An operator is a symbol or keyword that represents an operation to be applied to the data • We use operators to manipulate data in the program • e.g.: variableA = 40 + 20; cout << "A"; • Operand – input to an operator • Binary operator – an operator that accepts 2 operands • e.g.: 40 + 20 • Unary operator – an operator that accepts only 1 operand • e.g.: -5
2. Arithmetic Operators • When used as an unary operator, – becomes a negation operator, which turns positive value into negative value and vice versa. • e.g.: -5 yields "negative five" • + can also be used as an unary operator but it is meaningless. Exercise: evaluate the following expressions • 20 % 3 • 2 % 9 • 30 / 20 / 2 • 10 * 2 + 4 * 3
3. Operator Precedence & Associativity • How should we evaluate the following expression? In what order should the operators be applied? 2 - 25 / 10 + 33 % 10 * 2 • Among different operators, operator precedence tells us which operator(s) should be applied first. • Among operators with the same precedence,operator associativity tells us whether the left-most or the right-most operator should be applied first.
3. Operator Precedence & Associativity • Operators at the same level have the same precedence. • e.g.: - a * b - c is equivalent to ((- a) * b) – c • 2 - 25 / 10 + 33 % 10 * 2 = ?
3.1. Parentheses • Use parentheses '(' and ')' to explicitly specify the evaluation order of sub-expressions (a + b) * (c + d) • Multiple level of parentheses ((a + b) * (a + b) - c) * (d - e) • Don't use '[', ']' or '{', '}' • Tips: Use parentheses for clarity or when you are not sure about the precedence of the operators.
4. Expressions • An expression is a combination of operators, constants, variables, and function calls • e.g.: 30 24 + a d = b * b - 4 * a * c sqrt(4.0) + a * sqrt(9.0) • An expression • Can always be evaluated to a value • Can be part of another expression • Can an expression be a statement?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // Showing only part of a program int a = 0, b = 2, c, d; a = a + 1; cout << "a = " << a << "\n"; b = b * a; a = 0; cout << "b = " << b << "\n"; d = c = b = 0; cout << "b = " << b << "\n"; cout << "c = " << c << "\n"; cout << "d = " << d << "\n"; // d = c = b = 0 is quivalent to d = (c = (b = 0)) Equivalent to int a, b, c, d; a = 0; b = 2; Examples illustrating key concepts of assignment operators
5.1. Assignment Operators variable = expression • Low precedence, right-to-left associativity • expression is evaluated first and the evaluated value is copied to variable. • "variable = expression" is also an expression which evaluates to the value of variable. • e.g.: var1 = var2 = 3 + 2 is evaluated as var1 = (var2 = (3 + 2))
5.2. Assignment Operators – Short Form • k = k + 2 can be written as k += 2 • The semantics of variable = variable op (expression) is equivalent to variable op= expression Some "short-form" assignment operators += -= *= /= %=.
Be Careful! • Note that the following statement j *= k + 3; is equivalent to j = j * (k + 3); rather than j = j * k + 3;
6. The increment operator ++ • Increase the value of a variable by one. • e.g. value = 10; value++; // Same as value = value + 1; cout << value; // Output 11 • Unary operator • Can only be applied to variables i++ // Valid 777++ // Invalid (a = 3)++ // Invalid
6.1. Prefix and Postfix Forms • The ++ operator can occur in either prefix or postfix position, with different results. • ++i • Increase the value of i by 1 • The value of the expression "++i" is the value of iAFTER the increment operation. • i++ • Increase the value of i by 1 • The value of the expression "i++" is the value of iBEFORE the increment operation.
Tips: Good practice for using ++ and -- Avoid mixing ++ or -- with other operators in the same expression. a = ++c; should be written as c++; // or ++c; a = c; b = c++; should be written as b = c; c++; // or ++c;
6.1. Prefix and Postfix Forms Prefix Form Postfix Form int a, b; a = 0; b = a++; cout << a << endl; cout << b << endl; cout << b++ << endl; int a, b; a = 0; b = ++a; cout << a << endl; cout << b << endl; cout << ++b << endl; 1 1 2 1 0 0 • The -- operator (called the decrement operator) is similar to ++, except that the value of the associated variable is decreased by one.
7. Swapping the value of two variables int a = 0, b = 1, tmp; // How to exchange/swap the value of a and b? a = b; // Method A ? b = a; tmp = b; // Method B ? b = a; a = tmp; tmp = b; // Method C ? a = tmp; b = a; Answer: Method B
8. ++ and -- Operators int num1 = 1, num2 = 2, num3 = 4; 1. num1 = ++num2; // num1 = ? , num2 = ? 2. num1 = num3-- * 4; // num1 = ? , num3 = ? 3. num1 = num2 = 2; num1 *= num2++; // num1 = ? , num2 = ?
Summary • How to manipulate numerical values using arithmetic operators • How to evaluate expressions containing different operators • Understand the behavior of ++ and -- in both posfix and prefix forms • How to swap the value of two variables