Conditionals II
Last chapter introduced turning everything into a bool. And then using that to determine if we want to execute a group of code or not.
It also explained both ... and
to combine two conditionals. This chapter introduces a few more keywords like that. Once done, you’ll have seen them all! (There aren’t many options for our simple bools.)
And at the end, we discover what happens if you ask a computer if a number is equal to a string 😉
Or
The and
keyword is true if both of its values are true, and false otherwise.
There’s also a middle ground.
Ask if one of the two is true with
either VAL1 or VAL2
It’s actually more powerful than this. The logical “or” operator is also happy if both are true. As such, it’s an inverted and
: it only returns false if all its values are false.
The player can play the game if it has some input device. We don’t care if it’s a gamepad ( = controller) or a keyboard. As long as one of them is true, the player can play the game!
Try changing them both to false. Or both to true.
Above/Below
Similarly, there’s a middle ground between “check if a is b” (very specific) and “check if a is not b” (very broad).
You might know these as “less than” (<
) or “greater than” (>
). That’s how most languages parse them. But, as usual, my language also supports just writing them out.
Use
above
to ask if the first value is greater than the other
Use
below
to ask if the first value is less than the other
These are mostly used to create limits on code. For example,
Try changing the numbers on purpose to make the conditional true or false.
A note about “else”
Almost all languages support a few more conditional statements than just if
.
Take the previous example. What if we also want to display a message to welcome new users (if we’re not full)? We’d need to write a whole new statement, which is just the first one with minor changes.
That’s why languages support a faster way: the else
statement. It executes if the previous statement is not true. Therefore, if you create an if-else, you’re sure one of the code blocks is executed. Either the first (if true) or the second (if false).
For example, this is how you’d write my example in JavaScript. That language powers interactivity in browsers, and therefore also this website.
1const usersOnline = 256
2const maxCapacity = 240
3
4if(usersOnline >= maxCapacity) {
5 console.log("Sorry, we're full!");
6} else {
7 console.log("Welcome!");
8}
I decided not to support this in my language.
Why?
- If you code well, you’ll almost never need this.
- Conversely, knowing it exists can easily destroy how legible (or “accessible”) your code is.
Right now, you probably won’t see how to avoid it. I haven’t explained the crucial parts yet. But by the end of the course, you will see!
Mixing types
Well, see for yourself. Try comparing two values of any type you like and see the results.
The rules here are usually the same as operating on two different data types (like multiplying a number and a string).
A static language simply forbids this. It crashes and tells you the two values should be the same type.
A dynamic language applies type coercion. It tries to force or “coerce” both values to the same type.
This differs per language. But the following rule is generally agreed upon.
It tries to make everything a number and then compare that.
Why?
- Computers like numbers the most. They are the “default”, so to speak.
- Numbers are easily mapped to bools:
false
= 0,true
= 1 - Strings are easily converted to numbers (if possible). A computer understands that the string
"3.41"
is the number3.41
That will explain 99% of type coercion. It’s most likely to fail when comparing a number and a string (that is not a number).
In general, though, do not rely on this. That’s also why I recommend a static language with “type safety” in the end. Relying on type coercion is like telling the computer: “do whatever you want with my code, I hope things turn out alright!”
Take control into your own hands. You decide the data type and you fully control what your code does. When combining two values into a conditional, make sure they are always the same type.
Conclusion
This ends the chapters about bools and conditionals.
In this section, I just want to repeat the most important part. To make sure you recognize how all of coding is just data transformations.
You’ve just learned how to convert anything to a bool.
That was the point of these two chapters. Now you can …
- Convert multiple bools to one bool with
and
oror
- Convert any two values to a bool with
is
- Convert two numbers to a bool with
above
orbelow
- Invert any bool with
not
Once you have the value, you can use it for conditionals. To determine which part of the code to execute or to ignore.
Now let’s see how we can turn everything into another number!
Want to support me?
Buy one of my projects. You get something nice, I get something nice.
Donate through a popular platform using the link below.
Simply giving feedback or spreading the word is also worth a lot.