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

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

*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.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.Download the example file by clicking on the link below.A full list of example files can be found in the Appendix.

Building Blocks of Programs - Logic.dyn

20KB

Binary

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

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.0forstart,10.0forend, and1.0forstep.c.Output- our output is a list of 11 numbers ranging from 0-10.d.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.e.Equality Test (==) -add an equality test to the canvas. Plugmodulooutput into thexinput and0.0into theyinput.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(ortrue) represents even numbers and (1, orfalse) represents odd numbers.g.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.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!

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):

a. Use aSequenceNode with these input values.b. We've unplugged the in list input intoList.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:

a. The firstNumber Sliderrepresents the frequency of the wave, it should have a min of 1, a max of 4, and a step of 0.01.b. The secondNumber Sliderrepresents 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.

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.

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 thenewMinandnewMaxinputs respectively.

5. Create a

**Curve.PointAtParameter**Node, then connect the**Math.RemapRange**output from step 04 as its*param*input.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.a.List.FilterByBoolMask- PlugCurve.PointAtParameterfrom the previous step into thelistinput.b.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.

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.

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.

Last modified 1yr ago