< Return to Video

elif and else branches | Intro to computer science - Python | Khan Academy

  • 0:00 - 0:02
    - [Instructor] We can use
    an IF statement to control
  • 0:02 - 0:05
    that a particular block
    of code only executes
  • 0:05 - 0:07
    when the condition evaluates to true.
  • 0:07 - 0:09
    But what if we wanna do something else
  • 0:09 - 0:11
    only when the condition
    evaluates to false?
  • 0:11 - 0:14
    Well, we can add another
    IF statement and try
  • 0:14 - 0:16
    and construct a condition
    that's the exact opposite
  • 0:16 - 0:18
    of the original condition.
  • 0:18 - 0:19
    That's a bit annoying
  • 0:19 - 0:22
    and sometimes the opposite
    condition isn't obvious
  • 0:22 - 0:23
    or it's super long.
  • 0:23 - 0:24
    To save us the trouble,
  • 0:24 - 0:27
    Python instead lets us use an else branch.
  • 0:27 - 0:30
    We can stick an else branch
    onto the end of any IF statement
  • 0:30 - 0:33
    and any instructions indented
    inside the else branch
  • 0:33 - 0:36
    only execute if the corresponding
    IF statements condition
  • 0:36 - 0:38
    evaluates to false.
  • 0:38 - 0:39
    That makes the IF branch
  • 0:39 - 0:43
    and the else branch mutually
    exclusive based on the answer
  • 0:43 - 0:43
    to the condition,
  • 0:43 - 0:47
    the computer decides which
    of the two paths to take.
  • 0:47 - 0:49
    The Python syntax for an else branch
  • 0:49 - 0:50
    is just the keyword else
  • 0:50 - 0:52
    followed by a colon.
  • 0:52 - 0:54
    Again, we use indentation
    to tell the computer
  • 0:54 - 0:57
    which lines of code are
    inside the else branch.
  • 0:57 - 0:59
    An else branch must always
    follow an IF branch.
  • 0:59 - 1:01
    Otherwise, what are we
    taking the opposite of?
  • 1:01 - 1:03
    Let's trace that execution path.
  • 1:03 - 1:06
    If the condition evaluates
    to true as normal,
  • 1:06 - 1:08
    the computer goes on to
    execute any instructions
  • 1:08 - 1:10
    indented inside that IF branch.
  • 1:10 - 1:13
    When it's done, execution
    jumps to the next line of code
  • 1:13 - 1:16
    that's indented outside
    of the conditional.
  • 1:16 - 1:18
    If the condition evaluates to false,
  • 1:18 - 1:20
    the computer skips the
    rest of the IF branch
  • 1:20 - 1:21
    and jumps to the else branch.
  • 1:21 - 1:24
    Then it executes any lines
    of code that are indented
  • 1:24 - 1:26
    inside the else branch.
  • 1:26 - 1:28
    When it's done, it just
    continues execution
  • 1:28 - 1:30
    with the next line of code indented
  • 1:30 - 1:32
    outside of the conditional.
  • 1:32 - 1:34
    Note that this execution path
  • 1:34 - 1:36
    with an else branch is
    different from this,
  • 1:36 - 1:38
    where we just have that
    instruction indented outside
  • 1:38 - 1:39
    of the IF statement.
  • 1:39 - 1:41
    Here if the condition evaluates to true,
  • 1:41 - 1:43
    we print mobile layout,
  • 1:43 - 1:44
    then we jump outside of the conditional
  • 1:44 - 1:46
    and print desktop layout.
  • 1:46 - 1:48
    If the condition evaluates to false,
  • 1:48 - 1:50
    we skip the IF branch, jump
    outside of the conditional
  • 1:50 - 1:52
    and print desktop layout.
  • 1:52 - 1:54
    Because the instruction is not indented
  • 1:54 - 1:58
    it's independent of the
    conditional`, it always executes.
  • 1:58 - 2:00
    However, if we indent this instruction
  • 2:00 - 2:02
    inside an else branch instead,
  • 2:02 - 2:04
    we only print desktop
    layout if the condition
  • 2:04 - 2:06
    evaluates to false.
  • 2:06 - 2:08
    If the condition evaluates to true,
  • 2:08 - 2:11
    we print mobile layout
    and skip the else branch.
  • 2:12 - 2:14
    What if we have more than two cases
  • 2:14 - 2:16
    like a mobile tablet and a desktop layout?
  • 2:16 - 2:18
    We could try to construct three
  • 2:18 - 2:21
    mutually exclusive conditions
    or we can take advantage
  • 2:21 - 2:23
    of a shortcut and use the elif branch.
  • 2:23 - 2:25
    The elif or else IF branch
  • 2:25 - 2:29
    allows us to chain multiple
    conditions together.
  • 2:29 - 2:30
    Starting with the IF branch,
  • 2:30 - 2:33
    the computer evaluates
    each condition in order
  • 2:33 - 2:35
    until it finds one that evaluates to true,
  • 2:35 - 2:38
    then it chooses that branch to execute.
  • 2:38 - 2:39
    Note that order matters here
  • 2:39 - 2:42
    because the computer will
    stop checking other branches
  • 2:42 - 2:45
    as soon as it finds one
    that evaluates to true.
  • 2:45 - 2:47
    If instead I put this condition first,
  • 2:47 - 2:49
    the IF branch would
    capture any screen widths
  • 2:49 - 2:51
    that are smaller than 760.
  • 2:51 - 2:53
    That means that if I have a mobile screen
  • 2:53 - 2:56
    that's say 300 pixels,
    it will get captured
  • 2:56 - 2:59
    by this first case and
    print tablet layout.
  • 2:59 - 3:01
    The computer only ever chooses one branch.
  • 3:01 - 3:03
    It doesn't print mobile layout.
  • 3:03 - 3:06
    Even though that condition
    would have evaluated to true.
  • 3:06 - 3:08
    We can attach as many
    elif branches as we want
  • 3:08 - 3:11
    to any if branch and
    then we can optionally
  • 3:11 - 3:12
    add an else branch at the end.
  • 3:13 - 3:15
    So we can see what the computer's doing
  • 3:15 - 3:17
    let's trace each possible execution path.
  • 3:17 - 3:19
    If the first condition evaluates to true,
  • 3:19 - 3:21
    then the computer chooses that branch
  • 3:21 - 3:24
    and it executes any instructions
    indented inside of it.
  • 3:24 - 3:25
    Then it jumps to the next line of code
  • 3:25 - 3:27
    outside of the conditional.
  • 3:27 - 3:29
    If the first condition evaluates to false,
  • 3:29 - 3:32
    then the computer goes on
    to check the next condition.
  • 3:32 - 3:34
    If the second condition evaluates to true,
  • 3:34 - 3:36
    then the computer chooses this branch
  • 3:36 - 3:39
    and executes any instructions
    indented inside of it.
  • 3:39 - 3:40
    Then it jumps to the next line of code
  • 3:40 - 3:42
    outside of the conditional.
  • 3:42 - 3:44
    If the computer checks
    the first condition,
  • 3:44 - 3:46
    finds that it evaluates to false,
  • 3:46 - 3:47
    then checks the second condition,
  • 3:47 - 3:49
    also finds that it evaluates to false,
  • 3:49 - 3:51
    then it moves on to the else branch else.
  • 3:51 - 3:53
    Else branches don't have a condition,
  • 3:53 - 3:55
    so if the computer
    reaches it, it just runs.
  • 3:55 - 3:57
    So we execute the instructions indented
  • 3:57 - 3:58
    inside the else branch
  • 3:58 - 4:00
    and then we jump to the next line of code
  • 4:00 - 4:02
    outside of the conditional.
  • 4:02 - 4:05
    So if you have multiple related
    conditions in your program,
  • 4:05 - 4:08
    it's generally better to use
    a chain conditional with elif
  • 4:08 - 4:10
    and else branches instead
    of several independent
  • 4:10 - 4:12
    single branch conditionals.
  • 4:12 - 4:15
    Chain conditionals make
    programs easier to read
  • 4:15 - 4:16
    because it makes the relationship
  • 4:16 - 4:18
    between conditions obvious.
  • 4:18 - 4:20
    It reduces bugs because
    we as the programmer
  • 4:20 - 4:23
    don't have to worry about
    making sure all our conditions
  • 4:23 - 4:27
    are mutually exclusive and
    cover all possible cases
  • 4:27 - 4:29
    and it saves the computer some work.
  • 4:29 - 4:30
    With independent conditions
  • 4:30 - 4:31
    that computer doesn't
    know they're related,
  • 4:31 - 4:34
    so it'll evaluate every single
    one, even if it's impossible
  • 4:34 - 4:36
    for multiple of them to be true.
  • 4:36 - 4:37
    With chain conditionals,
  • 4:37 - 4:39
    we give the computer the hint
  • 4:39 - 4:40
    that these are all mutually exclusive,
  • 4:40 - 4:42
    so it knows it can stop evaluating
  • 4:42 - 4:45
    as soon as it finds a branch
    that evaluates to true.
Title:
elif and else branches | Intro to computer science - Python | Khan Academy
Description:

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

English subtitles

Revisions