Arithmetic Operators
From the syllabus
AQA: Be familiar with and be able to use ... (3.1.1.3/4.1.1.3)
- Addition, subtraction, multiplication, real/float division, integer division (including remainders), exponentiation, rounding, truncation
The table summarises the standard set of arithmetic operators:
Operator | Description | Example |
---|---|---|
+ | Adds two values | int n = a + b; |
- | Subtracts two values | int n = a - b; |
* | Multiplies two values | int n = a * b; |
/ | Divides dividend by divisor | int n = a / b; |
% | Modulus, remainder after integer division | int n = a % b; |
++ | Increment by one | int n = a++; |
-- | Decrement by one | int n = a--; |
The operators for addition, subtraction, and multiplication should present no problems as these work just the same as in Mathematics. However, division is less straightforward. When you divide an integer by another integer in C#, the result is also an integer, specifically the integer part of the answer, ignoring anything after the decimal point. The result is always truncated (rounded down), not rounded to the nearest integer.
For example:
In this case, the variable cake
will contain 3
, not 3.75
. Conversely, if you use a double
type:
Now, cake
will correctly hold 3.75
as it can store decimal values.
The modulus operator %
performs integer division but returns the remainder. For instance, 9 % 2
yields 1
.
Warning
Integer division by 0
will result in a runtime error, so always ensure your divisor is not zero.
For incrementing and decrementing, C# offers convenient operators:
The ++
operator increments a variable, while --
decrements it. These operators can be used either as postfix or prefix, yielding different results:
What will be printed to the screen after each operation? If you’re uncertain, write the code and check!
C# does not have an operator for exponentiation. Instead, we use the Math
class:
Both parameters must be double
, and the result is also a double
.
To round or truncate values, we utilize methods from the Math
class. Here’s an illustration: