print and println@printftry-catch for Exception HandlingfinallyVariables serve as containers for data. Julia programs frequently perform calculations with these values, storing the results back into variables. This process relies on arithmetic and assignment operations, which are fundamental to nearly every Julia program.
Arithmetic operations are how you perform mathematical computations in Julia. You've likely encountered these in everyday math. Julia uses standard symbols for most of these operations.
+): Adds two numbers.
julia> 5 + 3
8
-): Subtracts the second number from the first.
julia> 10 - 4
6
*): Multiplies two numbers.
julia> 7 * 6
42
/): Divides the first number by the second. Division always results in a floating-point number, even if the division is exact.
julia> 10 / 2
5.0
julia> 7 / 2
3.5
Julia provides a few more specialized arithmetic operators that are very useful:
Integer Division (÷ or div()): Performs division and returns the integer part of the quotient, discarding any remainder. You can type the ÷ symbol by typing \div then pressing Tab in the Julia REPL or many Julia-aware editors.
julia> 10 ÷ 3 # \div<TAB>
3
julia> div(10, 3)
3
Remainder (% or rem()): Returns the remainder after integer division. This is often called the modulo operator.
julia> 10 % 3
1
julia> rem(10, 3)
1
The remainder operator is useful for tasks like checking if a number is even or odd (e.g., number % 2 == 0 for even).
Exponentiation (^): Raises the first number to the power of the second number.
julia> 2^3 # 2 to the power of 3
8
julia> 5^2
25
Unary Minus and Plus (-, +): The minus sign can also be used to negate a number. The plus sign can be used to indicate a positive number, though it's often omitted.
julia> x = 5
5
julia> -x
-5
julia> +x
5
When an expression contains multiple operators, Julia follows a standard order of operations, often remembered by acronyms like PEMDAS/BODMAS (Parentheses/Brackets, Exponents/Orders, Multiplication and Division, Addition and Subtraction). This means * and / are performed before + and -.
For example:
julia> 2 + 3 * 4
14
Here, 3 * 4 (which is 12) is calculated first, and then 2 is added to the result, giving 14. If you want to change the order, you use parentheses ():
julia> (2 + 3) * 4
20
In this case, 2 + 3 (which is 5) is calculated first due to the parentheses, and then the result is multiplied by 4, giving 20.
It's good practice to use parentheses if you're ever unsure about the order of operations or if it makes the expression clearer to read.
Here's a summary of common arithmetic operators and their typical precedence (higher means evaluated earlier):
| Operator | Description | Example | Result | Precedence |
|---|---|---|---|---|
^ |
Exponentiation | 2 ^ 3 |
8 |
Highest |
- (unary) |
Negation | -5 |
-5 |
High |
*, /, ÷, % |
Multiplication, Division, Integer Division, Remainder | 6 * 2, 6 / 2, 7 ÷ 2, 7 % 2 |
12, 3.0, 3, 1 |
Medium |
+, - (binary) |
Addition, Subtraction | 5 + 2, 5 - 2 |
7, 3 |
Low |
The diagram below illustrates the order of evaluation for the expression
value = 10 + 4 * 2. Multiplication occurs before addition, followed by assignment.
In Chapter 2, you learned about variables and how to assign values to them using the equals sign =. This is the most fundamental assignment operation.
julia> width = 10 # Assigns the value 10 to the variable width
10
julia> height = 5 # Assigns the value 5 to the variable height
5
julia> area = width * height # Calculates width * height (50) and assigns it to area
50
The expression on the right-hand side of = is evaluated first, and then its result is stored in the variable on the left-hand side.
Often, you'll want to modify a variable's value based on its current value. For example, you might want to add a number to an existing variable. You could write:
julia> score = 100
100
julia> score = score + 10
110
This is perfectly valid. However, Julia, like many other programming languages, provides a more concise way to write such operations using compound assignment operators (also known as augmented assignment operators).
These operators combine an arithmetic operation with an assignment. Here are the common ones:
+= (add and assign): x += y is shorthand for x = x + y.
julia> count = 5
5
julia> count += 3 # Same as count = count + 3
8
-= (subtract and assign): x -= y is shorthand for x = x - y.
julia> balance = 50.0
50.0
julia> balance -= 10.5 # Same as balance = balance - 10.5
39.5
*= (multiply and assign): x *= y is shorthand for x = x * y.
julia> quantity = 3
3
julia> quantity *= 4 # Same as quantity = quantity * 4
12
/= (divide and assign): x /= y is shorthand for x = x / y.
julia> total_value = 100.0
100.0
julia> total_value /= 4 # Same as total_value = total_value / 4
25.0
%= (remainder and assign): x %= y is shorthand for x = x % y.
julia> items = 27
27
julia> items_per_box = 5
5
julia> items %= items_per_box # Same as items = items % items_per_box (remainder)
2
^= (exponentiate and assign): x ^= y is shorthand for x = x ^ y.
julia> num = 2
2
julia> num ^= 3 # Same as num = num ^ 3
8
Using these compound operators can make your code shorter and sometimes easier to read, especially when variable names are long.
Here's a table summarizing these assignment operators:
| Operator | Example | Equivalent to | Description |
|---|---|---|---|
= |
x = 5 |
x = 5 |
Basic assignment |
+= |
x += 2 |
x = x + 2 |
Add and assign |
-= |
x -= 2 |
x = x - 2 |
Subtract and assign |
*= |
x *= 2 |
x = x * 2 |
Multiply and assign |
/= |
x /= 2 |
x = x / 2 |
Divide and assign |
%= |
x %= 2 |
x = x % 2 |
Remainder (modulo) and assign |
÷= |
x ÷= 2 |
x = x ÷ 2 |
Integer divide and assign |
^= |
x ^= 2 |
x = x ^ 2 |
Exponentiate and assign |
When you perform arithmetic operations with numbers of different types (like an integer and a floating-point number), Julia often automatically "promotes" the result to a type that can represent both values accurately. For example, adding an Int to a Float64 will typically result in a Float64:
julia> 10 + 5.5
15.5
julia> typeof(10)
Int64
julia> typeof(5.5)
Float64
julia> typeof(10 + 5.5)
Float64
This is usually what you want, as it prevents loss of precision. We explored data types in Chapter 2, and understanding how they interact during operations is important as you write more complex programs. For now, be aware that Julia tries to do the sensible thing to preserve your data.
Arithmetic and assignment operations are the workhorses of computation in Julia. By combining them, you can perform a wide array of calculations and manage data effectively within your programs. As we move on to control flow, you'll see how these operations fit into larger structures that make decisions and repeat tasks.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with