Skip to main content
Mathematics LibreTexts

2.4: Expressions and Operator Precedence

  • Page ID
    53707
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Expressions

    An expression is a combination of variables, data elements (like numbers and strings), operations (like + or *) and functions (like ). We’ve seen a number of expressions throughout this chapter so far like

    x=6
    x+3
    str1 * str2
    length(str)
     

    In short, writing things in julia will consist of writing expressions (and slightly more complicated structures).

    Operator Precedence

    When we type out an expression like 11+2*(4+3)^3, it is important to understand the order in which operators are performed. For mathematics, the PEMDAS pnemonic is helpful to rememember in that the order is:

    • Parentheses: The expression inside the ( ) are done first. For the example above, the 4+3 is the first operation done.

    • Exponentials: The ^ is done next. Raise the 7 from above to the power of 3 resulting in 343.

    • Multiplication and Division: In this example, the 2*(343) is done next

    • Addition and Subtraction: Lastly add 11 to the result and the result is 697.

    In any computing language, there are other operators as well and there is order to that precedence, so we will see that there are other things to think about. For example, the assignment operator, has the lowest precedence. That is when assigning something to a variable, all calculations are done on the right side of the = before the assignment.

    Details on all this can be found on the JULIA DOCUMENTATION ON OPERATOR PRECEDENCE

    Comments

    A comment in computer code is sequences of characters which are ignored. The purpose of a comment is to alert a human on what is going on. You may have been told to write comments so that someone else who reads your code understands what you are doing. However, I have found that the person mostly like to read your code is you at a later date. You should add comments for yourself.

    In julia, a comment is anything to the right of a , pound sign or hash tag. For example:

    ## This calculates the area of a circle
    r=3
    pi*r^2 # this is the actual formula for the area

    Both lines 1 and 3 have comments. On line 1, the entire line is ignore since the line starts with #. On line 3, everything after the 2 (the power) is ignored. Also, notice that there are two hash tags on line 1 and 1 on line 3. This is simply different style. Since anything after a single is a comment, everything after the first one is ignored.


    2.4: Expressions and Operator Precedence is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?