print and println@printftry-catch for Exception HandlingfinallyAn expression in Julia is a fundamental building block of programs. Think of an expression as any piece of code that, when Julia processes it, produces a value. This value can be a number, a piece of text (a string), a true/false outcome (a Boolean), or even more complex data structures. Expressions are essential for programs to perform operations and make decisions, forming the core of how logic is executed.
At its simplest, a value itself is an expression. For instance:
5 # This expression evaluates to the integer 5
"hello" # This expression evaluates to the string "hello"
true # This expression evaluates to the Boolean true
Variables, which you learned about in the previous chapter, are also expressions because they evaluate to the value they store:
x = 10 # Assign 10 to x
y = x # The expression 'x' evaluates to 10, so y also becomes 10
More commonly, expressions involve operators, which are special symbols or keywords that perform an action on one or more values. These values are called operands. For example, in the expression 3 + 4, the numbers 3 and 4 are operands, and the + symbol is the operator that performs addition.
Julia provides a variety of operators to build expressions:
+ for addition, * for multiplication).> for greater than, == for equality).&& for logical AND, || for logical OR).We will look into these categories of operators in more detail in the upcoming sections of this chapter. For now, the focus is on understanding how they combine with values and variables to form expressions.
Most operators you'll encounter are binary operators, meaning they operate on two operands, like a + b or x > y. Some are unary operators, acting on a single operand, such as -5 (negation) or !true (logical NOT).
When Julia encounters an expression, it performs the specified operations to boil it down to a single resulting value. For instance, consider the expression (5 + 3) * 2. Julia evaluates this in steps:
5 + 3, is evaluated first, resulting in 8.8 * 2.16.
So, the expression (5 + 3) * 2 evaluates to the value 16.In expressions with multiple operators, like 2 + 3 * 4, Julia needs a set of rules to decide the order in which operations are performed. This is known as operator precedence. You might remember these rules from mathematics, often recalled by acronyms like PEMDAS/BODMAS. Julia follows a similar standard:
^) are typically done first.*) and division (/, \) are performed.+) and subtraction (-) are carried out.So, in 2 + 3 * 4:
3 * 4 happens first, yielding 12.2 + 12 happens, resulting in 14.If operators have the same precedence (e.g., * and /, or + and -), Julia usually evaluates them from left to right. This is called associativity. For example, 10 - 3 + 2 is evaluated as (10 - 3) + 2, which is 7 + 2, resulting in 9.
The exact precedence rules can be quite detailed, but you don't need to memorize them all at once. The most common ones align with standard math conventions.
To make your expressions clearer or to force a different order of evaluation than what precedence rules would dictate, you can use parentheses (). Operations within parentheses are always evaluated first, starting from the innermost pair.
For example:
2 + 3 * 4 evaluates to 14 (multiplication first).(2 + 3) * 4 evaluates to 20 (addition first, because of the parentheses).Using parentheses, even when not strictly necessary, can greatly improve the readability of your code, making it easier for you and others to understand the intent.
# Without parentheses, relies on precedence
result1 = 10 + 20 / 5 # 20 / 5 is 4, then 10 + 4 is 14
println(result1) # Output: 14.0
# With parentheses, for clarity or to change order
result2 = (10 + 20) / 5 # 10 + 20 is 30, then 30 / 5 is 6
println(result2) # Output: 6.0
The diagram below illustrates the step-by-step evaluation for an expression using parentheses.
Evaluation process for the expression
(10 + 20) / 5. Parentheses ensure addition occurs before division.
The value that an expression produces also has a data type. This type depends on the types of the operands and the nature of the operator.
2 + 3 involves two integers, and + (addition) produces an integer: 5 (which is an Int).2.0 + 3 involves a floating-point number and an integer. Julia promotes the integer to a float, and the addition produces a float: 5.0 (which is a Float64).5 > 2 involves two integers, and > (comparison) produces a Boolean: true (which is a Bool)."Julia" * " is fun" is not standard string concatenation syntax by default. You'd typically use string("Julia", " is fun") or string interpolation like str1 = "Julia"; str2 = " is fun"; "$str1$str2". Both would result in a String.Understanding expressions is significant because they form the core of most statements in Julia. A statement is a complete unit of execution, and often, statements are built around expressions that calculate values, make comparisons, or call functions. As we move forward, you'll see expressions used everywhere, particularly in controlling the flow of your programs.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with