# 7.1 Conditionals

The if statement can be used to conditionally execute a series of commands only when a certain criterion is satisfied. In its simplest form, its syntax is

```if <expression> { .... }
```

where the expression can take the form of, for example, x<0 or y==1. Note that the operator == is used to test the equality of two algebraic expressions; the operator = is only used to assign values to variables and functions. A full list of the operators available can be found in Table 3.1. As in many other programming languages, algebraic expressions are deemed to be true if they evaluate to any non-zero value, and false if they exactly equal zero. Thus, the following two examples are entirely legal syntax, and the first print statement will execute, but the second will not:

```if 2*3 {
print "2*3 is True"
}

if 2-2 {
print "2-2 is False"
}
```

The variables true and false are predefined constants, making the following syntax legal:

```if false {
print "Never gets here"
}
```

As in C, the block of commands which are to be conditionally executed is enclosed in braces (i.e. { }). The closing brace must be on a line by itself at the end of the block, or separated from the last command in the block by a semi-colon.

```if (x==0)
{
print "x is zero"
}

if (x==0) { print "x is zero" ; }
```

After such an if clause, it is possible to string together further conditions in else if clauses, perhaps with a final else clause, as in the example:

```if (x==0)
{
print "x is zero"
} else if (x>0) {
print "x is positive"
} else {
print "x is negative"
}
```

Here, as previously, the first script block is executed if the first conditional, x==0, is true. If this script block is not executed, the second conditional, x>0, is then tested. If this is true, then the second script block is executed. The final script block, following the else, is executed if none of the preceding conditionals have been true. Any number of else if statements can be chained one after another, and a final else statement can always be optionally supplied. The else and else if statements must always be placed on the same line as the closing brace of the preceding script block.

The precise way in which a string of else if statements are arranged in a Pyxplot script is a matter of taste: the following is a more compact but equivalent version of the example given above:

```if      (x==0) { print "x is zero"     ; } \
else if (x> 0) { print "x is positive" ; } \
else           { print "x is negative" ; }
```