-
What is Code?
-
Data
-
Input
-
Boolean logic and decisions
-
Loops
-
Modules
-
Examples
-
Exercises
Equals instruction
Often, we need to check if two values are equal.
To do this we use this instruction called equals which is written in code as ==
It is made of two equals signs which is confusing as we already use one equals sign as the assignment instruction.
The == instruction is like the + or - math instructions, but it returns a boolean value of True if the values are equal and False otherwise.
Here is an example:
The equals instruction == is between between the two values it needs to check. It is substituted with the result of the check.
So in this case, 3 == 4 is replaced by False and the if statement uses that value to make a decision.
Remember, the single equals is for assigning a value to a variable, and a double equals is for checking a value.
This is the same code, but we check if 3 equals 4 and assign the result to a variable, then that variable is substituted in the if statement check.
The equals instruction also works on different types