{"id":2056,"date":"2014-03-22T17:14:21","date_gmt":"2014-03-22T16:14:21","guid":{"rendered":"https:\/\/www.modelical.com\/?p=484"},"modified":"2022-03-21T12:52:10","modified_gmt":"2022-03-21T11:52:10","slug":"grasshopper-scripting-103-conditional-statements-and-operators","status":"publish","type":"post","link":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/","title":{"rendered":"Grasshopper Scripting 103"},"content":{"rendered":"<h2>Conditionals and Operators<\/h2>\n<p>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.<\/p>\n<p>Download here a <a title=\"GHS Conditional and Operators\" href=\"https:\/\/www.modelical.com\/wp-content\/uploads\/2014\/03\/B10_GHS_103_Conditionals1.zip\">GH definition<\/a> (0.9.014) with these and other examples.<\/p>\n<h3>Basic Conditional Statement<\/h3>\n<pre class=\"lang:c# decode:true\">private void RunScript(double x, double y, ref object A)\r\n  {\r\n    if (x &gt; y) {  \/\/Check if x is strictly larger than y\r\n        A = x;    \/\/If so, Assign x to A\r\n      }\r\n    else {        \/\/In any other case..\r\n      A = 0.0;    \/\/Assign 0.0 to A\r\n    }\r\n  }<\/pre>\n<p>Every conditional statement has a logical test and an action to perform if the test is true. The &#8220;else&#8221; is optional and will be executed in case the test is false.<\/p>\n<h3>\u00a0Operators<\/h3>\n<p>More info can be found in the corresponding <a title=\"C# Operators\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6a71f45d.aspx\">MS Page &#8211; C# Operators<\/a><\/p>\n<p><strong>Comparison Operators:<\/strong><\/p>\n<p>Comparison operators are used to check two elements against each other. Most of the times the elements will be numbers (integers, doubles, floats,&#8230;) but also other types, specially when determining if two elements are the same.<\/p>\n<li><strong>A &lt; B<\/strong> &#8211; Will check if the A is strictly smaller than B.<\/li>\n<li><strong>A &lt;= B<\/strong> &#8211; Will check if A is smaller or equal than B.<\/li>\n<li><strong>A == B<\/strong> &#8211; Will check if A is equal to B.<\/li>\n<li><span style=\"line-height: 1.5em;\"><strong>A &gt; B<\/strong> &#8211; Will check if the A is strictly larger than B.<\/span><\/li>\n<li><span style=\"line-height: 1.5em;\"><strong>A &gt;= B<\/strong> &#8211; Will check if A is larger or equal than B.<\/span><\/li>\n<li><strong>A != B<\/strong> &#8211; Will check if A is not equal to \u00a0B.<\/li>\n<p><strong>Assignment Operators:<\/strong><\/p>\n<p>Used to change the value of a variable:<\/p>\n<li><strong>A = B<\/strong> &#8211; Will make A the same as B.<\/li>\n<li><strong>A ^= B<\/strong> &#8211; Will raise A to the power of B and assign the result to A.<\/li>\n<li><strong>A *= B<\/strong> &#8211; Will multiply A times B and assign the result to A.<\/li>\n<li><strong>A \/= B<\/strong> &#8211; Will divide A over B and assign the result to A.<\/li>\n<li><strong>A %= B<\/strong> &#8211;\u00a0Will integer divide A over B and assign the result to A.<\/li>\n<li><strong>A += B<\/strong> &#8211;\u00a0Will add B to A and assign the result to A.<\/li>\n<li><span style=\"line-height: 1.5em;\"><strong>A -= B<\/strong> &#8211;\u00a0\u00a0Will subtract B from A and assign the result to A.\u00a0<\/span><\/li>\n<p><strong>Logical Operators:<\/strong><\/p>\n<p>Used to perform logical proofs between several comparison tests. Consider A and B as booleans.<\/p>\n<li><strong>A &amp; B<\/strong> &#8211; AND Operator. \u00a0True if both tests are true, checks both of them.<\/li>\n<li><strong>A &amp;&amp; B<\/strong> &#8211; Conditional AND Operator. True if both tests are true, if A is false, B is not evaluated.<\/li>\n<li><strong>A | B<\/strong> &#8211; OR Operator. True if either of A or B are true.<\/li>\n<li><strong>A^B<\/strong> &#8211; XOR Operator. \u00a0True if A and B are distinct. If both A and B are true or false, A^B will return false.<\/li>\n<li>!<strong>A<\/strong> &#8211; NOT Operator. True if A is false, False if A is true.<\/li>\n<p><strong>Examples:<\/strong><\/p>\n<pre class=\"lang:c# decode:true\">\/\/Example 0. Copy the contents of the script to your C# Component\r\nprivate void RunScript(double x, ref object A)\r\n  {\r\n    if (x &gt; 1) {\r\n      A = x;\r\n    }\r\n  }\r\n\/\/Example 1. Copy the contents of the script to your C# Component\r\nprivate void RunScript(double x, double y, double z, ref object A)\r\n  {\r\n    if (x &gt; z ) {\r\n      A = \"large\";\r\n    }\r\n    else if (x &lt;= z &amp;&amp; x &gt; y){\r\n      A = \"medium\";\r\n    }\r\n    else {\r\n      A = \"small\";\r\n    }\r\n  }\r\n\/\/Example 2. Copy the contents of the script to your C# Component\r\nprivate void RunScript(double x, double y, ref object A)\r\n  {\r\n    if (x == y ) {\r\n      A = \"match!\";\r\n    }\r\n    else if (x &lt; y){\r\n      A = \"smaller\";\r\n    }\r\n    else {\r\n      A = \"bigger\";\r\n    }\r\n  }\r\n\r\n\/\/Example 3. Copy the contents of the script to your C# Component\r\nprivate void RunScript(double x, double y, double z, ref object A)\r\n  {\r\n    if(x != 0.0){\r\n        if (x &lt; y ) {\r\n           x += z; \/\/ the same as x = x + z;\r\n        }\r\n        else {\r\n          x -= z; \/\/ the same as x = x - z;\r\n        }\r\n    }\r\n    A = x;\r\n  }<\/pre>\n<h3>The Switch \/ Case Structure<\/h3>\n<p>The Switch \/ Case structure is useful when you want to work with several options and instead of writing a lot of &#8220;IFs&#8221; you can calculate a switch value (Y in the example below) and define cases for each occurrence plus a default value:<\/p>\n<pre class=\"lang:c# decode:true\">private void RunScript(int y, ref object A)\r\n  {\r\n    switch (y)\r\n    {\r\n      case 0:\r\n        A = \"So you were the little king\/queen?\";\r\n        break;\r\n      case 1:\r\n        A = \"I hope you were the elder\";\r\n        break;\r\n      case 2:\r\n        A = \"Nice!\";\r\n        break;\r\n      case 3:\r\n        A = \"That's almost a Basketball team\";\r\n        break;\r\n      default:\r\n        A = String.Format(\"Wow {0}!!, Xmas dinner must be a true party!\", y);\r\n        break;\r\n    }\r\n  }<\/pre>\n<h3>Challenge<\/h3>\n<p>1. Write a component that filters a lists of input integer and returns only those which are even numbers.<\/p>\n<p>2. Write a component that filters a list of input integers and returns only those which are a multiple of a Y integer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":27118,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[423,418],"class_list":["post-2056","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guidelines","tag-technology-c","tag-technology-grasshopper"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Grasshopper Scripting 103 - Modelical<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Grasshopper Scripting 103 - Modelical\" \/>\n<meta property=\"og:description\" content=\"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. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/\" \/>\n<meta property=\"og:site_name\" content=\"Modelical\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Modelical\/\" \/>\n<meta property=\"article:published_time\" content=\"2014-03-22T16:14:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-21T11:52:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.modelical.com\/wp-content\/uploads\/31_GH.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"500\" \/>\n\t<meta property=\"og:image:height\" content=\"375\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Roberto Molinos\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@modelical\" \/>\n<meta name=\"twitter:site\" content=\"@modelical\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Roberto Molinos\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-103-conditional-statements-and-operators\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-103-conditional-statements-and-operators\\\/\"},\"author\":{\"name\":\"Roberto Molinos\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\"},\"headline\":\"Grasshopper Scripting 103\",\"datePublished\":\"2014-03-22T16:14:21+00:00\",\"dateModified\":\"2022-03-21T11:52:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-103-conditional-statements-and-operators\\\/\"},\"wordCount\":539,\"image\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-103-conditional-statements-and-operators\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/04\\\/Posts_31_GH_1-400x250-1.jpg\",\"keywords\":[\"Tec C#\",\"Tec Grasshopper\"],\"articleSection\":[\"Guidelines\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-103-conditional-statements-and-operators\\\/\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-103-conditional-statements-and-operators\\\/\",\"name\":\"Grasshopper Scripting 103 - Modelical\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-103-conditional-statements-and-operators\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-103-conditional-statements-and-operators\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/04\\\/Posts_31_GH_1-400x250-1.jpg\",\"datePublished\":\"2014-03-22T16:14:21+00:00\",\"dateModified\":\"2022-03-21T11:52:10+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-103-conditional-statements-and-operators\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/grasshopper-scripting-103-conditional-statements-and-operators\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/04\\\/Posts_31_GH_1-400x250-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.modelical.com\\\/wp-content\\\/uploads\\\/2015\\\/04\\\/Posts_31_GH_1-400x250-1.jpg\",\"width\":400,\"height\":250},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/\",\"name\":\"Modelical\",\"description\":\"We build information\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/#\\\/schema\\\/person\\\/3ad52fd99e6b5b98a59ef24c76a7c2d5\",\"name\":\"Roberto Molinos\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro\",\"caption\":\"Roberto Molinos\"},\"sameAs\":[\"https:\\\/\\\/www.modelical.com\"],\"url\":\"https:\\\/\\\/www.modelical.com\\\/en\\\/author\\\/roberto\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Grasshopper Scripting 103 - Modelical","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/","og_locale":"en_US","og_type":"article","og_title":"Grasshopper Scripting 103 - Modelical","og_description":"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. [&hellip;]","og_url":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/","og_site_name":"Modelical","article_publisher":"https:\/\/www.facebook.com\/Modelical\/","article_published_time":"2014-03-22T16:14:21+00:00","article_modified_time":"2022-03-21T11:52:10+00:00","og_image":[{"width":500,"height":375,"url":"https:\/\/www.modelical.com\/wp-content\/uploads\/31_GH.jpg","type":"image\/jpeg"}],"author":"Roberto Molinos","twitter_card":"summary_large_image","twitter_creator":"@modelical","twitter_site":"@modelical","twitter_misc":{"Written by":"Roberto Molinos","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/#article","isPartOf":{"@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/"},"author":{"name":"Roberto Molinos","@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5"},"headline":"Grasshopper Scripting 103","datePublished":"2014-03-22T16:14:21+00:00","dateModified":"2022-03-21T11:52:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/"},"wordCount":539,"image":{"@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/Posts_31_GH_1-400x250-1.jpg","keywords":["Tec C#","Tec Grasshopper"],"articleSection":["Guidelines"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/","url":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/","name":"Grasshopper Scripting 103 - Modelical","isPartOf":{"@id":"https:\/\/www.modelical.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/#primaryimage"},"image":{"@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/Posts_31_GH_1-400x250-1.jpg","datePublished":"2014-03-22T16:14:21+00:00","dateModified":"2022-03-21T11:52:10+00:00","author":{"@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.modelical.com\/en\/grasshopper-scripting-103-conditional-statements-and-operators\/#primaryimage","url":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/Posts_31_GH_1-400x250-1.jpg","contentUrl":"https:\/\/www.modelical.com\/wp-content\/uploads\/2015\/04\/Posts_31_GH_1-400x250-1.jpg","width":400,"height":250},{"@type":"WebSite","@id":"https:\/\/www.modelical.com\/en\/#website","url":"https:\/\/www.modelical.com\/en\/","name":"Modelical","description":"We build information","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.modelical.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.modelical.com\/en\/#\/schema\/person\/3ad52fd99e6b5b98a59ef24c76a7c2d5","name":"Roberto Molinos","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro","url":"https:\/\/secure.gravatar.com\/avatar\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9346c12c96cb942be9bb8b2454e296662a1baa0b4cd461ab315ae9a5185b0db6?s=96&d=initials&r=g&initials=ro","caption":"Roberto Molinos"},"sameAs":["https:\/\/www.modelical.com"],"url":"https:\/\/www.modelical.com\/en\/author\/roberto\/"}]}},"_links":{"self":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts\/2056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/comments?post=2056"}],"version-history":[{"count":0,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/posts\/2056\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/media\/27118"}],"wp:attachment":[{"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/media?parent=2056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/categories?post=2056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modelical.com\/en\/wp-json\/wp\/v2\/tags?post=2056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}