Switch Statement

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

Switch Statement Syntax

A switch statement is used to execute a block of Fractal Science Kit fractal program statements based on the value of an integer expression. This is frequently used with enums defined in the properties section.

The syntax of the switch statement is:

switch (<IntegerExpression>) {
  case <IntegerConstant> [, <IntegerConstant>]
    <Statements>
  case <IntegerConstant> [, <IntegerConstant>]
    <Statements>
  ...
  case else
    <Statements>
}

The value of the integer expression is matched against each of the integer constants given in the case statements. If a match is found, the associated block of statements is executed. Otherwise, the block of statements associated with the (optional) case else statement is executed.

Each case statement includes a comma (,) separated list of 1 or more integer constants. These constants are typically enums defined in the properties section of the program, but they can also be const values defined in the global section, literal numbers, or an expression that evaluates to a literal number. Each of the integer constants given in the case statements must be greater than or equal to 0 and less than 1024.

Example:

z = Polar(Sample.TrapValue, Sample.TrapAngleRaw)
     
switch (SolidMapProjection) {
 
  case ProjectionTypes.Stereographic
    r = z.r*0.5
    w = 0.5 - Sqrt(0.25-r^2)
    z = PolarToComplex(z)/(1-w)
   
  case ProjectionTypes.AzimuthalEquidistant
    z.r = Asin(z.r)
    z = PolarToComplex(z)
   
  case ProjectionTypes.CircleLimit
    z.r = Atanh(z.r)
    z = PolarToComplex(z)
}

This example uses an option called SolidMapProjection to determine how to project the point z onto the complex plane.

Example:

switch (Triangle) {
  case TriangleTypes.T_60_60_60
    ang = Math.PI / 3
  case TriangleTypes.T_30_60_90
    ang = Math.PI / 6
  case TriangleTypes.T_45_45_90
    ang = Math.PI / 4
}

This example sets ang to a radian angle based on the option Triangle.

Example:

switch (Triangle) {
  case TriangleTypes.T_60_60_60: ang = Math.PI / 3
  case TriangleTypes.T_30_60_90: ang = Math.PI / 6
  case TriangleTypes.T_45_45_90: ang = Math.PI / 4
}

This example is equivalent to the previous example but uses statement separator character (:) to join the case statement with its associated block (a single statement) on the same line for readability.

 

Copyright © 2004-2019 Ross Hilbert
All rights reserved