Skip to main content
Engineering LibreTexts

7.4: Compound Logical Operators

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

    To create compound statments of the relational operators, we can combine these using the logical operators. The truth or falsity of these follows basic rules of logic, so it helps to have some knowledge of truth tables. Again, read them as questions!

    & (“and” returns true if both parts are true)
    (“not” returns true if the initial value is false)
    | (‘or” returns true if either part is true)
    xor (“exclusive or” returns true if either part is true, but NOT both true)

    Example 7.2.1: Basic Logic

    >> (3 >= 2) & (3+4 == 6)

    ans=
    0

    Here, the question “is 3 greater than or equal to 2 AND 3 plus 4 equal to 6?” is answered as false (or a zero) since even though 3 is greater than 2, it is not true that 3 plus 4 is 6.

    Note that there are also the operators && and || which also mean “and” and “or” but are called “short-circuited” operators (look it up). As you work with these in the Editor, orange lines on the right side of the screen may appear suggesting you use && in place of & (or vice-versa) and || in place of | (or vice versa). Just take those suggestions and you’ll be fine. These operations extend to matrices with an entry-by-entry comparison of matrices of the same size.

    Example 7.2.2

    >> a=[0,1;1,2], b=[0,0;1,1]

    a =
    0 1
    1 2
    b =
    0 0
    1 1

    with

    >> a&b, a|b, xor(a,b)

    returns

    ans =
    0 0
    1 1

    ans =
    0 1
    1 1

    ans =
    0 1
    0 0

    One minor point to note is that while Matlab treats 0 as false, any other positive whole number is considered as true. So, in this example, even though the (2, 2) entry of a is a 2, it is considered as “true” for logical comparison.


    This page titled 7.4: Compound Logical Operators is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Troy Siemers (APEX Calculus) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.