discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Variable range in a FOR loop (?)

JT
jpm2nice-tech@yahoo.fr
Thu, Oct 23, 2025 9:46 AM

Hi, gentlemen,

Would somebody explain to me why this piece of code doesnt do what I am expecting?

p=0;
for(n=[1:1:10]){
    p=1-p;
    echo(p);
}

P is the parity of n. I expect 1,0,1,0… but I obtain 1,1,1…

In my mind it is a simple “global variable”. I should be able to modify it anywhere.

How can I get the parity by an other mean?

Thanks for any clue on the subject.

Hi, gentlemen, Would somebody explain to me why this piece of code doesnt do what I am expecting? ``` p=0; for(n=[1:1:10]){ p=1-p; echo(p); } ``` P is the parity of n. I expect 1,0,1,0… but I obtain 1,1,1… In my mind it is a simple “global variable”. I should be able to modify it anywhere. How can I get the parity by an other mean? Thanks for any clue on the subject.
DP
Dan Perry
Thu, Oct 23, 2025 10:04 AM

OpenScad doesn't allow changing the value of p.  Others will explain better
than I.
Here's a solution:

for (n=[1:10]){
p = n%2;
echo(p);
}

Dan

On Thu, Oct 23, 2025 at 10:46 AM jpm2nice-tech--- via Discuss <
discuss@lists.openscad.org> wrote:

Hi, gentlemen,

Would somebody explain to me why this piece of code doesnt do what I am
expecting?

p=0;
for(n=[1:1:10]){
p=1-p;
echo(p);
}

P is the parity of n. I expect 1,0,1,0… but I obtain 1,1,1…

In my mind it is a simple “global variable”. I should be able to modify it
anywhere.

How can I get the parity by an other mean?

Thanks for any clue on the subject.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

OpenScad doesn't allow changing the value of p. Others will explain better than I. Here's a solution: for (n=[1:10]){ p = n%2; echo(p); } Dan On Thu, Oct 23, 2025 at 10:46 AM jpm2nice-tech--- via Discuss < discuss@lists.openscad.org> wrote: > Hi, gentlemen, > > Would somebody explain to me why this piece of code doesnt do what I am > expecting? > > p=0; > for(n=[1:1:10]){ > p=1-p; > echo(p); > } > > > P is the parity of n. I expect 1,0,1,0… but I obtain 1,1,1… > > In my mind it is a simple “global variable”. I should be able to modify it > anywhere. > > How can I get the parity by an other mean? > > Thanks for any clue on the subject. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org
RW
Rogier Wolff
Thu, Oct 23, 2025 10:58 AM

On Thu, Oct 23, 2025 at 09:46:19AM +0000, jpm2nice-tech--- via Discuss wrote:

Hi, gentlemen,

Would somebody explain to me why this piece of code doesnt do what I am expecting?

p=0;
for(n=[1:1:10]){
    p=1-p;
    echo(p);
}

You cannot modify a variable. You can declare a new variable inside
a different scope.

For us humans it is confusing to have two different variables called
"p" but the computer/openscad knows what it's doing. So what you're
actually writing is:

p1 = 0;
for(n=[1:1:10]){
p2 = 1-p1;  (*)
echo(p2);
}

In your code you are on that line creating a new variable "p" that is
used in the rest of that scope and it's value is used if you mention
"p". On THAT line you can still use the OLD (outer) version of "p".

In openscad you can NEVER reassign a new value to a variable.

Roger. 

--
** R.E.Wolff@BitWizard.nl ** https://www.BitWizard.nl/ ** +31-15-2049110 **
** Verl. Spiegelmakerstraat 37 2645 LZ  Delfgauw, The Netherlands.
** KVK: 27239233    **
f equals m times a. When your f is steady, and your m is going down
your a** is going up.  -- Chris Hadfield about flying up the space shuttle.
**  'a' for accelleration.

On Thu, Oct 23, 2025 at 09:46:19AM +0000, jpm2nice-tech--- via Discuss wrote: > Hi, gentlemen, > > Would somebody explain to me why this piece of code doesnt do what I am expecting? > > ``` > p=0; > for(n=[1:1:10]){ > p=1-p; > echo(p); > } You cannot modify a variable. You can declare a new variable inside a different scope. For us humans it is confusing to have two different variables called "p" but the computer/openscad knows what it's doing. So what you're actually writing is: p1 = 0; for(n=[1:1:10]){ p2 = 1-p1; (*) echo(p2); } In your code you are on that line creating a new variable "p" that is used in the rest of that scope and it's value is used if you mention "p". On THAT line you can still use the OLD (outer) version of "p". In openscad you can NEVER reassign a new value to a variable. Roger. -- ** R.E.Wolff@BitWizard.nl ** https://www.BitWizard.nl/ ** +31-15-2049110 ** ** Verl. Spiegelmakerstraat 37 2645 LZ Delfgauw, The Netherlands. ** KVK: 27239233 ** f equals m times a. When your f is steady, and your m is going down your a** is going up. -- Chris Hadfield about flying up the space shuttle. ** 'a' for accelleration.
JT
jpm2nice-tech@yahoo.fr
Thu, Oct 23, 2025 1:09 PM

Thanks a lot to you all.

I understand now a few problems I had in the past!

Jean-Pierre

Thanks a lot to you all. I understand now a few problems I had in the past! Jean-Pierre
MM
Michael Marx (spintel)
Thu, Oct 23, 2025 1:20 PM

P is the parity of N? You do not reference n?

As others have said, inside the for(), p= males a new scope for p, which will always be p= (1-(p the outer cope p==0) = 1 - 0 = 1

Think of for() as making a tree structure of sub-objects, in each of your 10 sub-tree-branches, p=outside-p=0, inside-p=1-outside-p=1-0=1.
Each sub-tree-branch has a different n 1..10.

Do:

p=0;

for(n=[1:1:10]){

p=1-p;

echo(p,n);

translate([n*3,n,0])

    cube([p,p,n]);

}

[Hopefully that survives formatting...]

Then do F5 & Design/CSG-tree, it will show you the components that for() makes.


From: jpm2nice-tech--- via Discuss [mailto:discuss@lists.openscad.org]
Sent: Thursday, October 23, 2025 8:46 PM
To: discuss@lists.openscad.org
Cc: jpm2nice-tech@yahoo.fr
Subject: [OpenSCAD] Variable range in a FOR loop (?)

Hi, gentlemen,

Would somebody explain to me why this piece of code doesnt do what I am expecting?

p=0;
for(n=[1:1:10]){
p=1-p;
echo(p);
}

P is the parity of n. I expect 1,0,1,0… but I obtain 1,1,1…

In my mind it is a simple “global variable”. I should be able to modify it anywhere.

How can I get the parity by an other mean?

Thanks for any clue on the subject.

P is the parity of N? You do not reference n? As others have said, inside the for(), p= males a new scope for p, which will always be p= (1-(p the outer cope p==0) = 1 - 0 = 1 Think of for() as making a tree structure of sub-objects, in each of your 10 sub-tree-branches, p=outside-p=0, inside-p=1-outside-p=1-0=1. Each sub-tree-branch has a different n 1..10. Do: p=0; for(n=[1:1:10]){ p=1-p; echo(p,n); translate([n*3,n,0]) cube([p,p,n]); } [Hopefully that survives formatting...] Then do F5 & Design/CSG-tree, it will show you the components that for() makes. _____ From: jpm2nice-tech--- via Discuss [mailto:discuss@lists.openscad.org] Sent: Thursday, October 23, 2025 8:46 PM To: discuss@lists.openscad.org Cc: jpm2nice-tech@yahoo.fr Subject: [OpenSCAD] Variable range in a FOR loop (?) Hi, gentlemen, Would somebody explain to me why this piece of code doesnt do what I am expecting? p=0; for(n=[1:1:10]){ p=1-p; echo(p); } P is the parity of n. I expect 1,0,1,0… but I obtain 1,1,1… In my mind it is a simple “global variable”. I should be able to modify it anywhere. How can I get the parity by an other mean? Thanks for any clue on the subject.
JB
Jordan Brown
Thu, Oct 23, 2025 4:11 PM
There's an FAQ entry: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/FAQ#Why_am_I_getting_an_error_when_writing_a_=_a_+_1? See also the first page of the manual: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables_cannot_be_changed
GH
gene heskett
Thu, Oct 23, 2025 4:28 PM

On 10/23/25 12:12, Jordan Brown via Discuss wrote:

The is one exception to that no changes rule. what ever you use for the
for(n=[ in a for loop, the n is a variable that can be used within the
scope of the for's { code list loop } which can even include another
"for" loop. I do that every time I render a threaded bolt.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Cheers, Gene Heskett, CET.

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.

  • Louis D. Brandeis
    Don't poison our oceans, interdict drugs at the src.
On 10/23/25 12:12, Jordan Brown via Discuss wrote: > There's an FAQ entry: > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/FAQ#Why_am_I_getting_an_error_when_writing_a_=_a_+_1? > > See also the first page of the manual: > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables_cannot_be_changed The is one exception to that no changes rule. what ever you use for the for(n=[ in a for loop, the n is a variable that can be used within the scope of the for's { code list loop } which can even include another "for" loop. I do that every time I render a threaded bolt. > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org Cheers, Gene Heskett, CET. -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author, 1940) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis Don't poison our oceans, interdict drugs at the src.
FH
Father Horton
Thu, Oct 23, 2025 4:36 PM

Technically the variable does not change even in a FOR loop. Each iteration
is a new scope.

On Thu, Oct 23, 2025 at 11:28 AM gene heskett via Discuss <
discuss@lists.openscad.org> wrote:

On 10/23/25 12:12, Jordan Brown via Discuss wrote:

There's an FAQ entry:

See also the first page of the manual:

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables_cannot_be_changed
The is one exception to that no changes rule. what ever you use for the
for(n=[ in a for loop, the n is a variable that can be used within the
scope of the for's { code list loop } which can even include another
"for" loop. I do that every time I render a threaded bolt.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Cheers, Gene Heskett, CET.

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.

  • Louis D. Brandeis
    Don't poison our oceans, interdict drugs at the src.

OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Technically the variable does not change even in a FOR loop. Each iteration is a new scope. On Thu, Oct 23, 2025 at 11:28 AM gene heskett via Discuss < discuss@lists.openscad.org> wrote: > On 10/23/25 12:12, Jordan Brown via Discuss wrote: > > There's an FAQ entry: > > > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/FAQ#Why_am_I_getting_an_error_when_writing_a_=_a_+_1 > ? > > > > See also the first page of the manual: > > > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables_cannot_be_changed > The is one exception to that no changes rule. what ever you use for the > for(n=[ in a for loop, the n is a variable that can be used within the > scope of the for's { code list loop } which can even include another > "for" loop. I do that every time I render a threaded bolt. > > _______________________________________________ > > OpenSCAD mailing list > > To unsubscribe send an email to discuss-leave@lists.openscad.org > > Cheers, Gene Heskett, CET. > -- > "There are four boxes to be used in defense of liberty: > soap, ballot, jury, and ammo. Please use in that order." > -Ed Howdershelt (Author, 1940) > If we desire respect for the law, we must first make the law respectable. > - Louis D. Brandeis > Don't poison our oceans, interdict drugs at the src. > > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >
RW
Rogier Wolff
Thu, Oct 23, 2025 5:20 PM

On Thu, Oct 23, 2025 at 12:28:42PM -0400, gene heskett via Discuss wrote:

On 10/23/25 12:12, Jordan Brown via Discuss wrote:

The is one exception to that no changes rule. what ever you use for the
for(n=[ in a for loop, the n is a variable that can be used within the scope
of the for's { code list loop } which can even include another "for" loop. I
do that every time I render a threaded bolt.

It is not really an exception.

the

for (n=[1,2,3]) {
// something
}

loop creates three contexts with the contents of what's inside the
braces. With thee different n variables and a different value....

Roger. 

--
** R.E.Wolff@BitWizard.nl ** https://www.BitWizard.nl/ ** +31-15-2049110 **
** Verl. Spiegelmakerstraat 37 2645 LZ  Delfgauw, The Netherlands.
** KVK: 27239233    **
f equals m times a. When your f is steady, and your m is going down
your a** is going up.  -- Chris Hadfield about flying up the space shuttle.
**  'a' for accelleration.

On Thu, Oct 23, 2025 at 12:28:42PM -0400, gene heskett via Discuss wrote: > On 10/23/25 12:12, Jordan Brown via Discuss wrote: > > There's an FAQ entry: > > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/FAQ#Why_am_I_getting_an_error_when_writing_a_=_a_+_1? > > > > See also the first page of the manual: > > https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables_cannot_be_changed > The is one exception to that no changes rule. what ever you use for the > for(n=[ in a for loop, the n is a variable that can be used within the scope > of the for's { code list loop } which can even include another "for" loop. I > do that every time I render a threaded bolt. It is not really an exception. the for (n=[1,2,3]) { // something } loop creates three contexts with the contents of what's inside the braces. With thee different n variables and a different value.... Roger. -- ** R.E.Wolff@BitWizard.nl ** https://www.BitWizard.nl/ ** +31-15-2049110 ** ** Verl. Spiegelmakerstraat 37 2645 LZ Delfgauw, The Netherlands. ** KVK: 27239233 ** f equals m times a. When your f is steady, and your m is going down your a** is going up. -- Chris Hadfield about flying up the space shuttle. ** 'a' for accelleration.
GH
gene heskett
Thu, Oct 23, 2025 6:39 PM

On 10/23/25 12:36, Father Horton wrote:

Technically the variable does not change even in a FOR loop. Each iteration
is a new scope.

which I assume then allocates more memory. Which could explain why, at
around a couple hundred renders as I write new code, things get funkity
& I have to save and restart OpenSCAD. I've only 32Gigs of main memory. 
I've wondered why, now I know. Thank you.  Must be time to get a new
main board and more memory. . .

On Thu, Oct 23, 2025 at 11:28 AM gene heskett via Discuss <
discuss@lists.openscad.org> wrote:

On 10/23/25 12:12, Jordan Brown via Discuss wrote:

There's an FAQ entry:

See also the first page of the manual:

https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables_cannot_be_changed
The is one exception to that no changes rule. what ever you use for the
for(n=[ in a for loop, the n is a variable that can be used within the
scope of the for's { code list loop } which can even include another
"for" loop. I do that every time I render a threaded bolt.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Cheers, Gene Heskett, CET.

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.
- Louis D. Brandeis
Don't poison our oceans, interdict drugs at the src.


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Cheers, Gene Heskett, CET.

"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.

  • Louis D. Brandeis
    Don't poison our oceans, interdict drugs at the src.
On 10/23/25 12:36, Father Horton wrote: > Technically the variable does not change even in a FOR loop. Each iteration > is a new scope. which I assume then allocates more memory. Which could explain why, at around a couple hundred renders as I write new code, things get funkity & I have to save and restart OpenSCAD. I've only 32Gigs of main memory.  I've wondered why, now I know. Thank you.  Must be time to get a new main board and more memory. . . > > On Thu, Oct 23, 2025 at 11:28 AM gene heskett via Discuss < > discuss@lists.openscad.org> wrote: > >> On 10/23/25 12:12, Jordan Brown via Discuss wrote: >>> There's an FAQ entry: >>> >> https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/FAQ#Why_am_I_getting_an_error_when_writing_a_=_a_+_1 >> ? >>> See also the first page of the manual: >>> >> https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Variables_cannot_be_changed >> The is one exception to that no changes rule. what ever you use for the >> for(n=[ in a for loop, the n is a variable that can be used within the >> scope of the for's { code list loop } which can even include another >> "for" loop. I do that every time I render a threaded bolt. >>> _______________________________________________ >>> OpenSCAD mailing list >>> To unsubscribe send an email to discuss-leave@lists.openscad.org >> Cheers, Gene Heskett, CET. >> -- >> "There are four boxes to be used in defense of liberty: >> soap, ballot, jury, and ammo. Please use in that order." >> -Ed Howdershelt (Author, 1940) >> If we desire respect for the law, we must first make the law respectable. >> - Louis D. Brandeis >> Don't poison our oceans, interdict drugs at the src. >> >> _______________________________________________ >> OpenSCAD mailing list >> To unsubscribe send an email to discuss-leave@lists.openscad.org >> Cheers, Gene Heskett, CET. -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author, 1940) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis Don't poison our oceans, interdict drugs at the src.