Grasshopper Scripting 105
Loops (I)
Loops are a fundamental weapon when dealing with program flow. You’ll want your component to perform a certain action many times over a data-set or to keep on doing something until a condition is met. That’s exactly what loops are for. Let’s see them in action.
Download here the GH file (0.9.014) containing the examples.
Two Types Of Loops
The Foreach Loop is useful when you want to exactly the same operation to each element within a collection. For instance printing something to the console or just adding all the numbers in a collection. We have to supply an interator value, of the same type as the list, that will be evaluated at each step of the loop.
This example takes a list of numbers and returns the number of elements and the sum of all of them:
private void RunScript(List<double> x, ref object A)
{
double buffer = 0.0; //create a buffer to hold the sum
foreach (double val in x) //create a loop with val as iterator
{
buffer += val; //add val to the buffer
}
A = String.Format("There are {0} numbers in the list and the sum of all of them is {1}", x.Count, buffer);
}
This example takes a list of strings and creates a sentence with all the supplied values:
private void RunScript(List<string> x, ref object A)
{
string buffer = "My favourite animals are:"; //create our buffer string and set it to something
foreach (string val in x) //create a foreach loop with a val iterator
{
buffer = buffer + " " + val + ","; //add the item to the list including spaces and commas...
}
A = buffer; //output the value
}
Here the results:
The For (int i, i<n, i++) loop is useful when we want to keep control of the iteration process, being able modify actions depending on iterator state. Look at this example that will output only elements with even index from a list:
private void RunScript(List<string> s, ref object A)
{
List<string> evenList = new List<string>();
for (int i = 0; i < s.Count; i++){
if (i % 2 == 0) {// if the remainder of dividing i over 2 is 0 the item is even
evenList.Add(s[i]);
}
}
A = evenList;
}
The key here is the declaration of the loop:
Besides, the inner IF statement is checking when the iterator is even, look at it carefully.
Another example, this component will create a Fibonacci series:
private void RunScript(int n, ref object A)
{
//declare a new list
List<int> fibonacci = new List<int>();
//declare our buffer
int buffer;
//declare the loop, initialize it in 0 and make sure it stops at n
for (int i = 0; i < n; i++)
{
if (i < 2){
fibonacci.Add(i); //we have to add the first two values almost manually
}
else {
//calculate the current value by adding the two previous ones
buffer = fibonacci[i - 2] + fibonacci[i - 1];
//add the value to the list
fibonacci.Add(buffer);
}
}
//output the fibonacci series
A = fibonacci;
}
See the results here.
Challenge
1. Write a C# component that reads a list of numbers and outputs the largest of them together with its index.
2. Write a C# component that reads a list of numbers and outputs the smallest of them together with its index.

