Grasshopper Scripting 103
Conditionals and Operators
Working with single values improves a lot when you can perform what is called conditional logic. This introduces a basic concept called program flow: parts of our code that get executed depending on some conditions. Conditional statements require operators, expressions that compare two (mostly numerical) properties and check for a logical state.
Download here a GH definition (0.9.014) with these and other examples.
Basic Conditional Statement
private void RunScript(double x, double y, ref object A)
{
if (x > y) { //Check if x is strictly larger than y
A = x; //If so, Assign x to A
}
else { //In any other case..
A = 0.0; //Assign 0.0 to A
}
}
Every conditional statement has a logical test and an action to perform if the test is true. The “else” is optional and will be executed in case the test is false.
Operators
More info can be found in the corresponding MS Page – C# Operators
Comparison Operators:
Comparison operators are used to check two elements against each other. Most of the times the elements will be numbers (integers, doubles, floats,…) but also other types, specially when determining if two elements are the same.
Assignment Operators:
Used to change the value of a variable:
Logical Operators:
Used to perform logical proofs between several comparison tests. Consider A and B as booleans.
Examples:
//Example 0. Copy the contents of the script to your C# Component
private void RunScript(double x, ref object A)
{
if (x > 1) {
A = x;
}
}
//Example 1. Copy the contents of the script to your C# Component
private void RunScript(double x, double y, double z, ref object A)
{
if (x > z ) {
A = "large";
}
else if (x <= z && x > y){
A = "medium";
}
else {
A = "small";
}
}
//Example 2. Copy the contents of the script to your C# Component
private void RunScript(double x, double y, ref object A)
{
if (x == y ) {
A = "match!";
}
else if (x < y){
A = "smaller";
}
else {
A = "bigger";
}
}
//Example 3. Copy the contents of the script to your C# Component
private void RunScript(double x, double y, double z, ref object A)
{
if(x != 0.0){
if (x < y ) {
x += z; // the same as x = x + z;
}
else {
x -= z; // the same as x = x - z;
}
}
A = x;
}
The Switch / Case Structure
The Switch / Case structure is useful when you want to work with several options and instead of writing a lot of “IFs” you can calculate a switch value (Y in the example below) and define cases for each occurrence plus a default value:
private void RunScript(int y, ref object A)
{
switch (y)
{
case 0:
A = "So you were the little king/queen?";
break;
case 1:
A = "I hope you were the elder";
break;
case 2:
A = "Nice!";
break;
case 3:
A = "That's almost a Basketball team";
break;
default:
A = String.Format("Wow {0}!!, Xmas dinner must be a true party!", y);
break;
}
}
Challenge
1. Write a component that filters a lists of input integer and returns only those which are even numbers.
2. Write a component that filters a list of input integers and returns only those which are a multiple of a Y integer.