Statements


color assignment statement

The color assignment statement can take one of three forms:

In all 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 rgb color assignment would be:

   set_color(255, 0, 0);

This example sets the current pixel to red. Some final notes on the two

Finally, here's what an hsv color assignment would look like:

   set_color_hsv(1, 100, 200);
Some final notes on the color assignment statements:

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:

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

Here are some examples of double variable assignment statements:

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:

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

Here are some examples of complex variable assignment statements:

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.

switch statement

The switch statement is similar to the if/elseif/else construct above. Here is the general format:

   switch(double_expression)
   {
      case double_expression:
      {
         zero_or_more_statements
      }

      case double_expression1: case double_expression2:
      {
         zero_or_more_statements
      }
      break;

      default:
      {
         zero_or_more_statements
      }
   }

The value supplied as 'switch(double_expression)' is first evaluated and then compared to each value listed after the 'case' keywords. If a match is found then the statements within the '{}' under that case section are executed. If a match is found for a given case section and that section has no 'break' after it then the statements under the next case section will also be evaluated and so on until a section followed by a 'break' is found.

Lastly, if no match is found within any of the 'case' sections and if there is a 'default' section then the statements in the 'default' section will be executed.

boolean_expression

There are two situations where a boolean_expression is used:

A boolean_expression will be one of:

Here are some examples of boolean_expressions:

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:

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 items separated by '.' characters. They can be literal strings, double expressions or complex expressions.

Here are some examples:

      println("v1: " . $i * 6 . ", v2: " . sqrt($j) ^ 11);
      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.