< Return to Video

CST-170 Chapter 4 - Making Decisions - Part 1

  • 0:00 - 0:04
    Chapter 4 discusses making decisions.
  • 0:04 - 0:06
    before we get into the two types of
  • 0:06 - 0:08
    decision structures or selection
  • 0:08 - 0:11
    structures, we need to understand what
  • 0:11 - 0:13
    a boolean expression is. A boolean
  • 0:13 - 0:16
    expression is a comparison. The results
  • 0:16 - 0:18
    of the boolean expression is going to be
  • 0:18 - 0:22
    either a TRUE or FALSE. Either the
  • 0:22 - 0:25
    expression is TRUE or the expression is
  • 0:25 - 0:28
    FALSE. These boolean expressions are used
  • 0:28 - 0:30
    in every selection structure.
  • 0:30 - 0:32
    The question that is being asked in
  • 0:32 - 0:35
    the diamond symbol of your flow chart,
  • 0:35 - 0:38
    that question is really a boolean
  • 0:38 - 0:40
    expression. Let's look at the first type
  • 0:40 - 0:43
    of selection structure. It's called a
  • 0:43 - 0:46
    dual alternative or binary, 'bi',
  • 0:46 - 0:48
    being two, because it provides an action
  • 0:48 - 0:51
    for each of the two outcomes.
  • 0:51 - 0:53
    If the expression, remember the expression
  • 0:53 - 0:55
    will be written inside the diamond of
  • 0:55 - 0:57
    your flow chart, if the expression
  • 0:57 - 1:00
    results to TRUE or YES, we follow this
  • 1:00 - 1:04
    plan or path of action. The alternative
  • 1:04 - 1:07
    is if the expression evaluates to FALSE
  • 1:07 - 1:12
    or NO then you follow a different plan
  • 1:12 - 1:15
    of action. In pseudo code a dual
  • 1:15 - 1:18
    alternative selection is often written as
  • 1:18 - 1:23
    an IF THEN ELSE statement.
  • 1:23 - 1:26
    The second type of selection structure
  • 1:26 - 1:28
    is the single alternative or unary
  • 1:28 - 1:31
    selection, single being one action or
  • 1:31 - 1:33
    path to follow. If the expression
  • 1:33 - 1:36
    evaluates to TRUE, you follow the path
  • 1:36 - 1:39
    and perform some sort of action. If the
  • 1:39 - 1:42
    expression evaluates to false then there
  • 1:42 - 1:44
    is no special action to take; we simply
  • 1:44 - 1:48
    exit the structure. You could write your
  • 1:48 - 1:53
    expression in such a manner that the plan
  • 1:53 - 1:56
    of action is executed from the FALSE
  • 1:56 - 1:59
    results but that's what we call negative
  • 1:59 - 2:01
    logic and we are going to discuss avoiding
  • 2:01 - 2:06
    that. So the action does not always have
  • 2:06 - 2:09
    to happen if the expression is TRUE but
  • 2:09 - 2:11
    you do want to be careful about how
  • 2:11 - 2:14
    you write your expressions. You want to
  • 2:14 - 2:17
    make sure your logic is clear and
  • 2:17 - 2:21
    understandable so sometimes that negative
  • 2:21 - 2:24
    logic can confuse people. The single
  • 2:24 - 2:27
    alternative selection when written in
  • 2:27 - 2:30
    pseudo code is often written as an IF THEN
  • 2:30 - 2:34
    statement. If the expression is TRUE
  • 2:34 - 2:38
    then do this. That's it. There's no else.
  • 2:38 - 2:40
    The IF THEN ELSE decision, remember this
  • 2:40 - 2:44
    is the dual alternative selection, the IF
  • 2:44 - 2:47
    line, if the condition is TRUE then the
  • 2:47 - 2:50
    THEN clause. The THEN clause holds the
  • 2:50 - 2:53
    action or actions that execute when the
  • 2:53 - 2:56
    tested condition in the decision is TRUE.
  • 2:56 - 2:58
    If your boolean expression evaluates to
  • 2:58 - 3:01
    TRUE you do the actions within the THEN
  • 3:01 - 3:03
    clause. If the boolean expression
  • 3:03 - 3:07
    evaluates to FALSE the THEN clause is
  • 3:07 - 3:10
    skipped and the actions in the ELSE
  • 3:10 - 3:13
    clause are executed.
  • 3:13 - 3:16
    Your boolean expression does not have to
  • 3:16 - 3:19
    be an 'equal' symbol comparison. You
  • 3:19 - 3:20
    don't have to write your boolean
  • 3:20 - 3:25
    expression as 'value1 = value2' and
  • 3:25 - 3:27
    remember we want to make sure that we
  • 3:27 - 3:29
    are talking about comparisons now, we
  • 3:29 - 3:32
    quite often use the '=' sign to do
  • 3:32 - 3:35
    comparisons. Don't confuse that with
  • 3:35 - 3:38
    chapter 2 when we discussed assignment
  • 3:38 - 3:40
    statements and the assignment operator
  • 3:40 - 3:42
    was also the '=' symbol.
  • 3:42 - 3:44
    These are comparisons; however, we are
  • 3:44 - 3:47
    not going to limit ourselves to just
  • 3:47 - 3:50
    'equal to' type comparisons. Most
  • 3:50 - 3:53
    programming languages support six
  • 3:53 - 3:56
    different types of comparison operators.
  • 3:56 - 3:58
    All of these comparison operators will
  • 3:58 - 4:01
    result in a TRUE or FALSE. You compare
  • 4:01 - 4:04
    the two values by using either variables
  • 4:04 - 4:06
    or constants or a combination of a
  • 4:06 - 4:08
    variable and a constant. The term
  • 4:08 - 4:10
    'trivial expression' is when you compare
  • 4:10 - 4:13
    two constants. In this example
  • 4:13 - 4:17
    the digit 20, 20 equals 20 that's always
  • 4:17 - 4:19
    going to be TRUE. It's never going to
  • 4:19 - 4:21
    change. So quite often in our
  • 4:21 - 4:24
    expressions we use variables because the
  • 4:24 - 4:27
    data in the variable changes. It's not
  • 4:27 - 4:32
    uncommon to compare a variable value to a
  • 4:32 - 4:35
    constant. It's not uncommon to compare
  • 4:35 - 4:38
    a variable to a variable and when I say
  • 4:38 - 4:41
    that I mean the data in one variable
  • 4:41 - 4:43
    compared to the data in another variable.
  • 4:43 - 4:47
    But it is very trivial to compare a
  • 4:47 - 4:49
    constant to a constant because a constant
  • 4:49 - 4:51
    never changes. Avoid comparing a
  • 4:51 - 4:55
    constant to a constant.
  • 4:55 - 4:59
    Here are the six relational operators
  • 4:59 - 5:01
    that our commonly accepted.
  • 5:01 - 5:04
    the equivalency operator or the equal to,
  • 5:04 - 5:08
    greater-than, less-than, greater-than or
  • 5:08 - 5:09
    equal-to, less-than or equal-to, and then
  • 5:09 - 5:12
    not-equal-to. Now once again I'm going
  • 5:12 - 5:16
    to say that depending on your language
  • 5:16 - 5:19
    when you actually write the code these
  • 5:19 - 5:22
    symbols may be slightly different
  • 5:22 - 5:27
    depending on the language.
  • 5:28 - 5:31
    Any logical situation can be expressed
  • 5:31 - 5:33
    with only the three types of comparisons,
  • 5:33 - 5:36
    equal-to, greater-than, less-than. So
  • 5:36 - 5:38
    technically we could argue that you could
  • 5:38 - 5:43
    throw out the greater-than equal-to and
  • 5:43 - 5:46
    the lesser-than equal-to, but that's kind
  • 5:46 - 5:48
    of a debatable discussion. Now here it
  • 5:48 - 5:50
    says it makes the code more readable.
  • 5:50 - 5:53
    I would argue that really depends on the
  • 5:53 - 5:56
    individual. If you want to say
  • 5:56 - 6:01
    Ms. Veral's video on boolean expressions,
  • 6:01 - 6:03
    she talks about asking the computer her
  • 6:03 - 6:06
    birth date. Is your birthday on or before
  • 6:06 - 6:08
    July 1st? Well if you want to be
  • 6:08 - 6:12
    inclusive, including July 1st, you could
  • 6:12 - 6:14
    just ask if it were before July 2nd.
  • 6:14 - 6:16
    That would include July 1st but not
  • 6:16 - 6:19
    include July 2nd. It's arguable whether
  • 6:19 - 6:21
    or not the less-than or equal-to, greater-
  • 6:21 - 6:23
    than or equal-to makes your code more
  • 6:23 - 6:26
    readable. Just know that we always have
  • 6:26 - 6:28
    the option that there's a work around
  • 6:28 - 6:31
    for these greater-than or equal-to,
  • 6:31 - 6:33
    less-than or equal-to.
  • 6:33 - 6:38
    And a not-equal operator, again, debatable
  • 6:38 - 6:39
    but I'm going to agree, probably the most
  • 6:39 - 6:41
    confusing of comparisons.
  • 6:41 - 6:44
    But the not-equal-to is asking if two
  • 6:44 - 6:49
    variables are not equal.
  • 6:49 - 6:52
    Here's some example flow chart and
  • 6:52 - 6:54
    example pseudo code using a negative
  • 6:54 - 6:57
    comparison. Is the customer code not
  • 6:57 - 7:00
    equal to 1. If the expression evaluates
  • 7:00 - 7:03
    to TRUE or YES, the customer code is not
  • 7:03 - 7:07
    1 and the discount is 0.25. If the
  • 7:07 - 7:10
    customer code not-equal to 1 evaluates to
  • 7:10 - 7:13
    FALSE, which think carefully that means
  • 7:13 - 7:16
    the customer code is equal to 1, then
  • 7:16 - 7:20
    the discount is 0.50. Here's that pseudo
  • 7:20 - 7:23
    code to match it. Customer code not
  • 7:23 - 7:26
    equal to 1 then discount is 0.25
  • 7:26 - 7:29
    otherwise discount 0.50. You can simply
  • 7:29 - 7:33
    rewrite this to be using positive logic
  • 7:33 - 7:35
    rather than negative logic. Instead of
  • 7:35 - 7:38
    saying not-equal-to. I want you to know
  • 7:38 - 7:40
    this before we change slides. Not-equal
  • 7:40 - 7:44
    to one, discount 0.25. Equal-to 1
  • 7:44 - 7:49
    discount 0.5. Notice that we're going to
  • 7:49 - 7:52
    keep the negative or the FALSE path on the
  • 7:52 - 7:57
    left and the TRUE path on the right.
  • 7:57 - 7:59
    But notice the question will change and
  • 7:59 - 8:03
    the actions will change.
  • 8:03 - 8:06
    Customer code is-equal-to 1, YES
  • 8:06 - 8:09
    discount 0.50. Customer code not-equal-to
  • 8:09 - 8:13
    1, discount 0.25. Look at the pseudo
  • 8:13 - 8:16
    code. Now I'm going to back up a slide.
  • 8:16 - 8:19
    Compare this pseudo code using straight
  • 8:19 - 8:23
    forward or positive logic, compared to
  • 8:23 - 8:27
    the same logic, excuse me, the same
  • 8:27 - 8:30
    actions but in negative logic. I would
  • 8:30 - 8:34
    argue that using the not-equal-to causes
  • 8:34 - 8:37
    a little bit more confusion. You are
  • 8:37 - 8:40
    certainly welcome to disagree.
  • 8:40 - 8:42
    Avoiding a common error with relational
  • 8:42 - 8:46
    operators, be very careful, read your code
  • 8:46 - 8:50
    do a desk check. I'm sorry, not your code.
  • 8:50 - 8:52
    Read your logic, walk through your pseudo
  • 8:52 - 8:54
    code, walk through your flow chart doing
  • 8:54 - 8:56
    a desk-check. Make sure you're using the
  • 8:56 - 8:59
    correct operators. A very common error
  • 8:59 - 9:01
    is missing that boundry or limit for a
  • 9:01 - 9:03
    selection. Again if we refer to the
  • 9:03 - 9:06
    birth date July 1st, you ask is your
  • 9:06 - 9:10
    birthday before July 1st. Well do you
  • 9:10 - 9:12
    want to include July 1st in your question?
  • 9:12 - 9:14
    That's where you've gotta ask. That's that
  • 9:14 - 9:17
    boundary or limit their talking about.
  • 9:17 - 9:19
    If I ask you to pick a number between 1
  • 9:19 - 9:22
    and 5. Between 1 and 5, does that mean
  • 9:22 - 9:26
    2, 3, or 4? Or are we being inclusive,
  • 9:26 - 9:29
    1, 2, 3, 4 and 5 pick one of those.
  • 9:29 - 9:30
    That's what we are talking about with the
  • 9:30 - 9:33
    boundary or the limit. So be careful
  • 9:33 - 9:35
    that you use the correct relational
  • 9:35 - 9:40
    operator in your boolean expressions.
Title:
CST-170 Chapter 4 - Making Decisions - Part 1
Description:

more » « less
Video Language:
English
Duration:
09:39

English subtitles

Revisions