0Pricing
C++ Academy · Lesson

Basic Operators and Type Conversion

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

Basic Operators and Type Conversion is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “Basic Operators and Type Conversion” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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