Basic Operators and Type Conversion
Master arithmetic, comparison, and logical operators plus implicit and explicit type conversions.
Arithmetic Operators
The standard arithmetic operators: + - * / and modulo %. Modulo works only on integers.
int a = 17, b = 5;
std::cout << a + b << "\n"; // 22
std::cout << a - b << "\n"; // 12
std::cout << a * b << "\n"; // 85
std::cout << a / b << "\n"; // 3 (integer division)
std::cout << a % b << "\n"; // 2Integer vs Floating Division
When both operands are integers, division truncates toward zero. Cast one operand to a floating type for a real division.
int a = 7, b = 2;
std::cout << a / b << "\n"; // 3
std::cout << static_cast<double>(a) / b; // 3.5All lessons in this course
- Setting Up a C++ Compiler
- Hello World and Build Process
- Variables, Auto Type Inference and constexpr
- Basic Operators and Type Conversion