{"id":357,"date":"2013-10-19T00:01:28","date_gmt":"2013-10-19T08:01:28","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=357"},"modified":"2013-10-19T08:10:53","modified_gmt":"2013-10-19T16:10:53","slug":"three-choices","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=357","title":{"rendered":"Three Choices"},"content":{"rendered":"<p>Either-or decisions are easy. They&#8217;re similar to the 1 or 0 of low-level programming: True\/False, On\/Off, Yes\/No. When a third element appears, decision-making becomes more complex. To put it another way: That third element can drive both the programmer and the program crazy.<br \/>\n<!--more--><br \/>\nFor example, consider a situation where a chunk of code must be executed when this condition is met:<\/p>\n<p>The value of x is equal to 38<br \/>\nor<br \/>\nThe value of y is in the range of 10 to 15<\/p>\n<p>Translating that requirement directly into a <em>if<\/em> statement looks something like this:<\/p>\n<pre><code>if( x==38 || y>=10 && y<=15 )<\/code><\/pre>\n<p>That statement reads, left-to-right, like the condition stated above. As a seasoned programmer, however, I don't blindly trust multiple conditions within an <em>if<\/em> statement. Normally I'd hurriedly wrap parentheses around part of the equation, but I'll let this one go for demonstration purposes. Here'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 x,y;\r\n\r\n    printf(\"Enter a value for x: \");\r\n    scanf(\"%d\",&x);\r\n    printf(\"Enter a value for y: \");\r\n    scanf(\"%d\",&y);\r\n\r\n    if( x==38 || y&gt;=10 && y&lt;=15 )\r\n        puts(\"The condition is acceptable\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Below is a sample run using the values 38 for <code>x<\/code> and 12 for <code>y<\/code>, which should meet both conditions, with <code>x<\/code> at 38 and the value of <code>y<\/code> between 10 and 15.<\/p>\n<pre><code>Enter a value for x: 38\r\nEnter a value for y: 12\r\nThe condition is acceptable<\/code><\/pre>\n<p>And here's a sample run using the values 5 and 9, which shouldn't meet the condition:<\/p>\n<pre><code>Enter a value for x: 5\r\nEnter a value for y: 9<\/code><\/pre>\n<p>Other sample runs demonstrated that the code works, which is good, but it's not an excuse to be sloppy or assume anything.<\/p>\n<p>If you have compiler warnings on, you'll see a message appear whenever you attempt to use more than one logical operator within an <em>if<\/em> comparison:<\/p>\n<p><code>Line 12: warning: suggest parentheses around && within ||<\/code><\/p>\n<p>The parentheses solution is this:<\/p>\n<pre><code>if( x==38 || (y&gt;=10 && y&lt;=15) )<\/code><\/pre>\n<p>In this case, however, the parentheses aren't really needed thanks to the order of precedence: The <code>&&<\/code> operator ranks higher than the <code>||<\/code> operator, and associativity goes left-to-right. Therefore the parentheses are unnecessary. They do, however, make the code more readable.<\/p>\n<p>My advice is to use parentheses whenever you're in doubt, which could imply all the time. Don't worry about any scourge attached to the parenthetical solution; if it works, and especially if it's more readable, consider it good.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The either-or choices are easy to make. Add in a third item and &#8212; oh no! &#8212; you&#8217;re going to have to use your brain and some logic. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=357\">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-357","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\/357","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=357"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions\/370"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}