{"id":5333,"date":"2022-05-07T00:01:27","date_gmt":"2022-05-07T07:01:27","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5333"},"modified":"2022-04-30T10:45:54","modified_gmt":"2022-04-30T17:45:54","slug":"conditional-expressions-used-as-values","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5333","title":{"rendered":"Conditional Expressions Used as Values"},"content":{"rendered":"<p>It was weird when I first saw it: A conditional expression used to determine a value. It sounds odd, but it works.<br \/>\n<!--more--><br \/>\nConsider <code>m&lt;3<\/code>. This expression isn&#8217;t an assignment; it&#8217;s a condition. Normally, you see it in a conditional statement, such as:<\/p>\n<p><code>if( m&lt;3 )<\/code><\/p>\n<p>The conditional expression evaluates as either TRUE or FALSE, though numeric values are behind each logical term: In C programming, FALSE is zero. TRUE is any other value. (In other programming languages, you may find -1 is always FALSE, though this value is TRUE in C.)<\/p>\n<p>What&#8217;s happening internally is that the compiler does the math on the conditional expression. Effectively, <code>m&lt;3<\/code> is equal to zero when variable <code>m<\/code> is three or greater; it&#8217;s one when <code>m<\/code> is less than three. This operation occurs for every conditional expression. You can see it happen by running the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_05_07-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_05_07-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int m = 1;\r\n    printf(\"%d\\n\",m&lt;3);\r\n    m = 5;\r\n    printf(\"%d\\n\",m&lt;3);\r\n    return(0);\r\n}<\/pre>\n<p>From the <em>printf()<\/em> statement at Line 6, I might think, &#8220;What the heck is going to print?&#8221; Because variable <code>m<\/code> is one, the result of <code>m&lt;3<\/code> is TRUE. The decimal integer value (<code>%d<\/code>) output is <code>1<\/code>.<\/p>\n<p>In the Line 8, the conditional expression becomes FALSE. The value output is zero. Here&#8217;s the sample run:<\/p>\n<p><code>1<br \/>\n0<\/code><\/p>\n<p>Using a conditional expression as a value rather than using the results to make a decision is unusual, but it happens. I remember the first time I saw such a construction in a program. I scrutinized the code because I was unsure why the condition&#8217;s result was used in such an unexpected manner. In this instance, the statement looked like this:<\/p>\n<p><code>r = offset + m<3;<\/code><\/p>\n<p>I forget the specifics of what the code did, as the unusual construction caught my attention. Regardless of what variable <code>r<\/code> is trying to represent, the result is either equal to <code>offset<\/code> when variable <code>m<\/code> is three or greater or equal to <code>offset<\/code> plus one. It's an interesting construction, one that works, but was completely new to me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A conditional expression can be TRUE or FALSE. These operations can also be interpreted as values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5333\">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-5333","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\/5333","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=5333"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5333\/revisions"}],"predecessor-version":[{"id":5354,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5333\/revisions\/5354"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}