#StoneProfitsSystem


Loops and Arrays

Loops:
  • Imagine that you wanted a script to perform the same routine over and over again 50 times in a row.
  • An example could be if you wanted a script to produce a table comparing temperatures in Fahrenheit and Celsius. The script should produce 50 lines in a table showing different temperatures according to the two scales.
  • Instead of adding 50 almost equal lines in your script you could use loops to make the script perform a task like this. 
  • There are two different kinds of loops: for and while.
For-Loop:
  • The for-loop is used when you know in advance how many times the script should perform the similar task.

Syntax:
for (variable=startvalue; variable<=endvalue; variable=variable+incrementfactor) 
{
// Here goes the scriptlines you want to be looped.
};
  • Enter a variablename where it says variable.
  • Enter the startvalue of the loop where it says startvalue.
  • Enter the endvalue of the loop where it says endvalue.
  • Enter the factor each loop should increment where it says incrementfactor.
Note: The increment factor could be negative if you wanted.
Furthermore the <= could be any comparing statement.
Eg. >, == or whatever.

Example:
  • In the example above it would be if you wanted it to create exactly 50 lines. 
  • You would simply tell the script to loop the same lines 50 times in a row.

While-Loop:

  • The while-loop is used when you don't know in advance how many times the loop should be performed. 
  • You simply want the loop to be performed until a certain condition becomes true.

Syntax:
while (variable<=endvalue) 
{
// Here goes the scriptlines you want to be looped.
}
  • Enter a variablename where it says variable.
  • Enter the endvalue of the loop where it says endvalue.
Note: The <= could be anything that would fit the purpose eg. >, == or whatever.

Example:
  • In the example above it would be if you wanted to make a table comparing Celsius and Fahrenheit, stepping 15 degrees for each row, and you wanted the table to contain values up to say 1200 degrees of Celsius. 



Arrays:
  • When working with more complex scripts you might face a situation in which you would have many more or less similar variables.
  •  Instead of being forced to write a line for each operation done to such a variable, you can use arrays to help you automatize the process.
Consider this example:
Example IExample II
value1=10;
 value2=20;
value3=30;
.... here would go 96 similar lines
.... value100=1000
-------------------------------------------------
you would need to enter 100 lines to
perform an operation on your variables.
value=new Array;
for (number=1;
 number<=100;
number=number+1)
{ value[number]=number*10};
----------------------------------------------
you only need to enter 3 lines no
matter how many variables you have.

  • To use variable-arrays you need to define the array before referring to any of the variables in it.
This is done using the syntax: variablename=new Array;
Replace variablename with the varible you want to work as an array.
  • As the example shows arrays becomes extremely powerfull when used in combination with loops.
  • However you do not have to handle array-variables in loops. 
  • Single variables can be addressed with a syntax like this:value[9]=170;

No comments:

Post a Comment