This lesson includes an expert video walkthrough — purchase once for a full year of unlimited replays to master every key point 🎬
Expression Evaluation
I. In-Class Exercises
Programming Exercises
- Temperature Conversion: L1071
- Reverse a Three-Digit Number: L1072
- Integer Multiplication Expression: L1073
Sample Code
1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 double f;
6 cin >> f;
7 double c = 5 * (f - 32) / 9;
8 printf("%.5f", c);
9
10 return 0;
11}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int number;
6 cin >> number;
7 cout << number % 10;
8 number = number / 10;
9 cout << number % 10;
10 number /= 10;
11 cout << number;
12 return 0;
13}1#include <bits/stdc++.h>
2using namespace std;
3
4int main() {
5 int num1, num2;
6 cin >> num1 >> num2;
7 cout << num1 << "*" << num2 << "=" << num1 * num2 << endl;
8
9 return 0;
10}II. Knowledge Summary
✨ Operator Summary Table
Operators in C++ have different precedence levels that determine the order of evaluation in expressions. Lower precedence numbers are evaluated first; operators with the same precedence are evaluated according to their associativity (left-to-right or right-to-left).
| Precedence | Operator Type | Operator | Name | Operation | Operands | Associativity | Result Type |
|---|---|---|---|---|---|---|---|
| 1 | Arithmetic | + | Positive | No change | 1 | Right-to-left | Numeric |
| 1 | Arithmetic | - | Negative | Negates right operand | 1 | Right-to-left | Numeric |
| 1 | Arithmetic | ++(prefix) | Prefix increment | Adds 1 to right operand | 1 | Right-to-left | Numeric |
| 1 | Arithmetic | --(prefix) | Prefix decrement | Subtracts 1 from right operand | 1 | Right-to-left | Numeric |
| 1 | Arithmetic | (postfix)++ | Postfix increment | Adds 1 to left operand | 1 | Left-to-right | Numeric |
| 1 | Arithmetic | (postfix)-- | Postfix decrement | Subtracts 1 from left operand | 1 | Left-to-right | Numeric |
| 1 | Logical | ! | NOT | Negates right operand | 1 | Right-to-left | Boolean |
| 2 | Arithmetic | * | Multiply | Left times right operand | 2 | Left-to-right | Numeric |
| 2 | Arithmetic | / | Integer division | Quotient of left divided by right | 2 | Left-to-right | Numeric |
| 2 | Arithmetic | / | Division | Left divided by right operand | 2 | Left-to-right | Numeric |
| 2 | Arithmetic | % | Modulo | Remainder of left divided by right | 2 | Left-to-right | Numeric |
| 3 | Arithmetic | + | Addition | Left plus right operand | 2 | Left-to-right | Numeric |
| 3 | Arithmetic | - | Subtraction | Left minus right operand | 2 | Left-to-right | Numeric |
| 4 | Relational | > | Greater than | Is left greater than right | 2 | Left-to-right | Boolean |
| 4 | Relational | >= | Greater or equal | Is left greater or equal to right | 2 | Left-to-right | Boolean |
| 4 | Relational | < | Less than | Is left less than right | 2 | Left-to-right | Boolean |
| 4 | Relational | <= | Less or equal | Is left less or equal to right | 2 | Left-to-right | Boolean |
| 5 | Relational | == | Equal to | Is left equal to right | 2 | Left-to-right | Boolean |
| 5 | Relational | != | Not equal | Is left not equal to right | 2 | Left-to-right | Boolean |
| 6 | Logical | && | Logical AND | Are both operands true | 2 | Left-to-right | Boolean |
| 7 | Logical | || | Logical OR | Is at least one operand true | 2 | Left-to-right | Boolean |
| 8 | Assignment | = | Assignment | Assigns right value to left operand | 2 | Right-to-left | Same as left |
| 8 | Assignment | += | Add-assign | Adds right to left and assigns | 2 | Right-to-left | Same as left |
| 8 | Assignment | -= | Subtract-assign | Subtracts right from left and assigns | 2 | Right-to-left | Same as left |
| 8 | Assignment | /= | Divide-assign | Divides left by right and assigns | 2 | Right-to-left | Same as left |
| 8 | Assignment | /= | Divide-assign | Divides left by right and assigns | 2 | Right-to-left | Same as left |
| 8 | Assignment | %= | Modulo-assign | Takes remainder and assigns to left | 2 | Right-to-left | Same as left |
III. Homework
Programming Exercises
- Calculate (a+b)*c: L1074
- Calculate (a+b)/c: L1075
- Area of a Trapezoid: L1076
- Evaluate a Polynomial: L1077