Settings

Choose language

for (i=1,i<5,i++)

for
Programming a loop. Expressions can be separated by commas or by semicolon.

Function parameters


  • The first expression is evaluated once unconditionally at the beginning of the loop.
  • In the beginning of each iteration, the second expression is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.
  • At the end of each iteration, the third expression is executed.

    This is usually referred to as working with the loop counter. First, set the initial value of the counter. Then decide, how many times you want to run the loop at maximum. Then decide, how the counter will be changed at the end of each loop run. The most usual way is to add plus one, which is written as i++. This is the easiest way to work with loops, but not the only possible one.

    Usage


    Loops are handy when you want to program repeating actions. For example, you want to find the position of points that would let you draft a curve of a given length.

    You know that the length of the curve should be 15 cm. You have defined points P1 and P2 at 15 cm apart from each other, but that would let you draft a line between them, not a curve. That's not what you want, so you are going to move the points closer until you are able to draft a curve between them, that is no longer than 15 cm. You are going to make the point closer by 0.2 cm every time. However, you don't want to make this an endless process, you are going to try say 10 times, and then stop executing these actions.

    Let us look at this piece of code:

    for (i=1,i<10,i++)
    {
    p2=apply(p2,0.2,[p2:p1].a1)
    c1=curve(p1,p2,110,0)
    if(c1.l<15)
    break
    else
    continue
    }




  • Let us see what the program will do step by step. Once it encounters the for loop, it looks at the first expression i=1 and executes it. Thus it creates a variable i and assigns it to 1. It will then enter the block of code within {} brackets.

    The software will move p2 by 0.2 cm towards p1. It will then draft a curve between p1 and p2. Then it will check if the length of the curve is less than 15 cm. If it is less than 15 cm, the software will jump out of the loop and move on to the next code we have written in the algorithm. This is what the break operator lets us do.
    Otherwise it will continue working within the for loop. In the example above we have described this with operator continue.

    Once all the statements are executed, Sewist will look at the third expression in the for description. It says i++ and this means we should plus 1 to the current value of i. So i now equals 1+1=2.

    Now Sewist looks at the second expression in the for description, which is i<10. This condition says that i should stay less than 10. At the moment it equals 2, so we can run the loop once again.

    Sewist will continue running the loop, executing the described actions, then adding plus 1 to i, and then checking condition. Once it has run the loop for 10 times, i will equal 11. The system will look at the second condition in for description and realise that it is not met. This is when the software will stop running inside the loop and will proceed with the rest of the code in the algorithm.

    We can see the result of this code on the right. The software had to run through the loop 7 times until the position of the points allowed to draft a curve that was less than 15 cm. The resulting curve is highlighted in the screen.