Something annoying about "C-like for"

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

Something annoying about "C-like for"

Ronaldo
The C-like for has the following general form:

[for( initialization;
    test;
      iteration ) output ]

The output is done always after each test and the for escape just after the test before the output. So, a better understanding of the control structure is grasped by:

[ for( i = 0,
       initialization;
     i < 8;
       (output(i))
       i = i+1,
       iteration ) ] 

 where I suppose a simple iteration and test. In above example, we will have 8 outputs and 8 iterations besides the initialization. However, the last iteration does not produces any output. When the iterations include very demanding processes or illegal operations firing warnings we need to do tests in the iteration steps in order to avoid them.

All that could be avoided if the output was done always before the test. 

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

nophead
Isn't it exactly the same as C? The iteration part is done after the loop body is executed and the test is done before it.

E.g.
initialisation;
while(test) {
    ....
   iteration;
}

So iteration is executed the same number of times as the body and the last case fails the test.

On Sat, 1 Feb 2020 at 13:42, Ronaldo Persiano <[hidden email]> wrote:
The C-like for has the following general form:

[for( initialization;
    test;
      iteration ) output ]

The output is done always after each test and the for escape just after the test before the output. So, a better understanding of the control structure is grasped by:

[ for( i = 0,
       initialization;
     i < 8;
       (output(i))
       i = i+1,
       iteration ) ] 

 where I suppose a simple iteration and test. In above example, we will have 8 outputs and 8 iterations besides the initialization. However, the last iteration does not produces any output. When the iterations include very demanding processes or illegal operations firing warnings we need to do tests in the iteration steps in order to avoid them.

All that could be avoided if the output was done always before the test. 
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

adrianv
Well....kind of.   The attraction of this loop structure is that you can
assign and reassign variables in the iteration part.  And while it may look
C-like, OpenSCAD is not C.  In OpenSCAD the loop body isn't "executed" it is
output.   In C you would actually do your computations in the output part at
the end.  But the whole reason to use this structure in OpenSCAD is so you
can reassign variables.  This means the computation all happens in the
iteration block.  Here's a simple example:

// Produces length n vector counting from 0
function example(n) =
  [each  // Needed to avoid singleton list output
     for(output=[], i=0;
         i<=n;
         output=concat(output,[i]),  // Here's the computation!
                 i=i+1,
                dummy=echo(i=i,output=output))
       if (i==n) output   // Output only in the last iteration
   ];

Note how I can repeatedly add to the output variable without the need for
recursion?  So there are two potential issues.  One issue is in getting the
answer out.  Here you see I need an "if" to output the answer when the
computation is done with a duplicated test for loop termination, which is
easy to detect since the termination test is very simple in this case.  The
second issue as Rinaldo notes is that the iteration block runs an extra
time---it must run when i=n even though I don't want output for this case.
So this can produce an error, or it can waste time.  

So running echo(example(4)) produces:

ECHO: i = 1, output = [0]
ECHO: i = 2, output = [0, 1]
ECHO: i = 3, output = [0, 1, 2]
ECHO: i = 4, output = [0, 1, 2, 3]
ECHO: i = 5, output = [0, 1, 2, 3, 4]
ECHO: [0, 1, 2, 3]


nophead wrote

> Isn't it exactly the same as C? The iteration part is done after the loop
> body is executed and the test is done before it.
>
> E.g.
> initialisation;
> while(test) {
>     ....
>    iteration;
> }
>
> So iteration is executed the same number of times as the body and the last
> case fails the test.
>
> On Sat, 1 Feb 2020 at 13:42, Ronaldo Persiano &lt;

> rcmpersiano@

> &gt; wrote:
>
>> The C-like for has the following general form:
>>
>> [for( initialization;
>>     test;
>>       iteration ) output ]
>>
>>
>> The output is done always after each test and the for escape just after
>> the test before the output. So, a better understanding of the control
>> structure is grasped by:
>>
>> [ for( i = 0,
>>        initialization;
>>
>>      i < 8;
>>
>>        (output(i))
>>
>>        i = i+1,
>>
>>        iteration ) ]
>>
>>
>>  where I suppose a simple iteration and test. In above example, we will
>> have 8 outputs and 8 iterations besides the initialization. However, the
>> last iteration does not produces any output. When the iterations include
>> very demanding processes or illegal operations firing warnings we need to
>> do tests in the iteration steps in order to avoid them.
>>
>> All that could be avoided if the output was done always before the test.
>> _______________________________________________
>> OpenSCAD mailing list
>>

> Discuss@.openscad

>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>
> _______________________________________________
> OpenSCAD mailing list

> Discuss@.openscad

> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

nophead
Perhaps to fix Ronaldo's issue allow comma separated expressions in the test part that only get executed if the test passes, before the output.



On Sat, 1 Feb 2020 at 14:50, adrianv <[hidden email]> wrote:
Well....kind of.   The attraction of this loop structure is that you can
assign and reassign variables in the iteration part.  And while it may look
C-like, OpenSCAD is not C.  In OpenSCAD the loop body isn't "executed" it is
output.   In C you would actually do your computations in the output part at
the end.  But the whole reason to use this structure in OpenSCAD is so you
can reassign variables.  This means the computation all happens in the
iteration block.  Here's a simple example:

// Produces length n vector counting from 0
function example(n) =
  [each  // Needed to avoid singleton list output
     for(output=[], i=0;
         i<=n;
         output=concat(output,[i]),  // Here's the computation!
                 i=i+1,
                dummy=echo(i=i,output=output))
       if (i==n) output   // Output only in the last iteration
   ];

Note how I can repeatedly add to the output variable without the need for
recursion?  So there are two potential issues.  One issue is in getting the
answer out.  Here you see I need an "if" to output the answer when the
computation is done with a duplicated test for loop termination, which is
easy to detect since the termination test is very simple in this case.  The
second issue as Rinaldo notes is that the iteration block runs an extra
time---it must run when i=n even though I don't want output for this case.
So this can produce an error, or it can waste time.   

So running echo(example(4)) produces:

ECHO: i = 1, output = [0]
ECHO: i = 2, output = [0, 1]
ECHO: i = 3, output = [0, 1, 2]
ECHO: i = 4, output = [0, 1, 2, 3]
ECHO: i = 5, output = [0, 1, 2, 3, 4]
ECHO: [0, 1, 2, 3]


nophead wrote
> Isn't it exactly the same as C? The iteration part is done after the loop
> body is executed and the test is done before it.
>
> E.g.
> initialisation;
> while(test) {
>     ....
>    iteration;
> }
>
> So iteration is executed the same number of times as the body and the last
> case fails the test.
>
> On Sat, 1 Feb 2020 at 13:42, Ronaldo Persiano &lt;

> rcmpersiano@

> &gt; wrote:
>
>> The C-like for has the following general form:
>>
>> [for( initialization;
>>     test;
>>       iteration ) output ]
>>
>>
>> The output is done always after each test and the for escape just after
>> the test before the output. So, a better understanding of the control
>> structure is grasped by:
>>
>> [ for( i = 0,
>>        initialization;
>>
>>      i < 8;
>>
>>        (output(i))
>>
>>        i = i+1,
>>
>>        iteration ) ]
>>
>>
>>  where I suppose a simple iteration and test. In above example, we will
>> have 8 outputs and 8 iterations besides the initialization. However, the
>> last iteration does not produces any output. When the iterations include
>> very demanding processes or illegal operations firing warnings we need to
>> do tests in the iteration steps in order to avoid them.
>>
>> All that could be avoided if the output was done always before the test.
>> _______________________________________________
>> OpenSCAD mailing list
>>

> Discuss@.openscad

>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>
> _______________________________________________
> OpenSCAD mailing list

> Discuss@.openscad

> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

Ronaldo
In reply to this post by nophead
Isn't it exactly the same as C? The iteration part is done after the loop body is executed and the test is done before it.

E.g.
initialisation;
while(test) {
    ....
   iteration;
}

So iteration is executed the same number of times as the body and the last case fails the test.

Yes, but, as adrianv pointed out, in C you may choose where the output is done: in the middle of the iteration, at the end or at the beginning (the only choice available in OpenSCAD).

Perhaps to fix Ronaldo's issue allow comma separated expressions in the test part that only get executed if the test passes, before the output.  

Would you elaborate on that with an example?

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

adrianv

> Perhaps to fix Ronaldo's issue allow comma separated expressions in the
>> test part that only get executed if the test passes, before the output.

Right now it does not appear you can put any expressions into the test.  The
test is simply a condition.  

But if you did make this change, it is basically restructuring the command
so that the computations all happen in the condition test instead of all in
the iteration block.  That seems kind of weird and confusing.  (In a comma
separated list, which thing is the condition?)  Changing when the output
occurs seems like a better solution, though it creates its own weird
behaviors, I think mainly if you try to use the for loop in a simpler way,
e.g.

function example2(n) =
  [ for(i=0;
        i<n;
        i=i+1) i];

currently outputs the same vector as my previous example, [0,...,n-1].  But
if we have the output run after the iteration, then it will instead produce
[1,...,n].  





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

nophead
In reply to this post by Ronaldo
From my own code I have:
    rotations = [for(i = 0, rot = fs_frame(tangents);
                     i <= last;
                     i = i + 1,
                     rot = i <= last ? rotate_from_to(tangents[i - 1], tangents[i]) * rot : undef) rot];

I was thinking it could be rewritten as:
    rotations = [for(i = 0, rot = fs_frame(tangents);
                     i <= last, rot = rotate_from_to(tangents[i], tangents[i + 1] ) * rot;
                     i = i + 1,
                     ) rot];

But the problem is the test gets executed before the first iteration, so it is not right. 

I think if it was written in C it would need a conditional break between the output and the calculation. I don't think it can be done with a simple for loop. I.e. it needs to be:

initialisation;
for(;;) {
   output;
   if(++i >= n)
     break;
   calculate;
}

In my case there needs to be one less calculate than there are outputs because the first output is calculated by the initialisation.


On Sat, 1 Feb 2020 at 15:14, Ronaldo Persiano <[hidden email]> wrote:
Isn't it exactly the same as C? The iteration part is done after the loop body is executed and the test is done before it.

E.g.
initialisation;
while(test) {
    ....
   iteration;
}

So iteration is executed the same number of times as the body and the last case fails the test.

Yes, but, as adrianv pointed out, in C you may choose where the output is done: in the middle of the iteration, at the end or at the beginning (the only choice available in OpenSCAD).

Perhaps to fix Ronaldo's issue allow comma separated expressions in the test part that only get executed if the test passes, before the output.  

Would you elaborate on that with an example?
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

adrianv
In C it can be done without complications something like this:

for(i=0, rot[0] = init, i<=last;i++)
  rot[i+1] = rotate_from_to(tangents[i],tangents[i+1])*rot[i];

In C you can create "output" any time you want.  You can do the same thing
in openscad using concat, but I assume that would be slow compared to your
posted approach.   In my posted example I built up the output using concat.  


nophead wrote

> From my own code I have:
>     rotations = [for(i = 0, rot = fs_frame(tangents);
>                      i <= last;
>                      i = i + 1,
>                      rot = i <= last ? rotate_from_to(tangents[i - 1],
> tangents[i]) * rot : undef) rot];
>
> I was thinking it could be rewritten as:
>     rotations = [for(i = 0, rot = fs_frame(tangents);
>                      i <= last, rot = rotate_from_to(tangents[i],
> tangents[i + 1] ) * rot;
>                      i = i + 1,
>                      ) rot];
>
> But the problem is the test gets executed before the first iteration, so
> it
> is not right.
>
> I think if it was written in C it would need a conditional break between
> the output and the calculation. I don't think it can be done with a simple
> for loop. I.e. it needs to be:
>
> initialisation;
> for(;;) {
>    output;
>    if(++i >= n)
>      break;
>    calculate;
> }
>
> In my case there needs to be one less calculate than there are outputs
> because the first output is calculated by the initialisation.
>
>
> On Sat, 1 Feb 2020 at 15:14, Ronaldo Persiano &lt;

> rcmpersiano@

> &gt; wrote:
>
>> Isn't it exactly the same as C? The iteration part is done after the loop
>>> body is executed and the test is done before it.
>>>
>>> E.g.
>>> initialisation;
>>> while(test) {
>>>     ....
>>>    iteration;
>>> }
>>>
>>> So iteration is executed the same number of times as the body and the
>>> last case fails the test.
>>>
>>
>> Yes, but, as adrianv pointed out, in C you may choose where the output is
>> done: in the middle of the iteration, at the end or at the beginning (the
>> only choice available in OpenSCAD).
>>
>> Perhaps to fix Ronaldo's issue allow comma separated expressions in the
>>> test part that only get executed if the test passes, before the output.
>>
>>
>> Would you elaborate on that with an example?
>> _______________________________________________
>> OpenSCAD mailing list
>>

> Discuss@.openscad

>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>
> _______________________________________________
> OpenSCAD mailing list

> Discuss@.openscad

> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

nophead
Yes although tangents[last+1] does not exist, so the loop needs to be one less, i.e. i < last. The first element is a specific value and the rest are calculated from the difference between two tangents, so for n tangents there are only n-1 differences.

On Sat, 1 Feb 2020 at 16:28, adrianv <[hidden email]> wrote:
In C it can be done without complications something like this:

for(i=0, rot[0] = init, i<=last;i++)
  rot[i+1] = rotate_from_to(tangents[i],tangents[i+1])*rot[i];

In C you can create "output" any time you want.  You can do the same thing
in openscad using concat, but I assume that would be slow compared to your
posted approach.   In my posted example I built up the output using concat. 


nophead wrote
> From my own code I have:
>     rotations = [for(i = 0, rot = fs_frame(tangents);
>                      i <= last;
>                      i = i + 1,
>                      rot = i <= last ? rotate_from_to(tangents[i - 1],
> tangents[i]) * rot : undef) rot];
>
> I was thinking it could be rewritten as:
>     rotations = [for(i = 0, rot = fs_frame(tangents);
>                      i <= last, rot = rotate_from_to(tangents[i],
> tangents[i + 1] ) * rot;
>                      i = i + 1,
>                      ) rot];
>
> But the problem is the test gets executed before the first iteration, so
> it
> is not right.
>
> I think if it was written in C it would need a conditional break between
> the output and the calculation. I don't think it can be done with a simple
> for loop. I.e. it needs to be:
>
> initialisation;
> for(;;) {
>    output;
>    if(++i >= n)
>      break;
>    calculate;
> }
>
> In my case there needs to be one less calculate than there are outputs
> because the first output is calculated by the initialisation.
>
>
> On Sat, 1 Feb 2020 at 15:14, Ronaldo Persiano &lt;

> rcmpersiano@

> &gt; wrote:
>
>> Isn't it exactly the same as C? The iteration part is done after the loop
>>> body is executed and the test is done before it.
>>>
>>> E.g.
>>> initialisation;
>>> while(test) {
>>>     ....
>>>    iteration;
>>> }
>>>
>>> So iteration is executed the same number of times as the body and the
>>> last case fails the test.
>>>
>>
>> Yes, but, as adrianv pointed out, in C you may choose where the output is
>> done: in the middle of the iteration, at the end or at the beginning (the
>> only choice available in OpenSCAD).
>>
>> Perhaps to fix Ronaldo's issue allow comma separated expressions in the
>>> test part that only get executed if the test passes, before the output.
>>
>>
>> Would you elaborate on that with an example?
>> _______________________________________________
>> OpenSCAD mailing list
>>

> Discuss@.openscad

>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>
> _______________________________________________
> OpenSCAD mailing list

> Discuss@.openscad

> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

Ronaldo
In reply to this post by adrianv
In C you can create "output" any time you want.  You can do the same thing
in openscad using concat, but I assume that would be slow compared to your
posted approach.   In my posted example I built up the output using concat. 

I don't see how concat may be of any help here. If your code is:

[ for(  init; // no "output" variable initialization
      test;
        iteration part 1,
        output = concat(output, [value]),
        iteration part 2
     ) if(last_iteration) each output ]

you may equivalently write:

[ for(  init; // no "output" variable initialization 
      test;
        iteration part 1,
        output = value,
        iteration part 2
     ) if( ! first output) output ]

without concat and each.

Your posted example for instance could be rewritten as:

function example(n) =
  [for(   i = 0;
        i<=n;
          output = i,  // Here's the computation!
          i = i+1//,
     )  if(i>0) output // avoids the nonexistent 
initial "output"
   ];

 What remains unsolved are the illegal or time consuming operations in the iteration section after the output computation. They have to be filtered one by one.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

adrianv
Ronaldo wrote

>>
>> In C you can create "output" any time you want.  You can do the same
>> thing
>> in openscad using concat, but I assume that would be slow compared to
>> your
>> posted approach.   In my posted example I built up the output using
>> concat.
>>
>
> I don't see how concat may be of any help here. If your code is:

My remark had to do with the ability to map the C code example I wrote that
computes nophead's list of rotations to OpenSCAD.  That example looked
something like

for(i=0, rot[0]=init_value; i<max; rot[i+1] = &lt;something> * rot[i], i++);

In this example, rot is "growing" at each iteration.  To map this code to
OpenSCAD you need a method to grow rot at each iteration.  That method is
concat.  

Note that this is really just an aside, and I don't think it's the right way
for nophead to solve his problem.   Also, as you note, it has nothing to do
with the basic problem, which is dealing with the extra evaluation of the
iteration block.  






--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

Ronaldo
The general scheme of the "C-like for" in OpenSCAD is:
 
[ for( initializations; test; computations ) output ]

where the test is done after the initializations and each iterated computation; the output is implicitly done just after the test.

As a proposal for an alternative syntax and semantic, consider:

[ for( initializations; computations; test ) output ]

where the output is implicitly done just after each iterated computation and before the test that follows it.  Note that in this form the initializations are not special initial computations as happens in the current form. They produce no output.
 
To not break current codes, both forms would be accepted, each one with its own semantics. 

With that alternative form, nophead's rotations could be expressed as:

rot0 = init_rot(tangents); 
rotations = [rot0, for( i = 0;
                          i = i+1,
                          rot = rft(i)*rot;
                        i < last) rot];

without any extra computations to be filtered but with the special initial case being dealt out of the "for". Other possibility would be:

rotations = [for( i = 0;
                    rot = i==0 ? init_rot(tangents) : rft(i)*rot,
                    i = i+1;
                  i <= last) rot];

I agree that the current syntax/semantics would be more natural for the semantics of the alternative form once the output comes syntactically just after the computations (and that was my first understanding of it). But there is no way back.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Something annoying about "C-like for"

RevarBat
In reply to this post by nophead
I would love comma separated subexpressions in the conditional.  Because otherwise, I end up with things like:

faces = [
for(
first = true,
finishing = false,
finished = false,
plen1 = len(prof1),
plen2 = len(prof2),
i=0, j=0, side=0;

!finished;

[INSERT CALCULATIONS HERE]
face = [p1, p3, p2],
i = i + (side? 1 : 0),
j = j + (side? 0 : 1),
first = false,
finished = finishing,
finishing = i>=plen1 && j>=plen2
) if (!first) face
]

- Revar


On Feb 1, 2020, at 7:07 AM, nop head <[hidden email]> wrote:

Perhaps to fix Ronaldo's issue allow comma separated expressions in the test part that only get executed if the test passes, before the output.



On Sat, 1 Feb 2020 at 14:50, adrianv <[hidden email]> wrote:
Well....kind of.   The attraction of this loop structure is that you can
assign and reassign variables in the iteration part.  And while it may look
C-like, OpenSCAD is not C.  In OpenSCAD the loop body isn't "executed" it is
output.   In C you would actually do your computations in the output part at
the end.  But the whole reason to use this structure in OpenSCAD is so you
can reassign variables.  This means the computation all happens in the
iteration block.  Here's a simple example:

// Produces length n vector counting from 0
function example(n) =
  [each  // Needed to avoid singleton list output
     for(output=[], i=0;
         i<=n;
         output=concat(output,[i]),  // Here's the computation!
                 i=i+1,
                dummy=echo(i=i,output=output))
       if (i==n) output   // Output only in the last iteration
   ];

Note how I can repeatedly add to the output variable without the need for
recursion?  So there are two potential issues.  One issue is in getting the
answer out.  Here you see I need an "if" to output the answer when the
computation is done with a duplicated test for loop termination, which is
easy to detect since the termination test is very simple in this case.  The
second issue as Rinaldo notes is that the iteration block runs an extra
time---it must run when i=n even though I don't want output for this case.
So this can produce an error, or it can waste time.   

So running echo(example(4)) produces:

ECHO: i = 1, output = [0]
ECHO: i = 2, output = [0, 1]
ECHO: i = 3, output = [0, 1, 2]
ECHO: i = 4, output = [0, 1, 2, 3]
ECHO: i = 5, output = [0, 1, 2, 3, 4]
ECHO: [0, 1, 2, 3]


nophead wrote
> Isn't it exactly the same as C? The iteration part is done after the loop
> body is executed and the test is done before it.
>
> E.g.
> initialisation;
> while(test) {
>     ....
>    iteration;
> }
>
> So iteration is executed the same number of times as the body and the last
> case fails the test.
>
> On Sat, 1 Feb 2020 at 13:42, Ronaldo Persiano &lt;

> rcmpersiano@

> &gt; wrote:
>
>> The C-like for has the following general form:
>>
>> [for( initialization;
>>     test;
>>       iteration ) output ]
>>
>>
>> The output is done always after each test and the for escape just after
>> the test before the output. So, a better understanding of the control
>> structure is grasped by:
>>
>> [ for( i = 0,
>>        initialization;
>>
>>      i < 8;
>>
>>        (output(i))
>>
>>        i = i+1,
>>
>>        iteration ) ]
>>
>>
>>  where I suppose a simple iteration and test. In above example, we will
>> have 8 outputs and 8 iterations besides the initialization. However, the
>> last iteration does not produces any output. When the iterations include
>> very demanding processes or illegal operations firing warnings we need to
>> do tests in the iteration steps in order to avoid them.
>>
>> All that could be avoided if the output was done always before the test.
>> _______________________________________________
>> OpenSCAD mailing list
>>

> Discuss@.openscad

>> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>>
>
> _______________________________________________
> OpenSCAD mailing list

> Discuss@.openscad

> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org





--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org