C++ Academy · Lesson

Basic Operators and Type Conversion

Master arithmetic, comparison, and logical operators plus implicit and explicit type conversions.

Lesson 4 of 413 steps

Basic Operators and Type Conversion is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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";  // 2

Integer 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.5

Increment and Decrement

++ and -- add or subtract one. Prefix (++x) increments first then returns the value. Postfix (x++) returns the original then increments.

Compound Assignment

Combine arithmetic with assignment: +=, -=, *=, /=, %=. These are shorter and often clearer.

int total = 0;
total += 5;     // total = total + 5
total *= 2;     // total = total * 2

Comparison Operators

Six comparison operators return bool: ==, !=, <, <=, >, >=.

Logical Operators

Combine boolean values with && (and), || (or), and ! (not). They short-circuit — if the result is decided early, the rest is not evaluated.

bool valid = (age >= 18) && (age < 120);
bool weekend = (day == "Sat") || (day == "Sun");

Bitwise Operators

For bit manipulation: & (and), | (or), ^ (xor), ~ (not), << (shift left), >> (shift right).

Implicit Conversions

C++ automatically converts between numeric types in many contexts — sometimes silently losing precision. Be careful with mixed-type expressions.

int i = 10;
double d = i;       // OK: int to double
int j = 3.7;        // OK but truncates to 3

static_cast: Explicit Conversion

The preferred way to convert between related types. It is checked at compile time.

double price = 9.99;
int dollars = static_cast<int>(price);  // 9

Other Casts: reinterpret_cast const_cast

Three other casts exist for special cases:

  • reinterpret_cast — reinterpret bit patterns (use rarely)
  • const_cast — remove const (use even more rarely)
  • dynamic_cast — runtime cast for polymorphic types

Avoid C-Style Casts

The old (int)x syntax is ambiguous — it can mean any of the four casts. Use the named cast that matches your intent.

Quick Check

What does 7 / 2 evaluate to in C++ when both operands are int?

Recap

C++ provides arithmetic, comparison, logical, and bitwise operators. Implicit conversions can lose data — prefer static_cast for explicit conversions and avoid C-style casts.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Basic Operators and Type Conversion” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Basic Operators and Type Conversion”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Basic Operators and Type Conversion”?

Master arithmetic, comparison, and logical operators plus implicit and explicit type conversions. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Basic Operators and Type Conversion” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Setting Up a C++ Compiler
  2. Hello World and Build Process
  3. Variables, Auto Type Inference and constexpr
  4. Basic Operators and Type Conversion
← Back to C++ Academy