Lesson 7
Operators and Expressions
Covers arithmetic, relational, logical, assignment operators.
Lesson content
What are operators and expressions?
Operators act on values. Values are operands.
Expression combines vars, literals, operators. Gives one value.
int sum = 10 + 20;- +: operator
- 10 and 20: operands
- 10 + 20: expression
- sum: stores result
Why do we need operators?
- Perform math calculations
- Compare values
- Make decisions
- Combine conditions
- Manipulate variables
- Work with bits at hardware level
Classification of C++ operators
C++ Operators
│
┌───────────┬────────────┬─────────────┬─────────────┐
│ │ │ │
▼ ▼ ▼ ▼
Arithmetic Relational Logical Assignment
│ │ │ │
▼ ▼ ▼ ▼
Bitwise Increment Conditional Miscellaneous1. Arithmetic operators
Operator | Description | Example
-------- | -------------------- | -------
+ | Addition | a + b
- | Subtraction | a - b
* | Multiplication | a * b
/ | Division | a / b
% | Modulus (Remainder) | a % b#include <iostream>
using namespace std;
int main()
{
int a = 15;
int b = 4;
cout << "Addition: " << a + b << endl;
cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
cout << "Remainder: " << a % b << endl;
return 0;
}2. Relational operators
Compare two values. Return true or false.
Operator | Meaning
-------- | -------------------------
== | Equal to
!= | Not equal to
> | Greater than
< | Less than
>= | Greater than or equal to
<= | Less than or equal to#include <iostream>
using namespace std;
int main()
{
int x = 10;
int y = 20;
cout << (x < y) << endl;
cout << (x == y) << endl;
return 0;
}3. Logical operators
Combine or reverse conditions.
- &&: Logical AND
- ||: Logical OR
- !: Logical NOT
#include <iostream>
using namespace std;
int main()
{
int age = 20;
bool hasID = true;
cout << (age >= 18 && hasID);
return 0;
}4. Assignment operators
Operator | Example | Equivalent To
-------- | -------- | -------------
= | x = 5 | Assign 5
+= | x += 2 | x = x + 2
-= | x -= 2 | x = x - 2
*= | x *= 2 | x = x * 2
/= | x /= 2 | x = x / 2
%= | x %= 2 | x = x % 25. Increment and decrement operators
Change a variable by one.
#include <iostream>
using namespace std;
int main()
{
int count = 5;
count++;
cout << count << endl;
return 0;
}6. Bitwise operators
Work on binary bits directly.
- &: Bitwise AND
- |: Bitwise OR
- ^: Bitwise XOR
- ~: Bitwise NOT
- <<: Left Shift
- >>: Right Shift
Used in embedded systems and drivers.
7. Conditional (ternary) operator
Shortcut for choosing between two values.
condition ? expression1 : expression2;
int age = 18;
string result = (age >= 18) ? "Adult" : "Minor";Expressions in C++
Expression combines operands and operators.
a + b
x * y + z
marks >= 40
age >= 18 && citizenOperator precedence
Precedence sets evaluation order.
Priority | Operators
-------- | ---------------------
Highest | ()
| ++, --
| *, /, %
| +, -
| <, <=, >, >=
| ==, !=
| &&
Lowest | ||int result = 5 + 2 * 3;
// Result: 11Common mistakes and pitfalls
- Using = instead of ==. Fix: use == for comparing
- Integer division expects decimals. Fix: use float/double
- Ignoring precedence. Fix: use parentheses
- % with floats. Fix: % works only on integers
// Incorrect
if (marks = 50)
{
cout << "Pass";
}// Correct
if (marks == 50)
{
cout << "Pass";
}Best practices
- Use parentheses for readability
- Prefer meaningful expressions over complex ones
- Avoid nested ternary operators
- Use logical operators carefully
- Keep expressions simple
When not to use certain operators
- Nested ternary. Use if...else instead
- Bitwise for Boolean logic. Use && or ||
- Long expressions without parens. Break into smaller parts
Summary
- Operators act on operands
- Expressions combine operators, operands, give a value
- C++ has many operator types
- Precedence sets evaluation order
- Parentheses reduce errors
- Needed before loops and conditionals
Frequently asked questions
What is an operator in C++?
Symbol performing an operation on operands.
What is an expression?
Operands and operators combined into one value.
What is the difference between = and ==?
= assigns a value. == compares two values.
Why is operator precedence important?
Sets operation order. Avoids unexpected results.
What should I learn after operators and expressions?
- Input and Output (cin, cout)
- Type Casting
- Decision-Making Statements (if, switch)
- Loops (for, while, do-while)
- Functions