If Statement

Home • Gallery • Tutorials • Download • Purchase • Site Map
 

If Statement Syntax

An if statement is used to control program execution in a Fractal Science Kit fractal program. An if statement has several different forms. Each form evaluates 1 or more conditional expressions and determines what statements should be executed based on the results.

Syntax 1:

if (<Condition>) {
  <Statements>
}

This form evaluates the conditional expression and executes the statements within the if block if and only if the expression is true.

Syntax 2:

if (<Condition>) {
  <Statements>
} else {
  <Statements>
}

This form evaluates the conditional expression and executes the 1st block of statements if the expression is true or the 2nd block of statements if the expression is false.

Syntax 3:

if (<Condition>) {
  <Statements>
} elseif (<Condition>) {
  <Statements>
} else {
  <Statements>
}

This form evaluates a conditional expression and executes the 1st block of statements if the expression is true. Otherwise, the 2nd conditional expression is evaluated and if true, the 2nd block of statements is executed. If both conditional expressions are false, the 3rd block of statements is executed. This form can include any number of elseif blocks and the final else block is optional.

Example 1:

if (Abs(z) > 1) {
  return
}

This statement executes the return statement if Abs(z)>1.

Example 2:

if (x = 0) {
  r = 1
} else {
  r = Sin(x)/x
}

This statement sets r to different values based on the value of x. The above statement could also have been written in the abbreviated form:

r = IIf(x = 0, 1, Sin(x)/x)

This uses the special function IIf to evaluate the conditional expression and return 1 of the 2 expressions based on the result.

IIf Syntax:

IIf(<Condition>, <Value-If-True>, <Value-If-False>)

Only 1 of the expressions <Value-If-True> or <Value-If-False> is evaluated, depending on the conditional expression. This is contrasted with normal function invocation, which evaluates all of the function arguments before calling the function.

Example 3:

if (t < 0) {
  t = 0
} elseif (t > 1) {
  t = 1
}

This statement ensures that t is between 0 and 1 inclusive as does the following:

t = IIf(t < 0, 0, IIf(t > 1, 1, t))

 

Copyright © 2004-2016 Hilbert, LLC
All rights reserved