# OpenSCAD User Manual/Conditional and Iterator Functions
### For loop

Evaluate each value in a range or vector, or each name in an object, applying it to the following Action.

#### For each value in a range

$_{Note:Forrange,valuesareseparatedbycolonsratherthancommasusedinvectors.}$
The Action is evaluated for each value in the range.
       start - initial value
       increment or step - amount to increase the value, optional, default = 1
       end - stop when next value would be past end
    examples:

#### For each element of a vector

The Action is evaluated for each element of the vector.
The vector can be described elsewhere, like 'for each' in other languages.

#### For each element of an object

[Note: Requires version Development snapshot]
The Action is evaluated for the name of each element of the object, in an unspecified order.

#### Notes

for is an Operator. Operators require braces if more than one Action is within its scope. Actions end in semicolons, Operators do not.
for is not an exception to the rule about variables having only one value within a scope. Each evaluation is given its own scope, allowing any variables to have unique values. No, you still can't do a=a+1;
Remember this is not an iterative language, the for does not loop in the programmatic sense, it builds a tree of objects one branch for each item in the range/vector, inside each branch the 'variable' is a specific and separate instantiation or scope.
Hence:
Produces: $_{[SeeDesign/Display-CSG-Treemenu]}$
While the group is built sequentially, all instances of the for exist as separate entities, they do not iterate one piece of code sequentially.

#### Nested for

While it is reasonable to nest multiple for statements such as:
instead, all ranges/vectors can be included in the same for operator.
    Examples using vector of vectors

### Intersection For Loop

Iterate over the values in a range or vector and create the intersection of objects created by each pass.
Besides creating separate instances for each pass, the standard for also groups all these instances creating an implicit union. intersection_for is a work around because the implicit union prevents getting the expected results using a combination of the standard for and intersection statements.
intersection_for uses the same parameters, and works the same as a For Loop, other than replacing the implicit union with an intersection.
| example 1 - loop over a range: |
| intersection_for(n = [1 : 6]) { rotate([0, 0, n * 60]) { translate([5,0,0]) sphere(r=12); } } | intersection_for | either intersection for or for intersection |
| example 2 - rotation : |
| intersection_for(i = [ [ 0, 0, 0], [ 10, 20, 300], [200, 40, 57], [ 20, 88, 57] ]) { rotate(i) cube([100, 20, 20], center = true); } | intersection_for | intersection for |

### If Statement

Performs a test to determine if the actions in a sub scope should be performed or not.
REALLY IMPORTANT. You can't change the value of Variables. If you have an assignment inside brackets, it creates a new variable that is lost as soon as you exit that scope.
    Parameters
       test: Usually a boolean expression, but can be any value or variable.
          See here for true or false state of values.
          See here for boolean and logical operators
          Do not confuse the assignment operator '=' with the equal operator '=='
       scope1: one or more actions to take when test is true.
       scope2: one or more actions to take when test is false.
Since 2015.03 variables can now be assigned in any scope. Note that assignments are only valid within the scope in which they are defined - you are still not allowed to leak values to an outer scope. See Scope of variables for more details.
    Nested if
The scopes of both the if portion and the else portion, can in turn contain if statements. This nesting can be to any depth.
When scope1 and scope2 contain only the if statement, the outer sets of braces can be removed.
One evolution is this:

#### else if

Note that else and if are two separate words. When working down the chain of tests, the first true uses its scope. All further tests are skipped.

##### example
### Conditional ? :

A function that uses a test to determine which of 2 values to return.
    Parameters
       test: Usually a boolean expression, but can be any value or variable.
          See here for true or false state of values.
          See here for boolean and logical operators
          Do not confuse assignment '=' with equal '=='
       TrueValue: the value to return when test is true.
       FalseValue: the value to return when test is false.
             A value in OpenSCAD is either a Number (like 42), a Boolean (like true), a String (like "foo"), a Vector (like [1,2,3]), or the Undefined value (undef). Values can be stored in variables, passed as function arguments, and returned as function results.
This works like the ?: operator from the family of C-like programming languages.
    Examples

#### Recursive function calls

Recursive function calls are supported. Using the Conditional "... ? ... : ... " it's possible to ensure the recursion is terminated. Note: There is a built-in recursion limit to prevent an application crash. If the limit is hit, the function returns undef.
    example

#### Formatting complex usage

Multiple nested conditionals can become difficult to understand. Formatting them like multi-line indented "if/else" statements is clearer.

### Assign Statement

[Deprecated: assign will be removed in future releases. Variables can now be assigned anywhere. If you prefer this way of setting values, the new Let Statement can be used instead.]
Set variables to a new value for a sub-tree.
    Parameters
       The variables that should be (re-)assigned
    example:

### Let Statement

[Note: Requires version 2019.05]
Set variables to a new value for a sub-tree. The parameters are evaluated sequentially and may depend on each other (as opposed to the deprecated assign statement).
    Parameters
       The variables that should be set
    example: