In the English language, we have different parts of speech,
like noun, adjective, preposition, verb.
And then there are a bunch of rules
that tell us how to put these different parts of speech together.
So if I said something like, "Dog books my eats,"
you'd be like, "What the heck does that mean?"
And if you didn't realize this before,
apparently, you can't just stick two nouns
in front of an adjective, in front of a verb.
Doesn't work.
But if I'd switched those and said, "My dog eats books,"
then you would totally know what I meant.
I could even replace this verb "eats"
with another verb like, I don't know, "throws",
and it would still make grammatical sense,
even if you can't imagine my dog throwing a book.
So in programming, instead of parts of speech,
we have these things called types.
You've already seen one of these: numbers.
We use numbers all the time in our drawing code.
And just like in English,
there are times it makes sense to use a number, and times when it doesn't.
If I started typing in this background function, "100 minus",
then whatever comes next better be a number,
or at least something that evaluates to a number like "14 + 15."
On the other hand, if I'd just typed "100 space",
well, I can't really put a number after that
because "100-space-10" doesn't mean anything.
So there's another type in programming, called the Boolean type.
And it's called Boolean
because some dude named George Boole invented it.
And unlike a number which has a ton of possible values,
a Boolean can only be one of two values: true or false.
And you can see when I type them they turn blue,
which means they're super special awesome words.
And you've already seen one place where we use booleans,
though you may not have realized it: if statements!
Let's get a quick refresh on how those work.
I'm just going to make a variable called 'number, ' give it a number, 40.
And write an If statement that says,
"If number is less than 50, then I will draw this first ellipse."
I'm just going to copy this into the If statement
and indent it by selecting everything and pressing tab.
So now this statement says,
"If number is less than 50," which it is, "then we'll draw the top ellipse."
And if I make number greater than 50,
you can see that the top ellipse disappears.
Alright, so this thing inside the parentheses
is actually a Boolean expression.
Remember, a math expression is anything that evaluates to a number:
like 3 plus 2 plus 4 times 8.
So a Boolean expression is anything that evaluates to a Boolean.
A good way to check if an expression evaluates to a Boolean,
is to stick the word "is" in front of it, and ask it like a question.
If it sounds like a yes or no question, then you know it's a Boolean expression.
So here we can say, "Is number less than 50?"
Yes, yes it is, and yes, that is a Boolean expression.
On the other hand, if I had something like, "4 + 4"
and I tried to ask, "is 4 + 4?" No. not a Boolean.
So back to our If statement.
I can actually put anything inside these parentheses,
as long as it's a Boolean or Boolean expression.
So I could say, "If true," and that ellipse would always be drawn.
Or I could say, "If false," and the ellipse would never be drawn.
I could also do something like "If 3 is less than 4,"
which is a Boolean expression that will always evaluate to true,
which is kinda pointless, the ellipse will always be drawn,
or "3 greater than 4," and that's always going to be false.
And I can also assign Booleans to variables, like this:
so I'm going to make a new variable, call it WinstonIsCool, and assign it
a Boolean value, so true or false.
Say true because Winston is definitely cool.
And now that this variable has a Boolean value,
I can copy it and stick it inside this If statement
and now you can see the ellipse is drawn,
because the value of WinstonIsCool is true.
I could also replace this with a Boolean expression,
so could be "2 less than 4."
Now if you're making a variable that's meant for a Boolean value,
you should give it a name
that describes the condition when the variable is true.
A good way to check if you've picked a good name for your variable
is to put it in an If statement and see if it makes sense as a condition.
So, forget WinstonIsCool, we already know that's true.
Let's say I had a variable called "muffins."
All right, "If muffins." Hmm.
Well, you know what?
That doesn't tell me anything, so that's a pretty bad variable name,
but if I had "If muffinsAreBaking", then that would tell me
that when this variable is true, then the muffins are baking.
And don't ask me what muffins, it's not important.
So for now let's go back to "If number is less than 50."
Cool.
Now let's look at some other Boolean expressions.
You've already seen "less than" and "greater than",
but you can also check if something is "less than or equal to."
So let's try, "If number is less than or equal to 48."
And we could also say, "If number is greater than or equal to 48."
If it is, we will draw this top-right ellipse.
Indent that.
And if you'd like to check if two things are exactly equal to each other
or you could say: "If number"
and then three equals signs, or "triple equals 48."
So that's a lot more like the equals sign you're used to in math,
except this time you have three of them in a row.
It's kind of overkill, right?
And then finally, we have if you want to check
if two things are not equal to,
so strictly not equal to, you can say,
"If number" and then an exclamation point, and then 2 equals signs, "48".
And then we will draw that last ellipse.
So if we go back to the top, we can see that number is 48,
so it is less than or equal to 48,
which is why the top-left ellipse is drawn.
It's also greater than or equal to 48, it's also equal to 48,
but it is not not equal to 48,
which is why we're missing that bottom-right ellipse.
And if we play around with number
you can see it changes which ellipses are drawn.
So now you guys know about Booleans.
And just like math expressions,
Boolean expressions can get really complicated.
But we will talk about those another time.