Looping Statements

 

The simplest of LScripts contains a series of Layout or Modeler commands. These commands are executed one statement at a time, one after another until the end of the script.

What if you wanted to repeat a series of statements x amount of times? You could type the exact statements over and over again. But that would be insane, and very inefficient. Not only that, but what if you do not know how many times you want the statements to repeat? The value may be based on a user input, or a variable that was set by another command. It would be impossible to create an LScript to handle these situations without using a looping statement.

A looping statement executes a block of code while a condition is true. When it executes the last statement within that block of code and comes across the closed marker (}), it returns to the statement’s open marker ({) and repeats the code. It repeats until the expression becomes false, then it moves on to the next statement. The most common of these statements is the for-loop.

The for-loop

The for-loop statement, although complex, is the most common of the looping statements. This statement is an iterating loop. This means that when the block of code in the loop is done, it changes a variable, compares it to a value in an expression, and determines if it should start over again. The process repeats until the result of the expression comes back false.

Syntax:

for(variable declaration; …expression…; iteration)
{
  …Statements to loop…
}

     Example:

for(i = 0; i < 10; i++)
{
  info(i);
}
When LScript executes the for statement, four things happen. First, the variable i is declared with a value of 0:

for(i = 0; i < 10; i++)

Next, LScript checks to see if the expression defined in the second part of the statement is true or not. In this case, LScript checks to see if the variable i is less than 10. (Currently, i is 0, so because 0 is less than 10, the expression is true.)

for(i = 0; i < 10; i++)

If the expression is true, LScript will execute the statement(s) in the block of code directly following the statement. The only statement which we have setup to run is a simple info() box:

for(i = 0; i < 10; i++)
{
  info(i);
}

However, this info() is not printing straight text. This info() statement prints the current value of the variable i, which is our iterator variable. This will allow us to better understand what changes are actually happening to the variable i.

The for statement is not done yet. When all the statement(s) in the block are run, the iterator variable is changed. How the variable gets changed is defined in the third portion of the for statement.

for(i = 0; i < 10; i++)

In this case, we used i++ , which is a quick way of saying i = i + 1. This statement defines how the variable i is changed every time the code gets looped. So where i started off having a value of 0, after all the statements in the code block are run, i will have the value of 1, then 2, then 3. This means that the info() box will be appearing and printing the value of i, until i is no longer less than 10.

As you can see, using the for statement not only gives your code the ability loop, but it can also count. This can come in very handy when dealing with values in arrays. Say you have the following array:

objects[1] = "Null";
objects[2] = "cow.lwo";
objects[3] = "ball.lwo";

Imagine what this code will do:

for(i = 1; i <= 3; i++)
{
  info(objects[i]);
}

When the iterator in a for statement tests false, LScript moves on to the next bunch of code.

The foreach-loop

This construct is intended to simplify the process of iterating over a linear series of values. It takes as parameters a variable to be used to contain each value, and an expression that will evaluate into some data type capable of iteration.

The foreach() iterator accepts the following data types for iteration:

* integer
* number
* File Object Agent
* Array (including Modeler LScript's points[] and polygons[]
automatic arrays, and Associative arrays)



...
editbegin();

foreach(x,polygons)
info(x);

editend();
...

--------------------

...
foreach(x,10 - 5)
info(x);
...

--------------------

...
f = File("c:\\config.sys","r");
foreach(x,f)
info(x);
...

--------------------

...
obj = getfirstitem(MESH);
foreach(x,obj)
info(x.name);
...

--------------------

...
a = @1,2,3@;
foreach(x,a)
info(x);
...


The while loop

The for statement is nice when you want to repeat a bunch of statements a known amount of times. However, knowing when you need to stop looping is not always possible, especially in a script designed to handle a wide range of situations. What if you want the loop to run until an action from the script tells the loop to stop? Enter the while loop to save the day!

Note: This coding savior has a very nasty side. Programming with the while loop requires a fair amount of care and concentration when you write or things can go horribly wrong.

Syntax:

while(…expression…)
{
  …statements…
}
…next statements…

Notice that the while loop looks a lot like an if-then statement. That is basically what a while statement is, except that instead of moving on to the next line of code, a running while loop will repeat until the expression is no longer true.

Note: In this way, the while loop is also like the for statement discussed previously.

Example:

done = false;
while(!done)
{
  if(x ==5)
       done = true;
  else
       x++;
}

Before we enter the while loop, we must declare the Boolean variable to be false. This variable is what the while loop will check to see if it should continue or not.

done = false;

This variable will simply act like a light switch to the block of code bound to the while statement. When the variable done is false, the loop runs, when it is true , the loop stops. Therefore, in order for any of the statements in the while block to run even once, the done variable must initially be set to false.

First, the while() statement checks to see if the Boolean variable is not true (false). If this is indeed the case, the bound code begins to run.

while(!done)
{
  if(x ==5)
       …

The if-then statement checks to see if the integer variable x is equal to 5. In this case, the variable x has a value of 0. So the else block of the if-then statement is executed. The else block is the iterator for this while() loop. The variable x now has a value of 1 and the loop repeats.

Eventually, x gets a value of 5 and the if-then statement becomes true. The Boolean variable done is assigned a value of true.

done = true;

When the while() expression checks to see if done is false, the loop ends. The focus of the program then turns to the next set of statements.

So what is so dangerous about this? Take a good look and see if you can find what is wrong with this example:

done = false;
while(!done)
{
  if(x ==5)
       done = true;
}

This is what we call an endless loop. The while() command checks if the value for the done boolean is false, but the block of code for the while() statement has no iterator. The x never changes, so it will NEVER have a value of 5 and done will never become true. Thus, the script never ends. You must manually kill Layout or Modeler to run LightWave again when you fall into an endless loop.

Note: You can also create endless loops with the for statement.

You must be very careful when you work with the for and while() loops. Make sure that your variables have some kind of iterator, or will change so that the script can end. Users really hate it when a script hangs and they must kill Lightwave and lose all their work.