help with 'if' statement?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

help with 'if' statement?

Chris Gray
this feels silly to me, being a programmer :)
 
but how does an if statement work in OpenSCAD?
 
I'd like to say
 
 
module foo(x)
{
      if(x == 0)
      { 
            y = 10;
      }
 
      // do work with y
}
 
 
 
when I do this I get a parser error?  if I were to call a module instead of doing an assignment everything would be fine
 
 
how would I do such a thing?
Reply | Threaded
Open this post in threaded view
|

Re: help with 'if' statement?

Giles Bathgate-2

Your use of if is correct however openscad dosent support an assign operator (yet another example of a confused user)

Regards

Giles

On 18 Sep 2010 21:10, "Chris Gray" <[hidden email]> wrote:

this feels silly to me, being a programmer :)
 
but how does an if statement work in OpenSCAD?
 
I'd like to say
 
 
module foo(x)
{
      if(x == 0)
      { 
            y = 10;
      }
 
      // do work with y
}
 
 
 
when I do this I get a parser error?  if I were to call a module instead of doing an assignment everything would be fine
 
 
how would I do such a thing?

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: help with 'if' statement?

Whosawhatsis
In reply to this post by Chris Gray
Your if statement is fine, the problem probably lies in the fact that
OpenSCAD doesn't have a real assignment operator (see the long thread
on this topic). Your best bet to do what you appear to be trying with
a minimum of duplicated code is probably to call a module with one set
of arguments if your condition is true, and a different set if it is
false.

On Sep 18, 2010, at 13:10, Chris Gray <[hidden email]> wrote:

> this feels silly to me, being a programmer :)
>
> but how does an if statement work in OpenSCAD?
>
> I'd like to say
>
>
> module foo(x)
> {
>     if(x == 0)
>     {
>           y = 10;
>     }
>
>     // do work with y
> }
>
>
>
> when I do this I get a parser error?  if I were to call a module instead of doing an assignment everything would be fine
>
>
> how would I do such a thing?
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: help with 'if' statement?

Giles Bathgate-2
In reply to this post by Chris Gray
On Sat, 2010-09-18 at 13:10 -0700, Chris Gray wrote:

> this feels silly to me, being a programmer :)
>  
> but how does an if statement work in OpenSCAD?
>  
> I'd like to say
>  
>  
> module foo(x)
> {
>       if(x == 0)
>       {  
>             y = 10;
>       }
>  
>       // do work with y
> }

How about re-writing it like this:

module foo(x)
{
    y = (x==0 ? 10 : 0);
    echo(y);
}


Regards

Giles.