discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

OpenSCAD IF statement not working

PF
Paul F. Sehorne
Sun, Jan 31, 2016 7:42 PM

Why does OpenSCAD http://www.openscad.orgversion 2015.03echo "0" for
the code fragment below? Instead of the expected "5".

x=0;
part="test";
if (part=="test") {x=5;}
echo (x);

Thanks,

Paul

Why does OpenSCAD <http://www.openscad.org>version 2015.03echo "0" for the code fragment below? Instead of the expected "5". x=0; part="test"; if (part=="test") {x=5;} echo (x); Thanks, -- *Paul*
NH
nop head
Sun, Jan 31, 2016 7:53 PM

Because the x in {x = 5;} is a new x that masks the global one inside the
braces.

if (part=="test") {x=5; echo(x);}

will echo 5 but I expect what you actually want is this:

part="test";
x = (part=="test") ? 5 : 0;
echo (x);

On 31 January 2016 at 19:42, Paul F. Sehorne paul@sehorne.org wrote:

Why does OpenSCAD http://www.openscad.org version 2015.03 echo "0" for
the code fragment below? Instead of the expected "5".

x=0;
part="test";
if (part=="test") {x=5;}
echo (x);

Thanks,

Paul


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

Because the x in {x = 5;} is a new x that masks the global one inside the braces. if (part=="test") {x=5; echo(x);} will echo 5 but I expect what you actually want is this: part="test"; x = (part=="test") ? 5 : 0; echo (x); On 31 January 2016 at 19:42, Paul F. Sehorne <paul@sehorne.org> wrote: > Why does OpenSCAD <http://www.openscad.org> version 2015.03 echo "0" for > the code fragment below? Instead of the expected "5". > > x=0; > part="test"; > if (part=="test") {x=5;} > echo (x); > > Thanks, > -- > *Paul* > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
F
fred
Sun, Jan 31, 2016 7:58 PM

I had to swap
? 5 : 0to read? 0 : 5to make it work.

On Sunday, January 31, 2016 2:54 PM, nop head <nop.head@gmail.com> wrote:

Because the x in {x = 5;} is a new x that masks the global one inside the braces.

if (part=="test") {x=5; echo(x);}

will echo 5 but I expect what you actually want is this:

part="test";
x = (part=="test") ? 5 : 0;
echo (x);

On 31 January 2016 at 19:42, Paul F. Sehorne paul@sehorne.org wrote:

Why does OpenSCAD version 2015.03  echo "0" for the code fragment below? Instead of the expected "5". 

x=0;
part="test";
if (part=="test") {x=5;}
echo (x);

Thanks,

Paul


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


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

I had to swap ? 5 : 0to read? 0 : 5to make it work. On Sunday, January 31, 2016 2:54 PM, nop head <nop.head@gmail.com> wrote: Because the x in {x = 5;} is a new x that masks the global one inside the braces. if (part=="test") {x=5; echo(x);} will echo 5 but I expect what you actually want is this: part="test"; x = (part=="test") ? 5 : 0; echo (x); On 31 January 2016 at 19:42, Paul F. Sehorne <paul@sehorne.org> wrote: Why does OpenSCAD version 2015.03 echo "0" for the code fragment below? Instead of the expected "5".  x=0; part="test"; if (part=="test") {x=5;} echo (x); Thanks, -- Paul _______________________________________________ OpenSCAD mailing list Discuss@lists.openscad.org http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list Discuss@lists.openscad.org http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
NH
nop head
Sun, Jan 31, 2016 8:02 PM

If you do that it will print 0 instead of 5. The OP wanted 5.

On 31 January 2016 at 19:58, fred fred_dot_u@yahoo.com wrote:

I had to swap
? 5 : 0
to read
? 0 : 5
to make it work.

On Sunday, January 31, 2016 2:54 PM, nop head nop.head@gmail.com wrote:

Because the x in {x = 5;} is a new x that masks the global one inside the
braces.

if (part=="test") {x=5; echo(x);}

will echo 5 but I expect what you actually want is this:

part="test";
x = (part=="test") ? 5 : 0;
echo (x);

On 31 January 2016 at 19:42, Paul F. Sehorne paul@sehorne.org wrote:

Why does OpenSCAD version 2015.03 echo "0" for the code fragment below?
Instead of the expected "5".

x=0;
part="test";
if (part=="test") {x=5;}
echo (x);

Thanks,

Paul


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


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


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

If you do that it will print 0 instead of 5. The OP wanted 5. On 31 January 2016 at 19:58, fred <fred_dot_u@yahoo.com> wrote: > I had to swap > ? 5 : 0 > to read > ? 0 : 5 > to make it work. > > > On Sunday, January 31, 2016 2:54 PM, nop head <nop.head@gmail.com> wrote: > > > Because the x in {x = 5;} is a new x that masks the global one inside the > braces. > > if (part=="test") {x=5; echo(x);} > > will echo 5 but I expect what you actually want is this: > > part="test"; > x = (part=="test") ? 5 : 0; > echo (x); > > > > On 31 January 2016 at 19:42, Paul F. Sehorne <paul@sehorne.org> wrote: > > Why does OpenSCAD version 2015.03 echo "0" for the code fragment below? > Instead of the expected "5". > > x=0; > part="test"; > if (part=="test") {x=5;} > echo (x); > > Thanks, > -- > *Paul* > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > >
TH
Triffid Hunter
Wed, Feb 3, 2016 6:37 AM

because OpenSCAD isn't a sequential language, it's functional.

Once a variable is declared, it remains fixed at the provided value until
it goes out of scope.

You create a new x inside the braces, set it to 5, then immediately
discard it. Your echo statement spits out the value of the toplevel x,
which you've set to zero.

OpenSCAD is not like C/Java/Python/whatever else you're used to. It's got
its own thing going on which, once you get used to it, has its own beauty.

because OpenSCAD isn't a sequential language, it's functional. Once a variable is declared, it remains fixed at the provided value until it goes out of scope. You create a *new* x inside the braces, set it to 5, then immediately discard it. Your echo statement spits out the value of the toplevel x, which you've set to zero. OpenSCAD is *not* like C/Java/Python/whatever else you're used to. It's got its own thing going on which, once you get used to it, has its own beauty.