Skip to main content
Engineering LibreTexts

8.3: IF Statements

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

    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.

    IF THEN Statement

    The IF statement, using the conditional expression, is how programs make decisions. The general format for an IF statement is as follows:

    if ( <conditional expression> ) then
        <fortran statement(s)>
    end if 
    

    Where the <fortran statements> may include one or more valid Fortran statements.

    For example, given the declaration of,

    integer :: gameLives
    

    based on the current value of gameLives, a reasonable IF statement might be;

    if ( gameLives == 0 ) then
        write (*,*) "Game Over."
        write (*,*) "Please try again."
    end if
    

    which will display the message “Game Over.” and “Please try again.” on the next line if the value of gameLives is equal to 0.

    IF THEN Statement, Simple Form

    Additionally, another form of the IF statement includes

    if ( <conditional expression> ) <fortran statement>
    

    In this form, only a single statement is executed if the conditional expression evaluates to true. The previous example might be written as;

    if ( gameLives == 0 ) write (*,*) "Game Over."
    

    In this form, no “then” or “end if” are required. However, only one statement can be executed.

    IF THEN ELSE Statement

    The IF THEN ELSE statement expands the basic IF statement to also allow a series of statements to be performed if the conditional expression evaluates to false.

    The general format for an IF THEN ELSE statement is as follows:

    if ( <conditional expression> ) then
        <fortran statement(s)>
    else
        <fortran statement(s)>
    end if
    

    Where the <fortran statements> may include one or more valid Fortran statements.

    For example, given the declaration of,

    integer :: gameLives
    

    based on the current value of gameLives is, a reasonable IF THEN ELSE statement might be:

    if ( gameLives > 0 ) then
        write (*,*) "Still Alive, Keep Going!"
    else
        write (*,*) "Extra Life Granted."
        gameLives = 1
    end if
    

    Which will display the message “Still Alive, Keep Going!” if the value of gameLives is greater than 0 and display the message “Extra Life Granted.” if the value of gameLives is less than or equal to 0.

    IF THEN ELSE IF Statement

    The IF THEN ELSE IF statement expands the basic IF statement to also allow a series of IF statements to be performed in a series.

    The general format for an IF THEN ELSE IF statement is as follows:

    if ( <conditional expression> ) then
        <fortran statement(s)>
    else if ( <conditional expression> ) then
        <fortran statement(s)>
    else
        <fortran statement(s)>
    end if
    

    Where the <fortran statements> may include one or more valid Fortran statements.

    For example, given the declaration of,

    integer :: gameLives
    

    based on the current value of gameLives, a reasonable IF THEN ELSE IF statement might be:

    if ( gameLives > 0 ) then
        write (*,*) "Still Alive, Keep Going!"
    else if ( gameLives < 0 ) then
        write (*,*) "Sorry, game over."
    else
        write (*,*) "Extra Life Granted."
        gamesLives = 1
    end if
    

    Which will display the message “Still Alive, Keep Going!” if the value of gameLives is greater than 0, display the message “Sorry, game over.” if the value of game lives is < 0, and display the message “Extra Life Granted.” if the value of gameLives is equal to 0.


    This page titled 8.3: IF 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?