Skip to main content
Mathematics LibreTexts

5.1: Boolean values and if statements

  • Page ID
    53667
  • \( \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}}\)

    A boolean value is something that is either true or false. These are built-in constants in Julia. Sometimes we will want to know if a statement is true or false, but generally, we will use them in other structures.

    We often use boolean to test various conditions. For each, testing equality, or com- parison of numbers we use ==,<,>,<=,>= for equality, less than, greater than, less than or equal or greater than or equal respectively.

    If we set 

    x=3
     

    and then we will check a few boolean statements containing x.  For example,

    x==3
    true

    Check also  x<3, x>=3 and x^2<10  to test a variety of comparisons. Edit the code block above to check.

    Compounds boolean statements

    We often want to test multiple boolean statements and can build up compound ones with either the “and” (&&) or “or” (||) operators. Recall the following table for && and ||

    AND T F   OR T F
    T T F   T T T
    F F F   F T F

    If we set

    x=3
    y=10
     

    if we want to test that x is greater than or equal to 0 and y is 5, we can enter:

    x>=0 && y==5
     

    This returns false, since only the first is true and both must be true for this compound statement to be true. However,

    x>=0 || y==5
     

    returns true since only one of the two need to be true.

    In both of these examples, it is important to note the order of operations or operator precedence. This was mentioned in section XXX, however as the number of operators grows, it’s important to know the precedence. In these cases the tests ==,<=,<,>=,> have precedence over && and ||. Therefore in the example above, x>=0 and y==5 is evaluated first before the ||

    Also, && has precedence over || in that if we evaluate

    x >=0 && y > 7 || y == 5
     

    results in true. You can think of this resulting in true && true || false and because of precedence the first pair is tested (to be true) then the result true || false results in true.

    Often when precedence is unclear, adding parentheses can be helpful. Instead, perhaps write the above as:

    (x >=0 && y > 7) || y == 5
     

    Exercise

    If ?? ???? check if ????


    This page titled 5.1: Boolean values and if statements is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Peter Staab.

    • Was this article helpful?