Skip to main content
Engineering LibreTexts

8: Selection Statements

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

    When writing a program, it may be necessary to take some action based on the outcome of comparing the values of some variables. All programming languages have some facility for decision-making. That is, doing one thing if some condition is true and (optionally) doing something else if it is not. Fortran IF statements and/or CASE statements are used to allow a program to make decisions.

    • 8.1: Conditional Expressions
      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.
    • 8.2: Logical Operators
      Logical operators are used between two logical variables or two conditional expressions.
    • 8.3: IF Statements
      IF statements are used to perform different computations or actions based on the result of a conditional expression (which evaluates to true or false). There are a series of different forms of the basic IF statement. Each of the forms is explained in the following sections.
    • 8.4: Example One
      Writing or developing programs is easier when following a methodology. As the program becomes more complex, using a clear methodology is even more important. The main steps in the methodology are: (1) Understand the Problem, (2) Create the Algorithm, (3) Implement the Program, and (4) Test/Debug the Program. To help demonstrate this process in detail, these steps will be applied to a familiar problem as an example.
    • 8.5: SELECT CASE Statement
      A SELECT CASE statement, often referred to as a CASE statement, is used to compare a given value with preselected constants and take an action according to the first constant to match.
    • 8.6: Example Two
    • 8.7: Exercises


    This page titled 8: Selection Statements 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?