Previous:The #ifdef and #ifndef Directives   Main Index   Next:The #while...#end Directive



The #switch, #case, #range and #break Directives

A more powerful conditional is the #switch directive. The syntax is as follows...

SWITCH_DIRECTIVE:
#switch ( Switch_Value ) SWITCH_CLAUSE... [#else TOKENS...] #end
SWITCH_CLAUSE:
#case( Case_Value ) TOKENS... [#break] |
#range( Low_Value , High_Value ) TOKENS... [#break]

The TOKENS are any number of POV-Ray keyword, identifiers, or punctuation and ( Switch_Value ) is a float expression. The parentheses are required. The #end directive is required. The SWITCH_CLAUSE comes in two varieties. In the #case variety, the float Switch_Value is compared to the float Case_Value. If they are equal, the condition is true. Note that values whose difference is less than 1e-10 are considered equal in case of round off errors. In the #range variety, Low_Value Switch and High_Value are floats separated by a comma and enclosed in parentheses. If Low_Value <= Switch_Value and Switch_Value<=High_Value then the condition is true.

In either variety, if the clause's condition is true, that clause's tokens are parsed normally and parsing continues until a #break, #else or #end directive is reached. If the condition is false, POV-Ray skips until another #case or #range is found.

There may be any number of #case or #range clauses in any order you want. If a clause evaluates true but no #break is specified, the parsing will fall through to the next #case or #range and that clause conditional is evaluated. Hitting #break while parsing a successful section causes an immediate jump to the #end without processing subsequent sections, even if a subsequent condition would also have been satisfied.

An optional #else clause may be the last clause. It is only executed if the clause before it was a false clause.

Here is an example:

 #switch (VALUE)

  #case (TEST_1)

   // This section is parsed if VALUE=TEST_1

  #break //First case ends

  #case (TEST_2)

   // This section is parsed if VALUE=TEST_2

  #break //Second case ends

  #range (LOW_1,HIGH_1)

   // This section is parsed if (VALUE>=LOW_1)&(VALUE<=HIGH_1)

  #break //Third case ends

  #range (LOW_2,HIGH_2)

   // This section is parsed if (VALUE>=LOW_2)&(VALUE<=HIGH_2)

  #break //Fourth case ends

  #else

   // This section is parsed if no other case or

   // range is true.

 #end // End of conditional part


Previous:The #ifdef and #ifndef Directives   Main Index   Next:The #while...#end Directive