Lesson 3
Expressions and Operators
Learn how variables store values, how assignment works, and why x = 100; actually means something.
Expressions
An expression is simply a piece of code that produces a value. Here’s some examples:
10 + 5
hp - 1
score > 100
"Hello " + player_name
In all those cases, we are performing some operation (addition, comparison, etc) and there will be some result that comes out of it.
But wait a minute, how does score > 100 produce a value?
The answer is that it produces a boolean result: it’s either true or it’s false. So while it is comparing two things, not adding or subtracting things like you would imagine an operation doing, the end result is that score is either greater than 100 or it’s not, and so it’s a true / false response.
GM has a “truthy” sense about it, which means that it’ll try to treat something as a boolean, even if it isn’t explicitly the keywords true or false.
That means that GM will happily treat 1 as true and 0 as false. Or 0.2 as false and 0.6 as true. The cutoff point for numbers is “if it’s greater than 0.5, it’s considered true, if not it’s false.
This also means that you can do some strange things, like ask if an object is true and GM will happily answer. But that’s more of a side effect of the “truthiness” and doesn’t really relate often to actual coding practices.
One more quick dive into expressions:
hp = hp - 1;
The left hand side is the variable. The right hand side is the expression. And the variable is being assigned the result of the expression. So if hp has been declared with the value 10, then the end result after this code is hp holds 9.
Operators
Coding has a series of operators that can be used in expressions. These are one of the “arcane symbols” parts of code. Operators take values on either side of them and produce a new value.
Sometimes that value is a number, like 10 + 5 is 15. Sometimes it’s a true / false answer, like hp > 0. Very similar to expressions.
There are familiar ones, like the maths operations that we all know and love (with varying definitions of love depending on your personal persuasion):
+
-
*
/
Add, subtract, multiply and divide, quite simple.
But there are also slightly more arcane ones, like the comparisons:
==Equals (the traditional equals: does the left hand equal the right hand,trueorfalse?)!=Not equals (an inversion of equals, does the left hand not equal the right hand)>Greater than<Lesser than>=Greater than or equal to<=Lesser than or equal to
All these conditions return true or false, so you are using them to ask a question about the two sides of the comparison:
x == y // Does x equal y?
x != y // Does x not equal y?
x > y // Is x greater than y
And so on.
GM will let you use a single = sign for both assignment and equality. This is confusing, and quite frankly, dumb. You should always use == to check equality and = to assign, even if GM lets you get away with = for both. Don’t get into the habit of always using =.
Finally, we have amongst the most arcane of the lot, drawn from the deep wellspring of ancient magic: logical operators.
Logical operators are a way to combine or flip true / false questions. Let’s look at them before we discuss further:
&&
||
!
A comparison gives you a single yes / no answer:
hp > 0
That’s either true or false. Logical operators let you join, choose between or flip those true or false answers.
&& means and. You use it when you have two comparisons and you want the answer to be true only when both the comparisons are true:
hp <= 0 && invincible == false
That’s asking two questions: is hp less than or equal to 0? and is invincible equal to false? Only if both of those are true does the full expression return true, otherwise it returns false.
Lets get through the rest of the logical operators.
|| means or and it returns true if at least one of the expressions is true. If both are true, it still returns true.
hp <= 0 || invincible == false
If either hp is less than 0 or invincible is false, the full expression will return true.
! means not and it’s a negation. It’s different to the previous two in that it doesn’t join two expressions together. It goes before one expression and flips its result. So if an expression returns true, ! will make it return false.
!has_key
If has_key is true, then !has_key returns false. If has_key is false, !has_key returns true.
In fact, using ! we could shorten the previous invincible comparisons:
hp <= 0 && !invincible
That’s semantically the same as our previous version with the full == false.
A cool little feature about the ! operator is that you can use it to toggle the value of a boolean variable.
is_alive = !is_alive;If is_alive starts off true, it will be swapped to false. If it starts off false, it will be swapped to true.
And since GM is so permissive, this also works for 1 and 0, it will swap between them depending on what value the variable has at the time of the toggle.
There are more logical operators available to us, but they are used much less frequently and require a deeper dive into computer science to properly understand, so we’ll leave them for a future lesson.
Ok, that was a lot to take in. It’s fine if you don’t fully understand all of this right now. It will become clearer the more you work with actual code later on.
You might also be asking “When in the world would I need to use these?”
As you’ll see later on, they are actually quite useful in many different situations, such as if statements, our very next topic, which require a true or false answer before they can decide what code to run.
Try it: Pick the correct operator
Which symbol returns true only when both conditions are true?
Symbol checks
Complete the tasks for the operators introduced in this lesson, like && and !.
Task: Type the correct operator
Enter the symbol that flips a true or false value.
Task: Fill in the operator
What's the logical operator that returns true when either or both of these expressions are true?
hp <= 0 invincible == false Code practice
Short code tasks that reinforce hp assignment examples.
Task: Enter the correct code
Write a line of code that adds 5 to hp.
Task: Fix the broken line
Change the comparison into an assignment.