top banner
Programming Gri
1: Introduction
2: Simple example
3: Fancy example
4: Running Gri
5: Programming Gri
6: General Issues
7: X-Y Plots
8: Contour Plots
9: Image Plots
10: Examples
11: Handling Data
12: Gri Commands
13: Gri Extras
14: Evolution of Gri
15: Installing Gri
16: Gri Bugs
17: System Tools
18: Acknowledgments
19: License
20: Newsgroup

21: Concept Index
navigate navigate navigate navigate navigate navigate

5.5: 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's 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
bottom banner