Tutorial 9: And, Or, Not

In this tutorial, we'll dive a little further into *if statements** and what can be done with them.

We know that if we want to check something, we can use an if statement. For example:

  1. dave = true
  2.
  3. if dave then
  4.     print( "Hurray!" )
  5. endif

Nice and simple to start with. We have a variable called dave which we've set to true.

Our if statement on line 3 checks to see if the dave variable is true. If it is, we print "Hurray!". How fantastic.

Let's introduce some more complexity. It's time to talk about two words: and and or

These words are called operators. We can use these operations to check multiple things together in our if statement.

Check it out.

  1. dave = true
  2. kat = true
  3.
  4. if dave and kat then
  5.     print( "Hurray!" )
  6. else 
  7.     print( "Oh no..." )
  8. endif

First of all, we've introduced a new variable into the mix. It's kat!

Now our if statement checks two things. We check if dave is true, and if kat is true. This if statement will return either true or false, and will only be true if both our variables are true. If we change either of the variables to false, our if statement will be false and we'll print "Oh no...".

The and operation works very similar to the way we use it to speak, it combines things.

Or is a little different, but again it works very similarly to the way we use it to speak. Or checks if either of the conditions are true.

See if you can predict the difference with the code below:

  1. dave = true
  2. kat = true
  3.
  4. if dave or kat then
  5.     print( "Hurray" )
  6. else 
  7.     print( "Oh no..." )
  8. endif

If we run this code, we'll get "Hurray", because both the dave and kat variables are true, and the if statement checks if either are true. Even if we change one of them to false, we'll still get "Hurray".

For our if statement to give us "Oh no...", we would have to make both the dave and kat variables false.

Summary

Before we get into some more practical examples of using and and or, take a look below for a summary.

Try to think of the and and or operations as things which give you a true or false answer.

And

Take a look at these examples below. No need to type them into your editor as they are unfinished.

The if statement below will give us true

  1. dave = true
  2. kat = true
  3.
  4. if dave and kat then

Here, our if statement gives us false:

  1. dave = false
  2. kat = true
  3.
  4. if dave and kat then

Here, our if statement gives us false:

  1. dave = false
  2. kat = false
  3.
  4. if dave and kat then

Or

The if statement below will give us true:

  1. dave = true
  2. kat = true
  3.
  4. if dave or kat then

Here, our if statement gives us true:

  1. dave = false
  2. kat = true
  3.
  4. if dave or kat then

Here, our if statement gives us false:

  1. dave = false
  2. kat = false
  3.
  4. if dave or kat then

Not

There is also another star player here... But for some reason they're not here...

That's right, it's not!

This is getting confusing...

not does exactly what you'd think it does. It checks if something is not true. We can use the ! symbol to use not, but it's also fine to use the word!

Let's see an example of not in our Dave and Kat program.

  1. dave = false
  2. kat = false
  3.
  4. if not dave == true or not kat == true then

This if statement will give us true, because we are checking to see if either dave or kat is false (not true).

To write this using the ! symbol, the syntax is a little different, spot the difference:

  1. dave = false
  2. kat = false
  3.
  4. if dave != true or kat != true then

See how we must put the ! just before the = sign? Think of this as saying "not equal to".

We can also write this another way, even more efficiently!

  1. dave = false
  2. kat = false
  3.
  4. if !dave or !kat then

Either of these ways of writing your code is perfectly fine, it's up to you to choose which you prefer!

Using And, Or and Not

Okay! Let's see a couple of examples where this might be really useful for a game project.

When we want to check the buttons on the Joy-Con's, we use if statements to check which buttons are being pressed. Here, and and or can help very much.

Let's say we want something to happen only if we are moving and pressing the A button.

  1. loop 
  2.     clear()
  3.
  4.     j = controls( 0 )
  5.
  6.     if j.lx != 0 and j.a then
  7.         print( "Hurray!" )
  8.     endif
  9.
 10.     update()
 11. repeat

As you can see we have a simple loop. We clear the screen at the very start, and update the screen at the very end.

Line 4 uses the controls() function which tells us the current state of all the controls. The result is stored in a variable called j.

Our if statement checks if the Joy-Con left control stick (lx) is being pushed in any direction j.lx != 0. If the control stick value is 0 it's totally still and right in the middle!

We also check if the A button is being pressed with joy.a = true.

Because of the and between these two checks, both must be true before our if statement becomes true.

Let's see an example using or.

  1. loop 
  2.     clear()
  3.
  4.     joy = controls( 0 )
  5.
  6.     if joy.zl or joy.zr then
  7.         print( "Hurray!" )
  8.     endif
  9.
 10.     update()
 11. repeat

This time we're checking for different buttons. We are now checking if either the left shoulder button joy.zl or the right shoulder button joy.zr become pressed.

This way, we could make the same thing happen for two different button presses.

Finally, let's see an example using not.

This time, we'll make it so that "Hurray" appears only when the button isn't being pressed.

  1. loop 
  2.     clear()
  3.
  4.     j = controls( 0 )
  5.
  6.     if !j.zl or !j.zr then
  7.         print( "Hurray!" )
  8.     endif
  9.
 10.     update()
 11. repeat

We've only added the ! symbols to our code, and now the result is totally different!

We are now checking if the shoulder buttons are false. While they are false, "Yahoo!" appears on the screen, but as soon as one becomes pressed and becomes true, our if statement is false and we do not see "Hurray!".

Re-cap

and, or and not are called operators. They perform an operation.

We mainly use them in if statements to make them capable of checking more complex things.

They are particularly useful when using the controls(0) function.

Imagine we want a character in a game to be able to perform a jumping attack. We will need and in this situation because we are checking if the character is jumping and if the attack button is pressed.

See you in the next tutorial!

Functions and Keywords used in this tutorial

and, clear(), controls(), else, endIf, if, loop, not, or, print(), repeat, then, update()