Skip to main content
Engineering LibreTexts

8.2: Logical Operators

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

    Logical operators are used between two logical variables or two conditional expressions. They are:

    Logical Operator Explanation
    .and. the result is true if both operands are true
    .or. the result is true if either operand is true
    .not. logical negate (if true, makes false and if false, makes true)

    Logical operators are used to combine conditional expressions as needed to form a more complex conditional expression. For example, given the declaration of,

    integer :: gameLives, extraLives
    

    it might be useful to know if the current value of gameLives and extraLives are both 0 which would indicate the game is over. In this case, the relational operator would be AND with the complete conditional expression,

    ( (gameLives == 0) .and. (extraLives == 0) )
    

    which will result in a true or false result. Since the AND logical operation is used, the final result will be true only if both conditional expressions are true.

    Another way of check the status to determine if the game should continue might be,

    ( (gameLives > 0) .or. (extraLives > 0) )
    

    which still results in a true or false result. However, since the OR logical operation is used, the final result will be true if either conditional expressions is true.

    The relational operators (e.g., <, <=, >, >=, ==, /=) have higher precedence than logical operators (AND, OR, NOT). This means each of the smaller conditional expressions will be completed before the logical operation is applied.

    A conditional expression can be a combination of multiple conditional expressions combined with logical operators.


    This page titled 8.2: Logical Operators is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?