Chapters:
1: Introduction
2: Simple example
3: Invocation
4: Finer Control
5: X-Y Plots
6: Contour Plots
7: Image Plots
8: Examples
9: Gri Commands
10: Programming
11: Environment
12: Emacs Mode
13: History
14: Installation
15: Gri Bugs
16: Test Suite
17: Gri in Press
18: Acknowledgments
19: License
Indices:
Concepts
Commands
Variables

## 10.6: If Statements

Gri has ``if`' statements to make your programs more flexible. Here's an example:

    query \thick "Use thick lines? (0 or 1)" ("0")
    if \thick
      set line width 2
    else
      set line width 0.5
    end if

If you answer 1 to the question, the line thickness will be set at 2 points. If you answer 0 then a thin line will be used. If you press carriage return a thin line will be used.

The item following the ``if`' can be

- a number (1 means true; anything else means false)
- a variable (1 means true; anything else means false). Example:

      if .plot_contours.
        draw contour
      end if

- a synonym which expands to a number (1 means true; anything else means false). Example:

      \plot_contours = "1"
      if \plot_contours
        draw contour
      end if

  (Don't worry about the fact that synonyms are strings; Gri expands the string value before interpreting the ``if`' statement.)

- an expression of the form ``{string1 == string2 }`'. The symbol ``==`' is an operator which tests for string equality. This expands to ``1`' if the strings are equal, or ``0`' otherwise. The strings may be either synonyms or string constants. If the string constant contains only one word, then it is not necessary to enclose it in quotes, but it is clearer to do so. Examples:

      if {"\variable" == "Salinity"}
        set x name "Salinity"
      else
        set x name "Unknown"
      end if

- a rpn (reverse polish notation) expression (see rpn Mathematics):

      if {rpn .time. 100 <}
        # ie, (100 < time), not (time < 100)
        show "Time > 100"
      else if {rpn .time. 100 >}
        show "Time < 100"
      else if {rpn "\item" "later" ==}
        show "Time ... later babe"
      else
        show "Time is equal to 100"
      end if
      if {rpn .time. 10 * 100 ==}
        show "Time is equal to 10"
      else
        show "Time is not equal to 10"
      end if

There is no need to put the else part in if you don't need it. You can do

    set line width 0.5
    if \use_thick_lines
      set line width 2
    end if

if you wish.

If you want just the else part, you can do

    if ! \use_thick_lines
      set line width 0.5
    end if

(The exclamation point denotes logical negation: ``! true`' equals ``false`'.)

If statements may be nested many levels deep. You may also have ``else if`' blocks, as in:

    if {"\variable" == "S"}
      set x name "Salinity"
      set x axis 32 33 0.5 .1
    else if {"\variable" == "T"}
      set x name "Temperature"
      set x axis 15 20 1 0.5
    else
      set x name "Unknown"
    end if