## 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 |
---|---|---|---|---|

If | If | test, true, false | result | |

Formula | IF(x,y,z) | x, y, z | result | |

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

booleanis set totrue, which means that the result is a string reading:"this is the result if true".The three Nodes creating theIfstatement are working identically here.

Again, the Nodes are working identically. If the

booleanis changed tofalse, our result is the numberPi, as defined in the originalIfstatement.

### Filtering a List

Download the example file that accompanies this exercise (Right click and "Save Link As..."): Building Blocks of Programs - Logic.dyn. A full list of example files can be found in the Appendix.

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

Number Range -add a number range to the canvas.Numbers -add three number nodes to the canvas. The value for each number node should be:0.0forstart,10.0forend, and1.0forstep.Output -our output is a list of 11 numbers ranging from 0-10.Modulo (%)-Number Rangeintoxand2.0intoy. 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.Equality Test (==) -add an equality test to the canvas. Plugmodulooutput into thexinput and0.0into theyinput.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(ortrue) represents even numbers and (1, orfalse) represents odd numbers.List.FilterByBoolMask -this Node will filter the values into two different lists based on the input boolean. Plug the originalnumber rangeinto thelistinput and theequality testoutput into themaskinput. Theinoutput represents true values while theoutoutput represents false values.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!

### From Logic to Geometry

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

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

- The input values have changed.
- 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.

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

- The
first slidershould have a min of 1, a max of 4, and a step of 0.01.- The
second slidershould have a min of 0, a max of 1, and a step of 0.01.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.

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.

Math.RemapRange -Using the number sequence created in step 01, 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 thenewMinandnewMaxinputs respectively.

Curve.PointAtParameter -PlugPolycurve.ByPoints(from step 2) intocurveandMath.RemapRangeintoparam. This step creates points along the curve. We remapped the numbers to 0 to 1 because the input ofparamis looking for values in this range. A value of0represents the start point, a value of1represents the end points. All numbers in between evaluate within the[0,1]range.

List.FilterByBoolMask -PlugCurve.PointAtParameterfrom the previous step into thelistinput.Watch -a watch node forinand a watch node foroutshows 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.

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.

Number Slider -stepping back to the beginning of the definition, we can flex the number slider and watch the zipper update. The top row of images represents a range values for the top number slider. This is the frequency of the wave.Number Slider -the bottom row of images represents a range of values for the bottom slider. This is the amplitude of the wave.