Search⌘ K
AI Features

Arithmetic and Assignment Operators

Explore how to perform calculations and update variables using C++ arithmetic and assignment operators. Understand integer versus floating-point division, the difference between prefix and postfix increments, and how operator precedence affects expressions. Gain practical knowledge to write clear and correct computations in your programs.

Mathematics is the engine behind almost every program, whether we are calculating damage in a game, processing financial transactions, or resizing an image. C++ provides a robust set of operators to handle these tasks efficiently. However, unlike standard algebra, C++ math has specific rules regarding types and evaluation order.

In this lesson, we will learn how to manipulate data using arithmetic and assignment operators, ensuring our calculations are both correct and efficient.

Basic arithmetic operators

Arithmetic operators allow programs to perform basic math operations on values, forming the foundation of most calculations. C++ provides five fundamental arithmetic operators:

  • Addition (+): It adds two values together. It is commonly used to combine values, update counters, or calculate totals.

  • Subtraction (-): It finds the difference between two values. This is often used when decreasing values or calculating remaining amounts.

  • Multiplication (*): It multiplies two values. It is useful for scaling values or performing repeated addition.

  • Division (/): It divides one value by another. Remember that when used with integers, the result is also an integer, and any decimal part is discarded. To get a decimal result, at least one operand must be a floating-point value.

  • Modulus (%): It gives the remainder after division. It works only with integers and is commonly used to check divisibility or determine whether a number is even or odd.

Let's see them in action ... ...