discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

If Statement problem

E
eexpss
Thu, Jan 12, 2017 10:20 AM

code here:

module bracket(has_pillar){
adjz=1;
if(has_pillar){adjz=hoffset;}
echo(has_pillar = has_pillar, adjz = adjz, hoffset = hoffset);
translate([0,0,adjz]){
.....

when run, I got:

Compiling design (CSG Tree generation)...
ECHO: has_pillar = true, adjz = 1, hoffset = 4.9
ECHO: has_pillar = false, adjz = 1, hoffset = 4.9
Compiling design (CSG Products generation)...

sometimes I can see in console:
ignore invalid adjz, like this message.

I use nightly version.

--
View this message in context: http://forum.openscad.org/If-Statement-problem-tp20043.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

code here: module bracket(has_pillar){ adjz=1; if(has_pillar){adjz=hoffset;} echo(has_pillar = has_pillar, adjz = adjz, hoffset = hoffset); translate([0,0,adjz]){ ..... when run, I got: Compiling design (CSG Tree generation)... ECHO: has_pillar = true, adjz = 1, hoffset = 4.9 ECHO: has_pillar = false, adjz = 1, hoffset = 4.9 Compiling design (CSG Products generation)... *sometimes* I can see in console: ignore invalid *adjz*, like this message. I use nightly version. -- View this message in context: http://forum.openscad.org/If-Statement-problem-tp20043.html Sent from the OpenSCAD mailing list archive at Nabble.com.
NH
nop head
Thu, Jan 12, 2017 10:25 AM

The new adjz that you define in braces after the if only has the scope of
the braces. You can't use if to achieve the result you want. You need to
use a conditional expression, like this:

adjz = has_pillar ? hoffset : 1;

On 12 January 2017 at 10:20, eexpss eexpss@gmail.com wrote:

code here:

module bracket(has_pillar){
adjz=1;
if(has_pillar){adjz=hoffset;}
echo(has_pillar = has_pillar, adjz = adjz, hoffset = hoffset);
translate([0,0,adjz]){
.....

when run, I got:

Compiling design (CSG Tree generation)...
ECHO: has_pillar = true, adjz = 1, hoffset = 4.9
ECHO: has_pillar = false, adjz = 1, hoffset = 4.9
Compiling design (CSG Products generation)...

sometimes I can see in console:
ignore invalid adjz, like this message.

I use nightly version.

--
View this message in context: http://forum.openscad.org/If-
Statement-problem-tp20043.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

The new adjz that you define in braces after the if only has the scope of the braces. You can't use if to achieve the result you want. You need to use a conditional expression, like this: adjz = has_pillar ? hoffset : 1; On 12 January 2017 at 10:20, eexpss <eexpss@gmail.com> wrote: > code here: > > module bracket(has_pillar){ > adjz=1; > if(has_pillar){adjz=hoffset;} > echo(has_pillar = has_pillar, adjz = adjz, hoffset = hoffset); > translate([0,0,adjz]){ > ..... > > when run, I got: > > Compiling design (CSG Tree generation)... > ECHO: has_pillar = true, adjz = 1, hoffset = 4.9 > ECHO: has_pillar = false, adjz = 1, hoffset = 4.9 > Compiling design (CSG Products generation)... > > *sometimes* I can see in console: > ignore invalid *adjz*, like this message. > > I use nightly version. > > > > > -- > View this message in context: http://forum.openscad.org/If- > Statement-problem-tp20043.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
E
eexpss
Thu, Jan 12, 2017 10:43 AM

your code really works.

I always thought that this grammar is the same as the C language. So it has
different.

--
View this message in context: http://forum.openscad.org/If-Statement-problem-tp20043p20045.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

your code really works. I always thought that this grammar is the same as the C language. So it has different. -- View this message in context: http://forum.openscad.org/If-Statement-problem-tp20043p20045.html Sent from the OpenSCAD mailing list archive at Nabble.com.
N
nophead
Thu, Jan 12, 2017 10:52 AM

The syntax is C like but the semantics are different because there are no
variables in OpenSCAD, only named constants that can only have one value for
the entire script.

Note also that the conditional assignment works in C as well.

--
View this message in context: http://forum.openscad.org/If-Statement-problem-tp20043p20047.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

The syntax is C like but the semantics are different because there are no variables in OpenSCAD, only named constants that can only have one value for the entire script. Note also that the conditional assignment works in C as well. -- View this message in context: http://forum.openscad.org/If-Statement-problem-tp20043p20047.html Sent from the OpenSCAD mailing list archive at Nabble.com.