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.

  • A < B – Will check if the A is strictly smaller than B.
  • A <= B – Will check if A is smaller or equal than B.
  • A == B – Will check if A is equal to B.
  • A > B – Will check if the A is strictly larger than B.
  • A >= B – Will check if A is larger or equal than B.
  • A != B – Will check if A is not equal to  B.
  • Assignment Operators:

    Used to change the value of a variable:

  • A = B – Will make A the same as B.
  • A ^= B – Will raise A to the power of B and assign the result to A.
  • A *= B – Will multiply A times B and assign the result to A.
  • A /= B – Will divide A over B and assign the result to A.
  • A %= B – Will integer divide A over B and assign the result to A.
  • A += B – Will add B to A and assign the result to A.
  • A -= B –  Will subtract B from A and assign the result to A. 
  • Logical Operators:

    Used to perform logical proofs between several comparison tests. Consider A and B as booleans.

  • A & B – AND Operator.  True if both tests are true, checks both of them.
  • A && B – Conditional AND Operator. True if both tests are true, if A is false, B is not evaluated.
  • A | B – OR Operator. True if either of A or B are true.
  • A^B – XOR Operator.  True if A and B are distinct. If both A and B are true or false, A^B will return false.
  • !A – NOT Operator. True if A is false, False if A is true.
  • 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.