Booleans (Video Version)
-
0:03 - 0:06In the English language,
we have different parts of speech, -
0:06 - 0:08like noun, adjective, preposition, verb.
-
0:08 - 0:10And then there are a bunch of rules
-
0:10 - 0:13that tell us how to put
these different parts of speech together. -
0:13 - 0:19So if I said something like,
"Dog books my eats," -
0:19 - 0:22you'd be like,
"What the heck does that mean?" -
0:22 - 0:24And if you didn't realize this before,
-
0:24 - 0:26apparently, you can't just stick
two nouns -
0:26 - 0:28in front of an adjective,
in front of a verb. -
0:28 - 0:29Doesn't work.
-
0:29 - 0:35But if I'd switched those and said,
"My dog eats books," -
0:35 - 0:37then you would totally know what I meant.
-
0:37 - 0:39I could even replace this verb "eats"
-
0:39 - 0:43with another verb like,
I don't know, "throws", -
0:43 - 0:45and it would still make grammatical sense,
-
0:45 - 0:48even if you can't imagine
my dog throwing a book. -
0:48 - 0:50So in programming,
instead of parts of speech, -
0:50 - 0:52we have these things called types.
-
0:52 - 0:54You've already seen one of these: numbers.
-
0:54 - 0:57We use numbers all the time
in our drawing code. -
0:57 - 0:59And just like in English,
-
0:59 - 1:02there are times it makes sense to use
a number, and times when it doesn't. -
1:02 - 1:06If I started typing in
this background function, "100 minus", -
1:06 - 1:09then whatever comes next
better be a number, -
1:09 - 1:14or at least something that evaluates
to a number like "14 + 15." -
1:14 - 1:18On the other hand,
if I'd just typed "100 space", -
1:18 - 1:20well, I can't really put
a number after that -
1:20 - 1:24because "100-space-10"
doesn't mean anything. -
1:24 - 1:28So there's another type in programming,
called the Boolean type. -
1:28 - 1:29And it's called Boolean
-
1:29 - 1:34because some dude
named George Boole invented it. -
1:34 - 1:37And unlike a number
which has a ton of possible values, -
1:37 - 1:42a Boolean can only be
one of two values: true or false. -
1:43 - 1:45And you can see
when I type them they turn blue, -
1:45 - 1:47which means they're
super special awesome words. -
1:47 - 1:49And you've already seen one place
where we use booleans, -
1:49 - 1:52though you may not have realized it:
if statements! -
1:52 - 1:54Let's get a quick refresh
on how those work. -
1:54 - 1:59I'm just going to make a variable
called 'number, ' give it a number, 40. -
1:59 - 2:01And write an If statement that says,
-
2:01 - 2:09"If number is less than 50,
then I will draw this first ellipse." -
2:11 - 2:13I'm just going to copy this
into the If statement -
2:13 - 2:16and indent it by selecting everything
and pressing tab. -
2:16 - 2:18So now this statement says,
-
2:18 - 2:23"If number is less than 50," which it is,
"then we'll draw the top ellipse." -
2:23 - 2:25And if I make number greater than 50,
-
2:25 - 2:28you can see
that the top ellipse disappears. -
2:28 - 2:31Alright, so this thing
inside the parentheses -
2:31 - 2:33is actually a Boolean expression.
-
2:33 - 2:36Remember, a math expression
is anything that evaluates to a number: -
2:36 - 2:40like 3 plus 2 plus 4 times 8.
-
2:40 - 2:44So a Boolean expression is anything
that evaluates to a Boolean. -
2:44 - 2:46A good way to check
if an expression evaluates to a Boolean, -
2:46 - 2:50is to stick the word "is" in front of it,
and ask it like a question. -
2:50 - 2:54If it sounds like a yes or no question,
then you know it's a Boolean expression. -
2:54 - 2:57So here we can say,
"Is number less than 50?" -
2:57 - 3:01Yes, yes it is, and yes,
that is a Boolean expression. -
3:01 - 3:04On the other hand,
if I had something like, "4 + 4" -
3:04 - 3:10and I tried to ask, "is 4 + 4?"
No. not a Boolean. -
3:10 - 3:12So back to our If statement.
-
3:12 - 3:15I can actually put anything
inside these parentheses, -
3:15 - 3:17as long as it's a Boolean
or Boolean expression. -
3:17 - 3:21So I could say, "If true,"
and that ellipse would always be drawn. -
3:21 - 3:25Or I could say, "If false,"
and the ellipse would never be drawn. -
3:25 - 3:29I could also do something like
"If 3 is less than 4," -
3:29 - 3:32which is a Boolean expression
that will always evaluate to true, -
3:32 - 3:35which is kinda pointless,
the ellipse will always be drawn, -
3:35 - 3:38or "3 greater than 4,"
and that's always going to be false. -
3:38 - 3:41And I can also assign Booleans
to variables, like this: -
3:41 - 3:49so I'm going to make a new variable,
call it WinstonIsCool, and assign it -
3:49 - 3:52a Boolean value, so true or false.
-
3:52 - 3:54Say true because Winston
is definitely cool. -
3:54 - 3:57And now that this variable
has a Boolean value, -
3:57 - 4:00I can copy it and stick it
inside this If statement -
4:00 - 4:04and now you can see the ellipse is drawn,
-
4:04 - 4:07because the value
of WinstonIsCool is true. -
4:07 - 4:11I could also replace this
with a Boolean expression, -
4:11 - 4:14so could be "2 less than 4."
-
4:14 - 4:18Now if you're making a variable
that's meant for a Boolean value, -
4:18 - 4:20you should give it a name
-
4:20 - 4:22that describes the condition
when the variable is true. -
4:22 - 4:25A good way to check if you've picked
a good name for your variable -
4:25 - 4:28is to put it in an If statement
and see if it makes sense as a condition. -
4:28 - 4:31So, forget WinstonIsCool,
we already know that's true. -
4:31 - 4:34Let's say I had a variable
called "muffins." -
4:34 - 4:37All right, "If muffins." Hmm.
-
4:37 - 4:38Well, you know what?
-
4:38 - 4:42That doesn't tell me anything,
so that's a pretty bad variable name, -
4:42 - 4:46but if I had "If muffinsAreBaking",
then that would tell me -
4:46 - 4:51that when this variable is true,
then the muffins are baking. -
4:51 - 4:54And don't ask me what muffins,
it's not important. -
4:54 - 4:59So for now let's go back
to "If number is less than 50." -
4:59 - 5:00Cool.
-
5:00 - 5:03Now let's look
at some other Boolean expressions. -
5:03 - 5:06You've already seen
"less than" and "greater than", -
5:06 - 5:09but you can also check
if something is "less than or equal to." -
5:09 - 5:13So let's try, "If number is
less than or equal to 48." -
5:13 - 5:20And we could also say, "If number
is greater than or equal to 48." -
5:20 - 5:24If it is, we will draw
this top-right ellipse. -
5:24 - 5:27Indent that.
-
5:27 - 5:30And if you'd like to check if two things
are exactly equal to each other -
5:30 - 5:32or you could say: "If number"
-
5:32 - 5:36and then three equals signs,
or "triple equals 48." -
5:39 - 5:42So that's a lot more like the equals sign
you're used to in math, -
5:42 - 5:45except this time
you have three of them in a row. -
5:45 - 5:47It's kind of overkill, right?
-
5:47 - 5:49And then finally, we have
if you want to check -
5:49 - 5:51if two things are not equal to,
-
5:51 - 5:53so strictly not equal to, you can say,
-
5:53 - 5:58"If number" and then an exclamation point,
and then 2 equals signs, "48". -
5:58 - 6:02And then we will draw that last ellipse.
-
6:04 - 6:07So if we go back to the top,
we can see that number is 48, -
6:07 - 6:10so it is less than or equal to 48,
-
6:10 - 6:12which is why
the top-left ellipse is drawn. -
6:12 - 6:16It's also greater than or equal to 48,
it's also equal to 48, -
6:16 - 6:19but it is not not equal to 48,
-
6:19 - 6:22which is why we're missing
that bottom-right ellipse. -
6:22 - 6:24And if we play around with number
-
6:24 - 6:26you can see it changes
which ellipses are drawn. -
6:27 - 6:30So now you guys know about Booleans.
-
6:30 - 6:31And just like math expressions,
-
6:31 - 6:34Boolean expressions
can get really complicated. -
6:34 - 6:36But we will talk about those another time.
- Title:
- Booleans (Video Version)
- Description:
-
This is just a screen grab of our interactive coding talk-through, prepared to make captioning and translation easier. It is better to watch our talk-throughs here:
https://www.khanacademy.org/cs/programming/ - Video Language:
- English
- Duration:
- 06:37
Denise RQ edited English subtitles for Booleans (Video Version) | ||
Denise RQ edited English subtitles for Booleans (Video Version) | ||
Denise RQ edited English subtitles for Booleans (Video Version) | ||
Denise RQ edited English subtitles for Booleans (Video Version) | ||
Denise RQ edited English subtitles for Booleans (Video Version) | ||
Denise RQ edited English subtitles for Booleans (Video Version) | ||
Jennifer Heilemann edited English subtitles for Booleans (Video Version) |