Operators and Expressions
Operators combine values to create expressions. You will use them for calculations, comparisons, assignments, and compound conditions.
Arithmetic Operators
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Remainder | a % b |
Arithmetic example
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 3;
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "a / b = " << a / b << endl;
cout << "a % b = " << a % b << endl;
return 0;
}Comparison and Logical Operators
| Group | Operators | Purpose |
|---|---|---|
| Comparison | == != < > <= >= | Compare two values |
| Logical | && || ! | Combine or reverse conditions |
| Assignment | = += -= *= /= | Assign or update values |
Important
Integer division removes the decimal part. For example, 10 / 3 produces 3. Use a double value when you need a decimal result.
Practice task
Build a rectangle calculator that accepts width and height, then displays area and perimeter.
Lesson Summary
Perform calculations, comparisons, assignments, and logical tests.