{"id":2270,"date":"2017-01-07T00:01:01","date_gmt":"2017-01-07T08:01:01","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2270"},"modified":"2016-12-31T09:12:33","modified_gmt":"2016-12-31T17:12:33","slug":"comparing-three-items","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2270","title":{"rendered":"Comparing Three Items"},"content":{"rendered":"<p>Logic can be frustrating, especially for non-Vulcans. The computer is kind of a Vulcan, so logic comes naturally to the machine. Programming languages are packed with logical expressions. A programmer&#8217;s duty is to convert human thoughts into the raw, powerful logic the computer understands &mdash; and end up with the desired result.<br \/>\n<!--more--><br \/>\nAs a case in point, I recently wrote code that required me to compare three variables: <code>a<\/code>, <code>b<\/code>, and <code>c<\/code>. Were they equal to each other? For some reason, this process caused me a massive brain cramp.<\/p>\n<p>As a review, when two variables are equal, the comparison is made like this:<\/p>\n<p><code>(a == b)<\/code><\/p>\n<p>That portion of a statement evaluates either TRUE or FALSE. This is basic logic, and as long as you remember to use two equal signs instead of one in C, you&#8217;re gold.<\/p>\n<p>Now stir variable <code>c<\/code> into the mix:<\/p>\n<p><code>(a == b)<br \/>\n(b == c)<br \/>\n(a == c)<\/code><\/p>\n<p>All three statements must be TRUE if the three variables are identical, but you don&#8217;t need to code all three. You can, but it&#8217;s unimpressive.<\/p>\n<p>Using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Transitive_relation\" target=\"_blank\">transitive property<\/a>, you know that if <code>a == b<\/code> and <code>a == c<\/code> then <code>b == c<\/code>. Therefore, only two comparisons are needed:<\/p>\n<p><code><br \/>\n(a == b)<br \/>\n(a == c)<\/code><\/p>\n<p>In C, you could code such as comparison as:<\/p>\n<p><code>if( a == b && a == c )<\/code><\/p>\n<p>The program compares variables <code>a<\/code> and <code>b<\/code> and gets the result, TRUE or FALSE. Then it compares <code>b<\/code> and <code>c<\/code> and gets the result, TRUE or FALSE. Then it compares those two results: TRUE &amp;&amp; TRUE is what you need to see if all variables are equal to each other; TRUE &amp;&amp; TRUE evaluates to TRUE.<\/p>\n<p>Here&#8217;s sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a,b,c;\r\n\r\n    a=10; b=10; c=10;\r\n\r\n    if( a == b &amp;&amp; a == c )\r\n        puts(\"The variables are equal to each other\");\r\n    else\r\n        puts(\"The variables are not equal to each other\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Then I thought of a brilliant shortcut:<\/p>\n<p><code>if( a == b == c )<\/code><\/p>\n<p>So I modified the code. Here&#8217;s the output:<\/p>\n<pre><code>The variables are not equal to each other<\/code><\/pre>\n<p>This mistake is common in programming, mostly because the programmer tries to over-apply logic.<\/p>\n<p>Following the order of precedence, variable <code>a<\/code> is compared with variable <code>b<\/code> and the result is generated, TRUE or FALSE. Then that result is compared with variable <code>c<\/code>, as in:<\/p>\n<p><code>if(TRUE == c)<\/code><\/p>\n<p>Or:<\/p>\n<p><code>if(FALSE == c)<\/code><\/p>\n<p>Odds are good that&#8217;s not the comparison you wanted. It works with values 0 and 1 &mdash; and I&#8217;ve used that trick in my code &mdash; but it doesn&#8217;t work with other values, such as 10 in the sample program.<\/p>\n<p>The bottom line is that when you need to compare three values, you can get by with only two comparisons. Put them into a single statement and use the logical <code>&amp;&amp;<\/code> to connect them: <code>( a == b &amp;&amp; a == c )<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A direct comparison of three items seems logical, but logic isn&#8217;t in the business of making sense to humans. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2270\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2270","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2270"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2270\/revisions"}],"predecessor-version":[{"id":2298,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2270\/revisions\/2298"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}