# Logic

**Logic**, or more specifically, **Conditional Logic**, allows us to specify an action or set of actions based on a test. After evaluating the test, we will have a Boolean value representing `True` or `False` that we can use to control the Program Flow.

### Booleans

Numeric variables can store a whole range of different numbers. Boolean variables can only store two values referred to as True or False, Yes or No, 1 or 0. We rarely use booleans to perform calculations because of their limited range.

### Conditional Statements

The "If" statement is a key concept in programming: "If *this* is true, then *that* happens, otherwise *something else* happens. The resulting action of the statement is driven by a boolean value. There are multiple ways to define an "If" statement in Dynamo:

| Icon                                                                                                                                                                                                                                          | Name (Syntax)             | Inputs            | Outputs |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- | ----------------- | ------- |
| ![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-6ba18ae552565681ef15a3bdab420cb9a429e4ba%2FIf.jpg?alt=media\&token=14a7bd8e-8957-466c-b4dc-522cad015aa4) | If (**If**)               | test, true, false | result  |
| ![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-0c7f0bc560a2e4df6ed52e87ebe5d88331e8aa30%2FCode%20Block.jpg?alt=media)                                   | Code Block (**(x?y:z);**) | x? y, z           | result  |

Let's go over a brief example on each of these three nodes in action using the conditional "If" statement.

In this image, the *boolean* is set to *true*, which means that the result is a string reading: *"this is the result if true".* The three Nodes creating the *If* statement are working identically here.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-5470494d693e8ada89dbd5333c2e366477bac87c%2Flogic%20-%20conditional%20statements%2001%20false.jpg?alt=media)

Again, the Nodes are working identically. If the *boolean* is changed to *false*, our result is the number *Pi*, as defined in the original *If* statement.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-49433a20ae37f910c171e26c99a56a58bc3b2692%2Flogic%20-%20conditional%20statements%2002%20true.jpg?alt=media)

## Exercise: Logic and Geometry

> Download the example file by clicking on the link below.
>
> A full list of example files can be found in the Appendix.

{% file src="<https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-4b637189c8f132523a78034e8fb8a8f5e1538599%2FBuilding%20Blocks%20of%20Programs%20-%20Logic.dyn?alt=media&token=6ab24901-aec4-43a8-81aa-5765a1ba5fd9>" %}

### Part I: Filtering a List

1. Let's use logic to separate a list of numbers into a list of even numbers and a list of odd numbers.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-f4e25c7db4bea540e6db0341463e0dbd8ab3905c%2Flogic%20-%20exercise%20part%20I-01.jpg?alt=media\&token=89a44764-250d-443e-9b10-33f0f096cbda)

> a. **Number Range -** add a number range to the canvas.
>
> b. **Numbers -** add three number nodes to the canvas. The value for each number node should be: *0.0* for *start*, *10.0* for *end*, and *1.0* for *step*.
>
> c. **Output** - our output is a list of 11 numbers ranging from 0-10.
>
> d. **Modulo (%)-** **Number Range** into *x* and *2.0* into *y*. This calculates the remainder for each number in the list divided by 2. The output from this list gives us a list of values alternating between 0 and 1.
>
> e. **Equality Test (==) -** add an equality test to the canvas. Plug *modulo* output into the *x* input and *0.0* into the *y* input.
>
> f. **Watch -** The output of the equality test is a list of values alternating between true and false. These are the values used to separate the items in the list. *0* (or *true*) represents even numbers and (*1*, or *false*) represents odd numbers.
>
> g. **List.FilterByBoolMask -** this Node will filter the values into two different lists based on the input boolean. Plug the original *number range* into the *list* input and the *equality test* output into the *mask* input. The *in* output represents true values while the *out* output represents false values.
>
> h. **Watch** - as a result, we now have a list of even numbers and a list of odd numbers. We've used logical operators to separate lists into patterns!

### Part II: From Logic to Geometry

Building off of the logic established in the first exercise, let's apply this setup into a modeling operation.

2\. We'll jump off from the previous exercise with the same Nodes. The only exceptions (in addition to changing the format are):

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-861972db1a01929820b7fea74a5ec95e24ddd622%2Flogic%20-%20exercise%20part%20II-01.jpg?alt=media\&token=d18b5918-2ed6-4a0d-80cd-926a6f0a695e)

> a. Use a **Sequence** Node with these input values.
>
> b. We've unplugged the in list input into **List.FilterByBoolMask**. We'll put these Nodes aside for now, but they'll come in handy later in the exercise.

3\. Let's begin by creating a separate group of Graph as shown in the image above. This group of Nodes represents a parametric equation to define a line curve. A few notes:

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-0c9e58b5bb9fb4bd2d71480b7ae735cb93245a44%2Flogic%20-%20exercise%20part%20II-02.jpg?alt=media\&token=6365dbe5-5cc8-45f7-91a7-e7c31ffb082c)

> a. The first **Number Slider** represents the frequency of the wave, it should have a min of 1, a max of 4, and a step of 0.01.
>
> b. The second **Number Slider** represents the amplitude of the wave, should have a min of 0, a max of 1, and a step of 0.01.
>
> c. **PolyCurve.ByPoints -** if the above Node diagram is copied, the result is a sine curve in the Dynamo Preview viewport.

The method here for the inputs: use number nodes for more static properties and number sliders on the more flexible ones. We want to keep the original number range that we're defining in the beginning of this step. However, the sine curve that we create here should have some flexibility. We can move these sliders to watch the curve update its frequency and amplitude.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-8fa5144f1c54c37240b5283a81a954a5481f82b9%2Flogic%20-%20exercise%20part%20II-03.gif?alt=media\&token=12277d24-e8da-4038-9512-cc76c9b7e359)

4\. We're going to jump around a bit in the definition, so let's look at the end result so that we can reference what we're getting at. The first two steps are made separately, we now want to connect the two. We'll use the base sine curve to drive the location of the zipper components, and we'll use the true/false logic to alternate between little boxes and larger boxes.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-24d5a807ff7c73f3f2de6e5ef3c2245b26df77aa%2Flogic%20-%20exercise%20part%20II-04.jpg?alt=media\&token=92e40637-ef8e-472d-9ec3-fb10b8cfc640)

> a. **Math.RemapRange** - Using the number sequence created in step 02, let's create a new series of numbers by remapping the range. The original numbers from step 01 range from 0-100. These numbers range from 0 to 1 by the *newMin* and *newMax* inputs respectively.

5\. Create a **Curve.PointAtParameter** Node, then connect the **Math.RemapRange** output from step 04 as its *param* input.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-e53cdd48714b6277bb2b3a6c17cee92b9ed1567e%2Flogic%20-%20exercise%20part%20II-05.jpg?alt=media\&token=a446134d-80e3-42a1-b120-4bfe0b51de03)

This step creates points along the curve. We remapped the numbers to 0 to 1 because the input of *param* is looking for values in this range. A value of *0* represents the start point, a value of *1* represents the end points. All numbers in between evaluate within the *\[0,1]* range.

6\. Connect the output from **Curve.PointAtParameter** to the **List.FilterByBoolMask** to separate the list of odd and even indices.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-edbaea743ece72ab11a83afec697f8a20ffe4c59%2Flogic%20-%20exercise%20part%20II-06.jpg?alt=media\&token=cdbb33c7-d737-4baa-a2bc-15218fee7860)

> a. **List.FilterByBoolMask** - Plug **Curve.PointAtParameter** from the previous step into the *list* input.
>
> b. **Watch -** a watch node for *in* and a watch node for *out* shows that we have two lists representing even indices and odd indices. These points are ordered in the same way on the curve, which we demonstrate in the next step.

7\. Next, we are going to use the output result from **List.FilterByBoolMask** in step 05 to generate geometries with sizes according to its indices.

**Cuboid.ByLengths -** recreate the connections seen in the image above to get a zipper along the sine curve. A cuboid is just a box here, and we're defining its size based on the curve point in the center of the box. The logic of the even/odd divide should now be clear in the model.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-a11214c87eced10e2d6166fcf11935a7214c8bab%2Flogic%20-%20exercise%20part%20II-07.jpg?alt=media\&token=93a1ff9e-0c12-427c-94b0-f18d4f68f0ff)

> a. List of cuboids at even indices.
>
> b. List of cuboids at odd indices.

Voila! You have just programmed a process of defining the geometry dimensions according to the logic operation demonstrated in this exercise.
