For in OpenSCAD

OpenSCAD doesn’t have traditional looping constructs like most programming languages. Instead, it uses functional programming concepts and built-in modules for repetition. Here are some ways to achieve similar functionality to the Go “for” loops in OpenSCAD:

// The most basic type, with a single condition.
// In OpenSCAD, we use a recursive module
module basic_loop(i) {
    if (i <= 3) {
        echo(i);
        basic_loop(i + 1);
    }
}
basic_loop(1);

// A classic initial/condition/after loop.
// In OpenSCAD, we use the built-in for loop
for (j = [0:2]) {
    echo(j);
}

// Accomplishing the basic "do this N times" iteration.
// OpenSCAD's for loop can be used directly
for (i = [0:2]) {
    echo("range", i);
}

// OpenSCAD doesn't have an infinite loop concept.
// The closest equivalent would be a recursive module with a manual break
module infinite_loop() {
    echo("loop");
    // Uncomment the next line to actually make it infinite
    // infinite_loop();
}
infinite_loop();

// Demonstrating continue-like behavior
for (n = [0:5]) {
    if (n % 2 == 0) {
        continue;
    }
    echo(n);
}

To run this OpenSCAD script, save it to a file with a .scad extension and open it in the OpenSCAD application. The echo statements will output to the console in OpenSCAD.

Note that OpenSCAD is primarily designed for creating 3D models, so these examples don’t produce any visual output. They demonstrate the logical flow and how to achieve similar functionality to traditional for loops in OpenSCAD’s functional paradigm.

OpenSCAD’s approach to iteration is quite different from imperative languages. It uses functional programming concepts and is designed for describing 3D models rather than general-purpose programming. The for loop in OpenSCAD is used primarily for generating repeated geometry, but we’ve used it here to demonstrate similar logical flow to the original example.