Join the Discord. Talk about Game Dev. Talk about Gaming.
You might have seen if statements with some confusing symbols: &&, || or !. These are called the logical operators, and are used to create more complicated logic within an if statement. They are:
&& – the Logical AND operator
|| – the Locical OR operator
! – the Logical NOT operator
I use them frequently in Puzzledorf 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’s any object in the space he wants to move. If the space is empty, he can walk. If it’s not empty, what’s in the space? Can he push it, or is it immovable? etc.
Quick Examples
But what do they do? I will give some quick examples.
if (A && B) { Jump; }
In the above psuedocode, both A and B have to be true for the player to jump. That is what && AND means, that both conditions either side must be true.
if (A || B) { Jump; }
In the above case, the player will jump when either A or B are true, but they do not both have to be true. So || OR is true so long as one of the checks on either side of || OR are true.
number1 = 5; number2 = 10; if (number1 != number2) { PrintScore; }
In the above case, the score will print if number1 is NOT equal to number2. ! NOT literally means NOT. It can be used in a variety of ways.
lightOn = false; pressButton = false; if(lightOn) { LightUpRoom; } if(!lightOn) { MakeRoomDark; } if (pressButton) { !lightOn; }
Above are other ways you can use ! NOT. First, it’s a different way to check if a value is true or false.
if (lightOn)
is the same as:
if (lightOn == true)
In the same way:
if (!lightOn)
is the same as:
if (lightOn == false)
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’s not, the room becomes dark.
Then we check to see if the button is pressed.
if (pressButton) { !lightOn; }
In the above code snippet:
!lightOn;
can be the same as writing:
lightOn = false;
or writing:
lightOn = true;
It simply means that, when applied in this way, it flips the value of a boolean, which is a true or false variable, to be the opposite value. So when we write:
!lightOn;
If lightOn was initially false, it would be switched to true. If it was initially true, it will be switched to false. It’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’t need to check if the light switch is on or off, saving us writing lots of extra code.
Ordering of Operations
All code runs from left to right, top to bottom. This is important to know – it can be useful for solving errors, setting up correct logic, and optimisation.
Consider the following code:
if (A && B) { Jump; }
We know that both A and B must be true. However, if A is false, the if statement will not even check if B is true. This can be useful in optimisation, as the program will not keep checking your other conditions, saving CPU power.
A lot of CPU power will go to checking your if / else statements, so consider them wisely. If you can reduce how much code needs to run or be checked, this can go a long way in optimisation.
On the other hand:
if (A || B) { Jump; }
If A is false, the statement will still check if B is true.
Complex Examples
Now that you understand the basics, here are some more complex examples showing ways you can combine them together.
if ( A && B && C ) { PrintScore; }
In the above, you check if both A, B and C are all true. If A is false, it won’t check B and C. If B is false, it won’t check C. So the more you combine together, it will always stop checking at the first condition that returns false.
Consider the following:
if ( A && ( B || C ) ) { PrintScore; }
Here we have used && and || together. Notice that we have nested if statements together in parentheses. This is similar to math class, where:
1 + ( 6 / 2 )
You always solve the brackets first. In the math equation, you have to divide 6 by 2, which is 3, before you add 1, which is 4. Furthermore, if you had:
1 + ( 6 / ( 4 / 2) )
You have to solve the innermost brackets first. In this case, you divide 4 by 2, which is 2, then divide 6 by the result, which is 3, then you add 1 to that result. The answer is 4.
In our if statement, first it checks whether A is true. If it is, it also then checks our || OR statement in the brackets. Either B or C would have to be true for PrintScore to run.
if ( A && ( B && C ) ) { PrintScore; }
In this variation, A, B and C would still all have to be true, and so you don’t really need the ( ) parentheses.
if ( A || ( B && C ) ) { PrintScore; }
In the above example, it will be true if either A is true, OR either B and C are both true.
if ( A || ( (B && C) || D ) ) { PrintScore; }
If A is true, or B and C are both true, or D is true, then our score will be printed.
So I hope you now have a good idea of what the logical operators are, and helpful ways that you might use them.
Leave a Reply