## Statements

#### color assignment statement

The color assignment statement can take one of two forms:

- indexed color assignment statement

  The format of the indexed color assignment statement is:

         set_color(
            double_expression
         );

- rgb color assignment statement The format of the rgb color assignment statement is:

         set_color(
            double_expression,
            double_expression,
            double_expression
         );

In both cases, "double_expression" is a mathematical expression that evaluates to a number. Note that an expression that evaluates to a complex value is not acceptable - a more formal definition of double_expression can be found here but for now just remember that a double_expression evaluates to a number. Here is a simple example of an indexed color assignment:

       set_color(2 * 50);

This example sets the current pixel to the rgb value associated with color table entry 100. A corresponding example for direct color assignment would be:

       set_color(255, 0, 0);

This example sets the current pixel to red. Some final notes on the two color assignment statements:

- indexed color assignment - the double_expression used should evaluate to a number for which a color table entry has been assigned (for more detail see the documentation for the colors section and for coloring techniques) If the double_expression evalutes to an index for which a color table entry has not been defined, the pixel will be colored black.

- direct color assignment - each of the double_expressions for red, green and blue must evalute to a number <= 0 and >= 255 (again, there is further information in the coloring techniques document). If any of the values are out of range, the pixel will be colored black.

#### double variable assignment statement

The general format of the double assignment statement is as follows:

       $variable_name = double_expression;

This statement assigns the value of 'double_expression' to the variable whose name is 'variable_name'. More precisely, the right hand side of this assignment (double_expression) can be composed of:

- numbers - such as: 3, 1.267, 1e-10

- double variable references - such as $a, $b, $c

- additional double variable assignments. This allows us to do things like:

  $a = $b = $c = 10.0;

- extended assignment such as:

  Example | Explanation
  --- | ---
  $a *= 2; | $a = $a * 2
  $a /= 3; | $a = $a / 3
  $a -= 4; | $a = $a - 4
  $a += 5; | $a = $a + 5
  $a++ | $a = $a + 1
  ++$a++ | $a = $a + 1
  $a-- | $a = $a - 1
  --$a++ | $a = $a - 1

  The difference between '$a++' and '++$a' comes about when those expressions are used as part of a longer assignment statement as in:

        $a = 1;
        $b = 1;
        $c = ++$a;
        $d = $b++;

  In this example, after all the statments are execute both a, b and c will be 2 but d would be 1. If the "++" or "--" comes after the variable the variable is incremented (or decremented) but the original value is used if this is part of a longer assignment statement

- the ternary operator which allows a shorthand if/else construct:

  $a = 2;
  $b = 3;
  $c = ($a > $b ? 1 : 2);

  Which means: "If $a is greater than $b assign $c the value 1 else assign $c the value 2"

- complex attributes
  real, imaginary, magnitude, sum_of_squares, degrees, radians

- double functions
  acsc, acsch, acos, acosh, acot, acoth, asec, asech, asin, asinh, atan, atanh, ceil, cos, cosh, cot, coth, csc, csch, exp, abs, fabs, fact, floor, log, log10, sec, sech, sin, sinh, sqrt, tan, tanh, atan2, mod, fmod, pow, min, max, gamma, get_sin_color, map_color

Additionally, a double expression can combine any of the items listed above using:

- - (unary minus)
- +, -, *, /
- ()
- % (a % b is shorthand for mod(a, b))
- ^ (a ^ b is shorthand for pow(a, b))

Here are some examples of double variable assignment statements:

- $a = 1 + 2 + 3;
- $b = real(z) + fmod($c, $d * sin(5));
- $c = -$a + pow(min(2, 3), max(3, 4)) * magnitude(z + c);

#### complex variable assignment statement

The format of the complex assignment statement is as follows:

       variable_name = complex_expression; or
       variable_name = double_expression;

Note that 'variable_name' is used, not '$variable_name'. The '$' is what distinguishes a double variable from a complex variable. The preceding statement assigns the value of 'complex_expression' to the complex variable whose name is 'variable_name'. More precisely, the right hand side of this assignment (complex_expression) can be composed of:

- a double expression (which we discussed above)

- complex variable references - such as: z, c, current

- complex numbers - such as: [1, 2] or [sin(2), $a + $b]. Note that complex numbers have the general form:

            [ double_expression, double_expression ]

  Anything that is a valid double_expression can be used to construct a complex number in this fashion.

- additional complex variable assignments (just like doubles) as in:

           a = b = c;
           z = y = $x;

- extended assignment (identical to the table/description of double variable extended assignment above)

- complex functions rotate, acos, acosh, acot, acoth, acsc, acsch, asec, asech, asin, asinh, atan, atanh, conj, cos, cosh, cot, coth, csc, csch, exp, log, sec, sech, sin, sinh, sqrt, tan, tanh, pow

Additionally, a complex expression can combine any of the items listed above using:

- - (unary minus)
- +, -, *, /
- ()
- ^ (a ^ b is shorthand for pow(a, b))

Here are some examples of complex variable assignment statements:

- a = pow(b, c) + [$a, sin(2) / 3];
- b = complex(1 + 2 + 3 + 4) * sin(a / c);
- c = (a + b) * (c - d) * sqrt(z * [$a, $b]);

#### conditional statement

Conditional statements look like this:

       if(boolean_expression)
       {
          zero or more statements
       }

       or

       if(boolean_expression)
       {
          zero or more statements
       }
       elseif(boolean_expression)
       {
          zero or more statements
       }

       or

       if(boolean_expression)
       {
          zero or more statements
       }
       else
       {
          zero or more statements
       }

       or

       if(boolean_expression)
       {
          zero or more statements
       }
       elseif(boolean_expression)
       {
          zero or more statements
       }
       elseif(boolean_expression)
       {
          zero or more statements
       }
       else
       {
          zero or more statements
       }

If 'boolean_expression' evaluates to a non-zero value, the statements inside that '{' '}' block are executed. If none of the boolean expressions evaluates to true (and if there is an 'else' portion) the statements inside the 'else' portion are executed. For a more precise definition of boolean_expression see the discussion in the next section.

It is important to note that statements here has the exact same meaning as is did here. This means that you can place any number of any type of statement inside your conditional statements.

#### boolean_expression

There are two situations where a boolean_expression is used:

- inside the 'while' portion of the formula section
- with an if or elseif conditional statement

A boolean_expression will be one of:

- boolean_expression && boolean_expression
- boolean_expression || boolean_expression
- ( boolean_expression )
- !boolean_expression
- double_expression < double_expression
- double_expression <= double_expression
- double_expression > double_expression
- double_expression >= double_expression
- double_expression == double_expression
- double_expression != double_expression

Here are some examples of boolean_expressions:

- $x > 2 || sin($a) < 0
- 1 == 2 - 1
- fmod(10, $a) <= fmod(10, $b * 2)

#### for statement

The format of the for statement is as follows:

    for( statement_list ; boolean_expression; statement_list )
    {
       zero or more statements
    }

A statement_list is a comma separated list of set_color or assignment statements. The for statement:

- Will execute the first statement_list exactly once, before it does anything else
- The statement will execute everything in its block '{' '}' so long as the boolean_expression remains true
- At the end of each pass through the statements in the block it will execute the other statement list

Here is an example

       $a = 0;

       for($i = $j = 0; $i < 10; $i ++, $j ++)
       {
          $a = $i + $j;
       }

Note that the for statements' statement lists cannot contain conditional statements (if/elseif/else) or other for statements. All other statement types are allowed.

#### print/println statement

This statement can be used when you'd like to print out the values of variables inside your formula file. The println statement is just like the print statement except it automatically adds a newline to the end of everything it prints.

The format of the print statement is:

       print("filename", print_statement_arguments);

       or

       print(print_statement_arguments);

If "filename" is supplied the output will be printed to the specified file. If no "filename" is supplied the output will be written to the console (which goes off into oblivion on win32 so use "filename" if you aren't running on unix).

The print_statement_arguments. are a set of values separated by plus signs. They can be literal strings, double variable names or complex variable names.

Here are some examples:

          println("i: " + $i + ", j: " + $j);
          print("Hello, World!\n"); // the '\n' appends a new line
          println("temp.txt", "this is written to the temp.txt file");

#### break/continue statement

These statements are only allowed inside a for statement or within the while section of the formula file,

The break statement causes the termination of the for or while loop, the continue statement causes the remainder of the statements within the for or while loop to be skipped and control returns back to the beginning of the loop.

Here are some examples:

       for($i = 0; $i < 10; $i ++)
       {
          if($i == 2) { break; }
          println("i: " + $i);
       }

       println("done");

       // The output from the above will be:
       // i: 0
       // i: 1
       // done

       for($i = 0; $i < 4; $i ++)
       {
          if($i == 2) { continue; }
          println("i: " + $i);
       }

       println("done");

       // The output from the above will be:
       // i: 0
       // i: 1
       // i: 3
       // done

Note that the 'break' or 'continue' only affects the inner most loop so in this example:

       for($i = 0; $i < 2; $i ++)
       {
          for($j = 0; $j < 2; $j ++)
          {
             if($j == 0) { break; }
          }
       }

The 'break' will terminate the inner loop but not the outer one.