< Return to Video

CST-170 Chapter 3 - Understanding Structure - Part 1

  • 0:00 - 0:03
    Chapter 3 covers understanding structure
  • 0:03 - 0:05
    Unstructured programming is often
  • 0:05 - 0:08
    referred to as spaghetti code.
  • 0:08 - 0:12
    Now spaghetti code might be code that can
  • 0:12 - 0:14
    be written into a working program but
  • 0:14 - 0:16
    it's often difficult to read and maintain
  • 0:16 - 0:18
    And this Spaghetti code or ustructured
  • 0:18 - 0:21
    programs are considered such because they
  • 0:21 - 0:24
    don't follow a few basic rules regarding
  • 0:24 - 0:26
    structured logic.
  • 0:26 - 0:27
    First let's talk about the three basic
  • 0:27 - 0:31
    structures. The sequence structure which
  • 0:31 - 0:33
    is performing actions in order, there's no
  • 0:33 - 0:35
    branching or skipping. It's perform one
  • 0:35 - 0:37
    task then perform the next task and then
  • 0:37 - 0:40
    the next and so on to any number of tasks
  • 0:40 - 0:43
    in a sequence. The second structure is
  • 0:43 - 0:46
    the selection or decision. We say ask a
  • 0:46 - 0:48
    question and then take an action based
  • 0:48 - 0:50
    on the answer to the question.
  • 0:50 - 0:52
    The question is going to be in the form
  • 0:52 - 0:56
    of some sort of boolean expression. Does
  • 0:56 - 0:58
    the answer to the question result in a
  • 0:58 - 1:00
    TRUE or FALSE, yes or no.
  • 1:00 - 1:01
    We are going to talk about dual
  • 1:01 - 1:04
    alternative and single alternative IF
  • 1:04 - 1:06
    selections. The third structure is called
  • 1:06 - 1:10
    the loop. A loop repeats an action or a
  • 1:10 - 1:12
    series of actions again based on the
  • 1:12 - 1:15
    answer to a question.
  • 1:15 - 1:20
    Here is the basic flow chart symbol of a
  • 1:20 - 1:22
    sequence structure. We're going to talk
  • 1:22 - 1:25
    about entry points and exit points that
  • 1:25 - 1:29
    structures have one entry here into
  • 1:29 - 1:33
    the structure and one exit here.
  • 1:33 - 1:36
    A selection structure, again one entry
  • 1:36 - 1:39
    point, one exit point. The question is
  • 1:39 - 1:43
    asked, represented by the decision symbol,
  • 1:43 - 1:46
    and depending on if the expression
  • 1:46 - 1:49
    evaluates to TRUE the flow of the program
  • 1:49 - 1:51
    takes one route if the expression
  • 1:51 - 1:54
    evaluates to FALSE the program logic
  • 1:54 - 1:57
    takes another route but notice regardless
  • 1:57 - 2:01
    of which route there is one exit point
  • 2:01 - 2:03
    for the structure.
  • 2:03 - 2:06
    Here's a pseudo code example of a dual
  • 2:06 - 2:09
    alternative IF again what we saw in the
  • 2:09 - 2:11
    previous slide here dual alternative IF
  • 2:11 - 2:13
    depending on the question you take either
  • 2:13 - 2:15
    this route or the dual route, the
  • 2:15 - 2:18
    alternate route. True versus false.
  • 2:18 - 2:20
    Dual alternative IF contains two
  • 2:20 - 2:23
    alternatives. If some condition is TRUE
  • 2:23 - 2:26
    then do one thing, else do the other.
  • 2:26 - 2:28
    So dual alternative, you have this
  • 2:28 - 2:33
    alternative or this alternative.
  • 2:33 - 2:35
    Another example of a decision structure
  • 2:35 - 2:37
    is the single alternative IF, in other
  • 2:37 - 2:40
    words if the condition evaluates to TRUE
  • 2:40 - 2:44
    we do some action otherwise we do nothing.
  • 2:44 - 2:46
    In this example if employee belongs to
  • 2:46 - 2:49
    dentalPlan then deduct $40 from
  • 2:49 - 2:52
    employeeGrossPay. There is no else clause
  • 2:52 - 2:55
    because there is not alternative, there
  • 2:55 - 2:57
    isn't that second alternative. If it's
  • 2:57 - 3:02
    TRUE deduct $40, if not exit the structure.
  • 3:02 - 3:05
    Here's the flow chart diagram of a single
  • 3:05 - 3:07
    alternative if. Again the question is
  • 3:07 - 3:10
    asked in the diamond, if the question or
  • 3:10 - 3:13
    expression evaluates to TRUE we follow
  • 3:13 - 3:15
    this route. If the expression is not TRUE
  • 3:15 - 3:18
    we follow this route. Again notice one
  • 3:18 - 3:22
    single entry, one single exit point.
  • 3:22 - 3:25
    The loop structure, again loops repeat a
  • 3:25 - 3:27
    set of actions based on the answer to that
  • 3:27 - 3:29
    boolean expression or to that boolean
  • 3:29 - 3:33
    question. The actions that are repeated
  • 3:33 - 3:36
    are referred to as the loop body. Again
  • 3:36 - 3:38
    a loop is sometimes called a repetition or
  • 3:38 - 3:41
    an iteration. Most of the time it's most
  • 3:41 - 3:44
    common to ask the question first, then
  • 3:44 - 3:47
    depending on the answer go into the loop
  • 3:47 - 3:49
    but there is such a thing called a
  • 3:49 - 3:52
    post-test loop. A pre-test loop we ask
  • 3:52 - 3:54
    the question first, if the evaluates to
  • 3:54 - 3:56
    TRUE, let's do what the body of the loop
  • 3:56 - 4:00
    says. The post-test loop would do the
  • 4:00 - 4:03
    body of the loop then ask the question
  • 4:03 - 4:05
    and depending on the answer decide whether
  • 4:05 - 4:09
    or not to repeat or reiterate the body of
  • 4:09 - 4:12
    the loop. And here is the flow chart
  • 4:12 - 4:13
    diagram of this.
  • 4:13 - 4:16
    The entry point is here the exit
  • 4:16 - 4:18
    point is here.
  • 4:18 - 4:20
    And then we come into here and we ask the
  • 4:20 - 4:26
    question. If the data evaluates to TRUE
  • 4:26 - 4:28
    based on the question then we do the
  • 4:28 - 4:32
    body of the loop. We come back here and
  • 4:32 - 4:34
    determine whether or not we go into the
  • 4:34 - 4:36
    loop again. Now we are going to learn
  • 4:36 - 4:39
    that the data that is being evaluated
  • 4:39 - 4:42
    that data that was originally picked up
  • 4:42 - 4:45
    here, we call it the sentinel value we
  • 4:45 - 4:47
    have to change that sentinel value
  • 4:47 - 4:49
    somewhere in the body of the loop. We
  • 4:49 - 4:52
    did talk about this in class so I'll
  • 4:52 - 4:53
    apologize for the confusion that I may
  • 4:53 - 4:55
    have caused here. But again notice that
  • 4:55 - 4:59
    there's one entry point, one exit point.
  • 4:59 - 5:01
    Eventually when you get to the point
  • 5:01 - 5:04
    where the expression evaluates to FALSE
  • 5:04 - 5:11
    you exit the structure.
  • 5:11 - 5:14
    Here's some pseudo code of a loop
  • 5:14 - 5:16
    structure. Again just generically while
  • 5:16 - 5:19
    the test condition continues to be TRUE
  • 5:19 - 5:23
    do something and an example.
  • 5:23 - 5:25
    All logic problems can be solved using
  • 5:25 - 5:28
    these three structures. These three
  • 5:28 - 5:31
    structures can be combined infinitely
  • 5:31 - 5:33
    in an infinite number of ways. Stacking
  • 5:33 - 5:35
    the structures, in other words having a
  • 5:35 - 5:37
    sequence immediately followed by a loop
  • 5:37 - 5:40
    immediately followed by a decision or any
  • 5:40 - 5:43
    variation from there. The decision
  • 5:43 - 5:46
    structures and the loop structures require
  • 5:46 - 5:48
    some sort of end-structure statement to
  • 5:48 - 5:50
    tell us we are done with this structure.
  • 5:50 - 5:52
    Typically that is an endif statement for
  • 5:52 - 5:55
    the decision and an end while statement
  • 5:55 - 5:57
    for the loop. Now again in pseudo code it
  • 5:57 - 6:00
    doesn't have to be this exact word endif
  • 6:00 - 6:02
    but it will be something like that or
  • 6:02 - 6:04
    this here. When you actually write the
  • 6:04 - 6:07
    program code these words would be
  • 6:07 - 6:13
    language specific. Here's an example of
  • 6:13 - 6:15
    stacking the three structures. You have
  • 6:15 - 6:19
    a sequence here. Notice that the sequence
  • 6:19 - 6:24
    one entry point one exit point. The exit
  • 6:24 - 6:26
    point of the sequence is the entry point
  • 6:26 - 6:30
    to the selection, which is here. Again
  • 6:30 - 6:32
    the selection has the one entry point the
  • 6:32 - 6:35
    one exit point. The selection's exit point
  • 6:35 - 6:38
    also serves as the entry point for the
  • 6:38 - 6:42
    loop which is here. So that is stacking.
  • 6:42 - 6:45
    Notice in the pseudo code we have step A
  • 6:45 - 6:47
    step B, that's your sequence. Immediately
  • 6:47 - 6:52
    followed by the decision if end if.
  • 6:52 - 6:56
    Notice the indentation. And then the loop
  • 6:56 - 7:00
    the while and the end while to end
  • 7:00 - 7:02
    the loop.
  • 7:02 - 7:03
    Not only can you stack the three
  • 7:03 - 7:06
    structures you can also nest them.
  • 7:06 - 7:10
    Nesting is placing one structure inside
  • 7:10 - 7:12
    another structure. In the pseudo code
  • 7:12 - 7:15
    again we would indent the nested structure
  • 7:15 - 7:17
    statement. This term block, a block of
  • 7:17 - 7:21
    code or a block of logic is a group of
  • 7:21 - 7:23
    statements that execute as a single unit.
  • 7:23 - 7:25
    And again that is very typical of what you
  • 7:25 - 7:27
    might find in the body of the loop.
  • 7:27 - 7:28
    Quite often you are going to find more
  • 7:28 - 7:31
    than one statement or action within the
  • 7:31 - 7:33
    body of a loop. But that can pertain to
  • 7:33 - 7:38
    decisions and structures as well.
  • 7:38 - 7:42
    Now here's an example of a sequence, steps
  • 7:42 - 7:46
    J, K, and L is a sequence which is nested
  • 7:46 - 7:50
    inside a decision. Notice the pseudo code
  • 7:50 - 7:53
    the decision begins with the if statement
  • 7:53 - 7:56
    and ends with the endif. The sequence is
  • 7:56 - 8:01
    indented inside the decision. Here's an
  • 8:01 - 8:06
    example of a loop this is a loop which is
  • 8:06 - 8:10
    nested inside the sequence, here's the
  • 8:10 - 8:14
    seqence, and the sequence is nested inside
  • 8:14 - 8:17
    a decision. Again, notice the pseudo code
  • 8:17 - 8:19
    notice the indentation.
  • 8:19 - 8:22
    Again these types of combinations can be
  • 8:22 - 8:27
    infinite. Another example.
  • 8:27 - 8:29
    Structured programs have the following
  • 8:29 - 8:30
    characteristics, they include only
  • 8:30 - 8:33
    combinations of the three basic structures
  • 8:33 - 8:37
    The sequence, the decision, and the loop.
  • 8:37 - 8:39
    Each of the structures has a single entry
  • 8:39 - 8:41
    and exit point.
  • 8:41 - 8:43
    Structures can be stacked or connected to
  • 8:43 - 8:45
    one another only at their entry or exit
  • 8:45 - 8:47
    points. And remember as I showed
  • 8:47 - 8:50
    the exit point of one is typically the
  • 8:50 - 8:53
    entry point for the next structure. And
  • 8:53 - 8:55
    any structure can be nested
  • 8:55 - 8:57
    within another structure.
Title:
CST-170 Chapter 3 - Understanding Structure - Part 1
Description:

more » « less
Video Language:
English
Duration:
08:57

English subtitles

Revisions