# OpenSCAD User Manual/Conditional and Iterator Functions

### For LoopEdit

Iterate over the values in a vector or range.

Vector version: for (variable=<vector>) <do_something> - <variable> is assigned to each successive value in the vector
Range version: for (variable=<range>) <do_something>

Range: [<start>:<end>] - iterate from start to end inclusive with a fixed increment of 1. Both start and end can be fractions. It also works if end is smaller than start but as of Version 2014.03 this usage is deprecated and will produce a warning message.

Range: [<start>:<increment>:<end>] - iterate from start to end with the given increment. The increment can be a fraction.
It's valid to use a negative increment, in this case end must be smaller than (or equal to) start.

Warning: If the increment is not an even divider of <end>-<start>, the iterator value for the last iteration will be <end>-(<end>-<start> mod <increment>).

Note for Version < 2014.03: The increment is given as an absolute value and cannot be negative. If <end> is smaller than <start> the increment should remain unchanged.

Nested loops : for ( variable1 = <range or vector>, variable2 = <range or vector> ) <do something, using both variables>
for loops can be nested, just as in normal programs. A shorthand is that both iterations can be given in the same for statement

 Usage example 1 - iteration over a vector: for (z = [-1, 1]) // two iterations, z = -1, z = 1 { translate([0, 0, z]) cube(size = 1, center = false); } OpenSCAD iteration over a vector
 Usage example 2a - iteration over a range: for ( i = [0 : 5] ) { rotate( i * 360 / 6, [1, 0, 0]) translate([0, 10, 0]) sphere(r = 1); } OpenSCAD iteration over a range)
 Usage example 2b - iteration over a range specifying an increment: // Note: The middle parameter in the range designation // ('0.2' in this case) is the 'increment-by' value // Warning: Depending on the 'increment-by' value, the // real end value may be smaller than the given one. for ( i = [0 : 0.2 : 5] ) { rotate( i * 360 / 6, [1, 0, 0]) translate([0, 10, 0]) sphere(r = 1); }
 Usage example 3 - iteration over a vector of vectors (rotation): for(i = [ [ 0, 0, 0], [ 10, 20, 300], [200, 40, 57], [ 20, 88, 57] ]) { rotate(i) cube([100, 20, 20], center = true); } OpenSCAD for loop (rotation)
 Usage example 4 - iteration over a vector of vectors (translation): for(i = [ [ 0, 0, 0], [10, 12, 10], [20, 24, 20], [30, 36, 30], [20, 48, 40], [10, 60, 50] ]) { translate(i) cube([50, 15, 10], center = true); } OpenSCAD for loop (translation)

Nested loop example

for (xpos=[0:3], ypos = [2,4,6]) // do twelve iterations, using each xpos with each ypos
translate([xpos*ypos, ypos, 0]) cube([0.5, 0.5, 0.5]);

### Intersection For LoopEdit

Iterate over the values in a vector or range and take an intersection of the contents.

Note: intersection_for() is a work around because of an issue that you cannot get the expected results using a combination of the standard for() and intersection() statements. The reason is that for() do a implicit union() of the contents.

Parameters

<loop variable name>
Name of the variable to use within the for loop.
 Usage example 1 - loop over a range: intersection_for(n = [1 : 6]) { rotate([0, 0, n * 60]) { translate([5,0,0]) sphere(r=12); } } OpenSCAD Intersection for
 Usage 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); } OpenSCAD Intersection for (rotation)

### If StatementEdit

Conditionally evaluate a sub-tree.

Parameters

• The boolean expression that should be used as condition

NOTE:

Do not confuse the assignment operator '=' with the equal operator '=='

if (a=b) dosomething();  // WRONG - this will FAIL to be processed without any error message
if (a==b) dosomething(); // CORRECT - this will do something if a equals b

Usage example:

if (x > y)
{
cube(size = 1, center = false);
} else {
cube(size = 2, center = true);
}

### Assign StatementEdit

Set variables to a new value for a sub-tree.

Parameters

• The variables that should be (re-)assigned

Usage example:

for (i = [10:50])
{
assign (angle = i*360/20, distance = i*10, r = i*2)
{
rotate(angle, [1, 0, 0])
translate([0, distance, 0])
sphere(r = r);
}
}