<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Stuart&#039;s Pixel Games]]></provider_name><provider_url><![CDATA[http://stuartspixelgames.com]]></provider_url><author_name><![CDATA[SPG]]></author_name><author_url><![CDATA[https://stuartspixelgames.com/author/stuie89/]]></author_url><title><![CDATA[What Are Logical Operators &#8211;&nbsp;C#]]></title><type><![CDATA[link]]></type><html><![CDATA[<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.humblebundle.com/g/puzzledorf"><img data-attachment-id="7638" data-permalink="https://stuartspixelgames.com/wide-ad/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png" data-orig-size="1920,586" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="wide-ad" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024" src="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736" alt="" class="wp-image-7638" srcset="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736 736w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1472 1472w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=150 150w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300 300w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=768 768w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024 1024w" sizes="(max-width: 736px) 100vw, 736px" /></a></figure></div>


<p>You might have seen&nbsp;<strong>if statements</strong> with some confusing symbols:&nbsp;<strong>&amp;&amp;, ||&nbsp;</strong>or&nbsp;<strong>!.</strong> These are called the&nbsp;<strong>logical operators</strong>, and are used to create more complicated logic within an if statement. They are:</p>



<p><strong>&amp;&amp;&nbsp;</strong>&#8211; the&nbsp;<strong>Logical </strong><strong>AND</strong>&nbsp;<strong>operator</strong></p>



<p><strong>||</strong> &#8211; the&nbsp;<strong>Locical OR operator</strong></p>



<p><strong>!&nbsp;</strong>&#8211; the&nbsp;<strong>Logical NOT operator</strong></p>



<p>I use them frequently in <strong><a href="https://www.humblebundle.com/g/puzzledorf">Puzzledorf</a></strong> to check when certain things are occurring. For example, take a look at the image below. When the player moves, I want to check if there&#8217;s any object in the space he wants to move. If the space is empty, he can walk. If it&#8217;s not empty, what&#8217;s in the space? Can he push it, or is it immovable? etc.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.humblebundle.com/g/puzzledorf"><img data-attachment-id="6100" data-permalink="https://stuartspixelgames.com/completelevelanimation/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2021/10/completelevelanimation.gif" data-orig-size="490,415" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="completelevelanimation" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2021/10/completelevelanimation.gif?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2021/10/completelevelanimation.gif?w=490" src="https://stuartspixelgames.files.wordpress.com/2021/10/completelevelanimation.gif?w=490" alt="" class="wp-image-6100" /></a></figure></div>


<h2 id="quick-examples">Quick Examples</h2>



<p>But what do they do? I will give some quick examples.</p>



<pre class="wp-block-preformatted"><strong>if (A &amp;&amp; B)</strong>
<strong>{</strong>
<strong>  Jump;</strong>
<strong>}</strong></pre>



<p>In the above psuedocode, both <strong>A&nbsp;</strong>and&nbsp;<strong>B</strong> have to be true&nbsp;for the player to jump. That is what&nbsp;<strong>&amp;&amp; AND </strong>means, that both conditions either side must be true.</p>



<pre class="wp-block-preformatted"><strong>if (A || B)</strong>
<strong>{</strong>
<strong>  Jump;</strong>
<strong>}</strong></pre>



<p>In the above case, the player will jump when either <strong>A or B&nbsp;</strong>are true, but they do not both have to be true. So ||&nbsp;<strong>OR&nbsp;</strong>is true so long as one of the checks on either side of <strong>|| OR</strong> are true.</p>



<pre class="wp-block-preformatted"><strong>number1 = 5;</strong>
<strong>number2 = 10;
</strong>
<strong>if (number1 != number2)</strong>
<strong>{</strong>
<strong>  PrintScore;</strong>
<strong>}</strong></pre>



<p>In the above case, the score will print if&nbsp;<strong>number1</strong> is&nbsp;<strong>NOT</strong> equal to&nbsp;<strong>number2.</strong>&nbsp;<strong>! NOT</strong> literally means&nbsp;<strong>NOT</strong>. It can be used in a variety of ways.</p>



<pre class="wp-block-preformatted"><strong>lightOn = false;</strong>
<strong>pressButton = false;</strong>

<strong>if(lightOn)</strong>
<strong>{</strong>
<strong>  LightUpRoom;</strong>
<strong>}
</strong>
<strong>if(!lightOn)</strong>
<strong>{</strong>
<strong>  MakeRoomDark;</strong>
<strong>}

if (pressButton)
{
  !lightOn;
}
</strong></pre>



<p>Above are other ways you can use <strong>! NOT.</strong> First, it&#8217;s a different way to check if a value is true or false.</p>



<pre class="wp-block-preformatted"><strong>if (lightOn)</strong></pre>



<p>is the same as:</p>



<pre class="wp-block-preformatted"><strong>if (lightOn == true)</strong></pre>



<p>In the same way:</p>



<pre class="wp-block-preformatted"><strong>if (!lightOn)</strong></pre>



<p>is the same as:</p>



<pre class="wp-block-preformatted"><strong>if (lightOn == false)</strong></pre>



<p>So first we check to see whether the light is on or not, and if the light is on, we light up the room. If it&#8217;s not, the room becomes dark.</p>



<p>Then we check to see if the button is pressed.</p>



<pre class="wp-block-preformatted"><strong>if (pressButton)
{
  !lightOn;
}</strong></pre>



<p>In the above code snippet:</p>



<pre class="wp-block-preformatted"><strong>!lightOn;</strong></pre>



<p>can be the same as writing:</p>



<pre class="wp-block-preformatted"><strong>lightOn = false;</strong></pre>



<p>or writing:</p>



<pre class="wp-block-preformatted"><strong>lightOn = true;</strong></pre>



<p>It simply means that, when applied in this way, it flips the value of a&nbsp;<strong>boolean</strong>, which is a&nbsp;<strong>true or false</strong> variable, to be the opposite value. So when we write:</p>



<pre class="wp-block-preformatted"><strong>!lightOn;</strong></pre>



<p>If&nbsp;<strong>lightOn</strong> was initially false, it would be switched to true. If it was initially true, it will be switched to false. It&#8217;s a perfect toggle for a light switch, because every time you press the light switch, it will be swapped to the opposite state. This means we don&#8217;t need to check if the light switch is on or off, saving us writing lots of extra code.</p>



<h2 id="ordering-of-operations">Ordering of Operations</h2>



<p>All code runs from left to right, top to bottom. This is important to know &#8211; it can be useful for solving errors, setting up correct logic, and optimisation.</p>



<p>Consider the following code:</p>



<pre class="wp-block-preformatted"><strong>if (A &amp;&amp; B)</strong>
<strong>{</strong>
<strong>  Jump;</strong>
<strong>}</strong></pre>



<p>We know that both&nbsp;<strong>A and B&nbsp;</strong>must be true. However, if&nbsp;<strong>A</strong> is&nbsp;false, the if statement will not even check if&nbsp;<strong>B&nbsp;</strong>is true. This can be useful in optimisation, as the program will not keep checking your other conditions, saving CPU power.</p>



<p>A lot of CPU power will go to checking your <strong>if / else</strong> statements, so consider them wisely. <span style="color:var(--color-text);font-size:1rem;">If you can reduce how much code needs to run or be checked, this can go a long way in optimisation.&nbsp;</span></p>



<p>On the other hand:</p>



<pre class="wp-block-preformatted"><strong>if (A || B)</strong>
<strong>{</strong>
<strong>  Jump;</strong>
<strong>}</strong></pre>



<p>If&nbsp;<strong>A</strong> is false, the statement will still check if&nbsp;<strong>B</strong> is true.</p>



<h2 id="complex-examples">Complex Examples</h2>



<p>Now that you understand the basics, here are some more complex examples showing ways you can combine them together.</p>



<pre class="wp-block-preformatted"><strong>if ( A &amp;&amp; B &amp;&amp; C )</strong>
<strong>{</strong>
<strong>&nbsp; PrintScore;</strong>
<strong>}</strong></pre>



<p>In the above, you check if both&nbsp;<strong>A, B&nbsp;</strong>and&nbsp;<strong>C</strong> are all true. If&nbsp;<strong>A</strong> is false, it won&#8217;t check&nbsp;<strong>B&nbsp;</strong>and&nbsp;<strong>C</strong>. If&nbsp;<strong>B</strong> is false,&nbsp;it won&#8217;t check&nbsp;<strong>C.</strong> So the more you combine together, it will always stop checking at the first condition that returns&nbsp;<strong>false.</strong></p>



<p>Consider the following:</p>



<pre class="wp-block-preformatted"><strong>if ( A &amp;&amp; ( B || C ) )</strong>
<strong>{</strong>
<strong>&nbsp; PrintScore;</strong>
<strong>}</strong></pre>



<p>Here we have used&nbsp;<strong>&amp;&amp;&nbsp;</strong>and&nbsp;<strong>||&nbsp;</strong>together. Notice that we have nested if statements together in parentheses. This is similar to math class, where:</p>



<pre class="wp-block-preformatted"><strong>1 + ( 6 / 2 )</strong></pre>



<p>You always solve the brackets first. In the math equation, you have to divide&nbsp;<strong>6 by 2,&nbsp;</strong>which is <strong>3</strong>, before you add <strong>1, </strong>which is <strong>4</strong>. Furthermore, if you had:</p>



<pre class="wp-block-preformatted"><strong>1 + ( 6 / ( 4 / 2) )</strong></pre>



<p>You have to solve the innermost brackets first. In this case, you divide&nbsp;<strong>4 by 2, </strong>which is <strong>2</strong>,&nbsp;then divide<strong> 6&nbsp;</strong>by the result, which is <strong>3</strong>, then you add <strong>1</strong> to that result. The answer is <strong>4</strong>.</p>



<p>In our&nbsp;<strong>if statement</strong>, first it checks whether&nbsp;<strong>A&nbsp;</strong>is true.<strong>&nbsp;</strong>If it is, it also then checks our&nbsp;<strong>|| OR</strong> statement in the brackets. Either&nbsp;<strong>B&nbsp;</strong>or&nbsp;<strong>C</strong> would have to be true for <strong>PrintScore</strong> to run.</p>



<pre class="wp-block-preformatted"><strong>if ( A &amp;&amp; ( B &amp;&amp; C ) )</strong>
<strong>{</strong>
<strong>&nbsp; PrintScore;</strong>
<strong>}</strong></pre>



<p>In this variation, <strong>A</strong>, <strong>B</strong> and <strong>C</strong> would still all have to be true, and so you don&#8217;t really need the <strong>( )</strong> <strong>parentheses</strong>.</p>



<pre class="wp-block-preformatted"><strong>if ( A || ( B &amp;&amp; C ) )</strong>
<strong>{</strong>
<strong>&nbsp; PrintScore;</strong>
<strong>}</strong></pre>



<p>In the above example, it will be true if either&nbsp;<strong>A</strong> is true,&nbsp;<strong>OR</strong> either&nbsp;<strong>B</strong> and <strong>C </strong>are both true.</p>



<pre class="wp-block-preformatted"><strong>if ( A || ( (B &amp;&amp; C) || D ) )</strong>
<strong>{</strong>
<strong>&nbsp; PrintScore;</strong>
<strong>}</strong></pre>



<p>If <strong>A</strong> is true, or <strong>B&nbsp;</strong>and&nbsp;<strong>C</strong> are both true,&nbsp;or&nbsp;<strong>D</strong> is true,<strong>&nbsp;</strong>then our score will be printed.</p>



<p>So I hope you now have a good idea of what the&nbsp;<strong>logical operators</strong> are, and helpful ways that you might use them.</p>



<div class="is-layout-flow wp-block-group"><div class="wp-block-group__inner-container">
<p class="has-text-align-center has-medium-font-size"><strong>If you enjoyed reading, try my game <a href="https://www.humblebundle.com/g/puzzledorf">Puzzledorf</a>.</strong></p>
</div></div>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="https://www.humblebundle.com/g/puzzledorf"><img data-attachment-id="7638" data-permalink="https://stuartspixelgames.com/wide-ad/" data-orig-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png" data-orig-size="1920,586" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="wide-ad" data-image-description="" data-image-caption="" data-medium-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300" data-large-file="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024" src="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736" alt="" class="wp-image-7638" srcset="https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=736 736w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1472 1472w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=150 150w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=300 300w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=768 768w, https://stuartspixelgames.files.wordpress.com/2022/04/wide-ad.png?w=1024 1024w" sizes="(max-width: 736px) 100vw, 736px" /></a></figure></div>]]></html><thumbnail_url><![CDATA[https://stuartspixelgames.files.wordpress.com/2017/03/c.png?fit=440%2C330]]></thumbnail_url><thumbnail_width><![CDATA[415]]></thumbnail_width><thumbnail_height><![CDATA[255]]></thumbnail_height></oembed>