Skip to main content
Engineering LibreTexts

8.1: Conditional Expressions

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

    The first step is to compare two values. Values may be literals, variables, or expressions. These values are compared with a relational operator and are referred to as operands. Relational operators are used between variables or operands of matching types. That is real to real, integer to integer, logical to logical, and character/string to character/string.

    The basic relational operators are:

    Relational Operation Relational Operator (normal) Relational Operator (alternate)
    Greater than > .gt.
    Greater than or equal >= .ge.
    Less than < .lt.
    Less than or equal <= .le.
    Equal to == .eq.
    Not equal to /= .ne.

    The normal form will be used for examples in this text. However, the alternate form may be used at any time. The alternate forms may be required to support older Fortran programs.

    A relational operation is used to form a conditional expression. The result of a conditional expression must always result in either a true or false result.

    The “==” (two equal signs) is used to compare. The “=” (single equal) is used for assignment (setting a variable). The “==” does not change any values, while the “=” does.

    For example, given the declaration of,

    integer :: gameLives
    

    it might be useful to know if the current value of gameLives is greater than 0.

    In this case, the conditional expression would be,

    (gamelives > 0)
    

    Which will result in a true or false result based on the value of the variable gameLives.


    This page titled 8.1: Conditional Expressions 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.