Is there a "return statement" in Openscad, to avoid a mess of nested ifs?
Something like this (return replaces the if mess to avoid the ring being
plotted):
module plateOrRing(plate = false) {
if (plate) {
cube(20, center=true);
// RETURN prematurely so the ring is not plotted
// return();
}
// ***
// IF MESS REPLACED BY return STATEMENT:
// if (!plate) {
// ***
// if not "plate", plot a ring
translate([0, 0, 10])
difference() {
cube(20, center = true);
translate([0,0,20])
translate([0,0,-20]) scale([1,1,3]) cube(10, center = true);
}; // difference
// } // IF MESS REPLACED
} // plateOrRing()
if (plate) { // do something } else { do something else }. There is also
if() // else if() // else when you have more than two possibilities.
https://openscad.org/cheatsheet/
Dan
On Sat, Oct 28, 2023 at 5:47 PM Jim Witte jim.witte@gmail.com wrote:
Is there a "return statement" in Openscad, to avoid a mess of nested ifs?
Something like this (return replaces the if mess to avoid the ring being
plotted):
module plateOrRing(plate = false) {
if (plate) {
cube(20, center=true);
// RETURN prematurely so the ring is not plotted
// return();
}
// ***
// IF MESS REPLACED BY return STATEMENT:
// if (!plate) {
// ***
// if not "plate", plot a ring
translate([0, 0, 10])
difference() {
cube(20, center = true);
translate([0,0,20])
translate([0,0,-20]) scale([1,1,3]) cube(10, center = true);
}; // difference
// } // IF MESS REPLACED
} // plateOrRing()
OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org
On 10/28/2023 8:47 AM, Jim Witte wrote:
Is there a "return statement" in Openscad,
No.
On 10/28/2023 9:56 AM, Jordan Brown wrote:
On 10/28/2023 8:47 AM, Jim Witte wrote:
Is there a "return statement" in Openscad,
No.
While that answers your question, I should perhaps add a suggestion: if
the size of an "if" block bothers you, consider refactoring it out into
its own module.
Why isn't there a return statement?
I can't really answer that, but I can tell you that it is just the tip
of the iceberg in ways that OpenSCAD is not like most programming
languages. A return statement would make some of those differences
even more obvious.
Try this:
if (true) {
echo("a");
cube();
assert(false);
}
echo("b");
x = echo("c");
What result did you expect? What result did you get? Why?