< Return to Video

Booleans (Video Version)

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

more » « less
Video Language:
English
Duration:
06:37

English subtitles

Revisions