< Return to Video

if statements | Intro to computer science - Python | Khan Academy

  • 0:00 - 0:02
    - [Instructor] We can
    use Boolean expressions
  • 0:02 - 0:03
    to ask questions in our programs,
  • 0:03 - 0:07
    but how can we branch control
    flow based on the answer?
  • 0:07 - 0:10
    To do that, we need conditionals.
  • 0:10 - 0:12
    Conditionals form the basis of selection.
  • 0:12 - 0:14
    They allow the computer to decide
  • 0:14 - 0:17
    which code to run depending
    on whether the answer
  • 0:17 - 0:21
    to a specific question or
    condition is true or false.
  • 0:21 - 0:24
    The first piece of a
    conditional is the IF statement.
  • 0:24 - 0:27
    We start an IF statement
    with the keyword If.
  • 0:27 - 0:29
    Then we add our condition,
  • 0:29 - 0:30
    which can be any boolean expression.
  • 0:30 - 0:33
    We always end this line with a colon
  • 0:33 - 0:34
    because an IF statement
    comes in two parts.
  • 0:34 - 0:36
    This first part, which is the condition
  • 0:36 - 0:39
    and the second part, which
    is the instructions to run
  • 0:39 - 0:42
    if that condition evaluates to true.
  • 0:42 - 0:45
    In Python, we indicate those
    instructions using indentation.
  • 0:45 - 0:48
    Any lines of code immediately
    following that condition
  • 0:48 - 0:50
    that are indented one level in
  • 0:50 - 0:52
    are considered inside that IF statement,
  • 0:52 - 0:54
    if the condition evaluates to true,
  • 0:54 - 0:57
    the computer will go on to
    execute all of the instructions
  • 0:57 - 0:59
    indented inside the IF statement in order.
  • 0:59 - 1:01
    When it's done, it'll go on to execute
  • 1:01 - 1:03
    the rest of the program as normal.
  • 1:03 - 1:05
    If the condition evaluates to false,
  • 1:05 - 1:08
    the computer will skip
    all the lines of code
  • 1:08 - 1:09
    that are indented inside
    of that IF statement.
  • 1:09 - 1:12
    Instead, it'll jump directly
    to the next line of code
  • 1:12 - 1:13
    that's outside of the IF statement.
  • 1:13 - 1:15
    That means the first line of code
  • 1:15 - 1:16
    that is indented at the same level
  • 1:16 - 1:18
    as that initial if.
  • 1:18 - 1:20
    Let's try an example.
  • 1:20 - 1:22
    Here we have the Boolean expression,
  • 1:22 - 1:24
    num_orders equals equals
    10, which asks the question,
  • 1:24 - 1:26
    is this the customer's 10th order?
  • 1:26 - 1:29
    We can then use this Boolean
    value in our IF statement.
  • 1:29 - 1:31
    We'll indent one print function call
  • 1:31 - 1:34
    inside of the IF
    statement and one outside.
  • 1:34 - 1:38
    The one inside will only
    execute if it is the 10th order,
  • 1:38 - 1:40
    that is if the variable is 10th order
  • 1:40 - 1:42
    contains the Boolean value True.
  • 1:42 - 1:45
    We can also write this
    without the variable
  • 1:45 - 1:47
    where we just inline that
    whole Boolean expression
  • 1:47 - 1:48
    in the IF statement.
  • 1:48 - 1:50
    Let's trace how the
    computer executes this.
  • 1:50 - 1:51
    It starts with the first line
  • 1:51 - 1:54
    and assigns a value 10 to
    the variable num_orders.
  • 1:54 - 1:56
    Then it hits this IF statement,
  • 1:56 - 1:59
    so first it evaluates
    the Boolean expression.
  • 1:59 - 2:03
    10 is equal to 10, so
    this evaluates to true.
  • 2:03 - 2:04
    Because it's true,
  • 2:04 - 2:07
    the computer will
    execute the lines of code
  • 2:07 - 2:08
    indented inside of the IF statement.
  • 2:08 - 2:11
    First, it'll print, "Your order is free!"
  • 2:11 - 2:13
    And then it'll assign the value zero
  • 2:13 - 2:14
    to the variable num_orders.
  • 2:14 - 2:17
    Then it just keeps moving through
  • 2:17 - 2:18
    the rest of the program in order,
  • 2:18 - 2:21
    so it'll print the value of
    num_orders, which is now zero.
  • 2:21 - 2:23
    Now let's say instead num_orders
  • 2:23 - 2:24
    was originally set to three.
  • 2:24 - 2:25
    When the computer executes this program,
  • 2:25 - 2:27
    it'll assign the value
    three to num_orders,
  • 2:27 - 2:29
    evaluate the Boolean expression.
  • 2:29 - 2:32
    Three is not equal to
    10, so this is false,
  • 2:32 - 2:33
    and then because it's false,
  • 2:33 - 2:35
    we'll skip the lines of code indented
  • 2:35 - 2:37
    inside the IF statement.
  • 2:37 - 2:39
    It'll jump to the next
    line of code outside,
  • 2:39 - 2:41
    which is print num_orders,
    so it'll print three.
  • 2:41 - 2:44
    Note that if we didn't
    indent the line of code
  • 2:44 - 2:46
    num_orders equal zero,
    it would be considered
  • 2:46 - 2:49
    outside of the IF statement,
    so in the false case,
  • 2:49 - 2:52
    num_orders would be set to zero,
  • 2:52 - 2:53
    and then it would print zero.
  • 2:53 - 2:55
    In the true case, we would
    print, "Your order is free."
  • 2:55 - 2:57
    Then we would set num_orders to zero,
  • 2:57 - 2:59
    and then we would print zero.
  • 2:59 - 3:01
    You can write IF statements
    with any Boolean expressions
  • 3:01 - 3:03
    as conditions, and you can put any
  • 3:03 - 3:06
    and as many instructions
    as you want inside of them.
  • 3:06 - 3:08
    With this conditional, I could make sure
  • 3:08 - 3:10
    to only ask the follow-up question,
  • 3:10 - 3:12
    chicken or tofu, if the
    user ordered pad Thai,
  • 3:12 - 3:13
    because if they order the papaya salad,
  • 3:13 - 3:15
    that question doesn't make sense.
  • 3:15 - 3:19
    Note that in most IDEs we
    indent using the tab key.
  • 3:19 - 3:20
    Our standard lines of code
  • 3:21 - 3:22
    should line up directly
    with the left margin.
  • 3:22 - 3:25
    That is, there should be no
    spaces or indents before them.
  • 3:25 - 3:27
    Lines of code indented
    inside of an IF statement
  • 3:27 - 3:29
    should be one tab key over.
  • 3:29 - 3:32
    Getting the indentation right is crucial
  • 3:32 - 3:33
    because this is the only
    way we can tell the computer
  • 3:33 - 3:35
    which lines of code are
    inside the IF statement
  • 3:35 - 3:37
    and which are outside.
  • 3:37 - 3:38
    We need to be careful here though
  • 3:38 - 3:40
    because now our control flow branches.
  • 3:40 - 3:44
    If the user orders pad Thai,
    this program works fine,
  • 3:44 - 3:46
    but if the user orders something
    else, we get a name error.
  • 3:46 - 3:48
    The variable protein is only defined
  • 3:48 - 3:50
    inside the IF statement.
  • 3:50 - 3:51
    If the condition is false,
  • 3:51 - 3:53
    this assignment statement doesn't execute,
  • 3:53 - 3:56
    so the computer doesn't
    know a variable protein.
  • 3:56 - 3:58
    To fix this, we either need to make sure
  • 3:58 - 4:00
    we're only accessing the variable protein
  • 4:00 - 4:03
    inside the IF statement where
    we're guaranteed it exists,
  • 4:03 - 4:05
    or we need to initialize
    the variable protein
  • 4:05 - 4:08
    before the IF statement,
  • 4:08 - 4:09
    which guarantees that it's always defined.
  • 4:09 - 4:11
    It's common in situations like this
  • 4:11 - 4:12
    to initialize the variable
  • 4:12 - 4:14
    to an empty or placeholder value
  • 4:14 - 4:16
    like the empty string or no protein.
  • 4:16 - 4:19
    Now there's no error on
    either possible path.
  • 4:19 - 4:22
    If the order is equal to
    pad Thai or if it's not.
  • 4:22 - 4:25
    Now that our control flow
    branches, when we run a program,
  • 4:25 - 4:27
    we're only testing one
  • 4:27 - 4:29
    of perhaps many possible passive
    execution through the code.
  • 4:29 - 4:30
    Just because it works for one case
  • 4:30 - 4:32
    doesn't mean it works for the other,
  • 4:32 - 4:34
    and it's up to us to test for that.
  • 4:34 - 4:36
    Otherwise, it'll be our users who suffer
  • 4:36 - 4:38
    when they're the first
    ones to find the bug.
Title:
if statements | Intro to computer science - Python | Khan Academy
Description:

more » « less
Video Language:
English
Team:
Khan Academy
Duration:
04:38

English subtitles

Incomplete

Revisions