W3cubDocs

/Gnuplot

If

New syntax:
if (<condition>) { <commands>;
       <commands>
       <commands>
} else {
       <commands>
}
Old syntax:
if (<condition>) <command-line> [; else if (<condition>) ...; else ...]

This version of gnuplot supports block-structured if/else statements. If the keyword if or else is immediately followed by an opening "{", then conditional execution applies to all statements, possibly on multiple input lines, until a matching "}" terminates the block. If commands may be nested.

The old single-line if/else syntax is still supported, but can not be mixed with the new block-structured syntax. See if-old.

If-old

Through gnuplot version 4.4, the scope of the if/else commands was limited to a single input line. Now a multi-line clause may be enclosed in curly brackets. The old syntax is still honored but cannot be used inside a bracketed clause.

If no opening "{" follows the if keyword, the command(s) in <command-line> will be executed if <condition> is true (non-zero) or skipped if <condition> is false (zero). Either case will consume commands on the input line until the end of the line or an occurrence of else. Note that use of ; to allow multiple commands on the same line will not end the conditionalized commands.

Examples:

pi=3
if (pi!=acos(-1)) print "?Fixing pi!"; pi=acos(-1); print pi
will display:
?Fixing pi!
3.14159265358979
but
if (1==2) print "Never see this"; print "Or this either"
will not display anything.

else:

v=0
v=v+1; if (v%2) print "2" ; else if (v%3) print "3"; else print "fred"
(repeat the last line repeatedly!)

Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
Distributed under the gnuplot license (rights to distribute modified versions are withheld).