Sei sulla pagina 1di 48

UNDERSTANDING

STRUCTURES
Spaghetti Code
 A slang term used to refer to a tangled
programming source code

 Logically snarled/twisted program statements

 Programs often work but are difficult to read and


maintain

 Confusing and prone to error


Spaghetti Code Logic
 This figure illustrates an
example of spaghetti code
logic of washing a dog.

 The figure is hard to


understand because of the
flow lines crossing each other.

 Programs written using


spaghetti code are more
difficult to maintain than other
programs.
Other Disadvantages of Spaghetti Code

 Slower Execution Speed


 Tangled code structure can make your program to
access the beginning of the code over and over again
where procedures are declared, which can slow the
program’s execution.

 Memory Leaks
 Memory leaks will not only slow a program down, but
also cause it to crash.
Structured Programming
 A Structure is a basic unit of programming logic; each structure is
one of the following:
 Sequence
 Selection
 Loop/Repetition/Iteration

 Structured programming (sometimes known as modular programming)


is a subset of procedural programming that enforces a logical
structure on the program being written to make it more efficient and
easier to understand and modify.

 Structured programming frequently employs a top-down design


model, in which developers map out the overall program structure
into separate subsections.
Structured Programming
 Each of the structures has a single entry point and a
single exit point.

 Structures can be stacked or connected to one


another only at their entry or exit points.

 Any structure can be nested within another structure.


Combining Structures
 All logic problems can
be solved using only
these three
structures—sequence,
selection, and loop.

 Attaching structures
end to end is called
stacking structures.
Combining Structures
 You can have a
sequence of three tasks
on one branch of a
selection, as shown in the
figure. Placing a
structure within another
structure is called
nesting structures.

 These three statements


constitute a group of
statements that executes
as a single unit.
Combining Structures
 The possible combinations
of logical structures are
endless, but each segment
of a structured program is
a sequence, a selection,
or a loop.

 The three structures are


shown together in the
figure. Notice that each
structure has one entry
point and one exit point.
Understanding the Reasons for Structure

 Clarity - As programs get bigger, they get more confusing if they are not structured.

 Professionalism - Make your programs to be structured. It is the way things are


done professionally like all other programmers do.

 Efficiency - Most newer computer languages support structure and use syntax that
lets you deal efficiently with sequence, selection, and looping. Newer languages
such as C#, C++, and Java enforce structure by their syntax.

 Maintenance - Its easier to modify and maintain structured programs as changes


are required in the future.

 Modularity - Structured programs can be easily broken down into modules that can
be assigned to any number of programmers. The routines are then pieced back
together like modular furniture at each routine’s single entry or exit point
Board Work:
 Create a flowchart that will accept a number
ranging from 1 to 100. If the input is higher than
100 it will print ―Condition Exceeded‖ and it will
ask for input again. If the input is less than 50 it will
print ―Result 1‖. If the input is higher than 50 it will
print ―Result 2‖.
Start

int number

Print ―Enter a number


from 1 to 100‖

input number

YES num>
NO
100
―Condition YES NO
Exceeded‖ num
>50

―Result 1‖ ―Result 2‖

End
RECOGNIZING
STRUCTURES
Recognizing Structures
 When you are beginning to learn about structured
program design, it is difficult to detect whether a
flowchart of a program’s logic is structured.

 In recognizing structure, the three basic structures


must be taken into consideration.

 By recognizing its structure we can identify if there


are tangled or unstructured flowchart segments.
Example 1:
 Does the flowchart
segment structured?
Sequence
 How many structures
does the flowchart
segment have?

Selection
 What structures do
you recognize from
the flowchart
segment?

Figure 1
Example 2:
Figure 2  Does the flowchart
segment structured?

Selection  How many structures


does the flowchart
segment have?

 What structures do
you recognize from
the flowchart
segment?

Loop
Example 3:

 Does the flowchart


This part of the segment structured?
segment is not
structured

 No, it is not built


from the three
basic structures.

Figure 3
Spaghetti Bowl Method
 One way to straighten out an unstructured flowchart
segment is to use the ―spaghetti bowl‖ method.

 That is, picture the flowchart as a bowl of spaghetti


that you must untangle. Imagine you can grab one
piece of pasta at the top of the bowl and start
pulling. As you ―pull‖ each symbol out of the
tangled mess, you can untangle the separate paths
until the entire segment is structured.
Spaghetti Bowl Method (cont’d.)
 Use the Spaghetti Bowl Method to structure the
flowchart segment

 Startpulling at Figure 3 top


 Encounter procedure box Labeled A (Figure 3-1)

Figure 3-1 Untangling Example 3, first step


Spaghetti Bowl Method (cont’d.)
 Next item is a question
 Testing
condition labeled B (Figure 3-2)
 Now know sequence starting with A ended

Figure 3-2 Untangling Example 3, second step


Spaghetti Bowl Method (cont’d.)
 Pull flowline from ―No‖ side of Question B
 Encounter C (Figure 3-3)

Figure 3-3 Untangling Example 3, third step


Spaghetti Bowl Method (cont’d.)
 Pull flowline from ―Yes‖ side of Question B
 Encounter D (Figure 3-4)

Figure 3-4 Untangling Example 3, fourth step


Spaghetti Bowl Method (cont’d.)
 Follow line on left side of Question D
 If line attached somewhere, untangle by repeating
tangled step
 Continue pulling flowline emerging from Step C
 Reach end of program segment (Figure 3-5)

Figure 3-5 Untangling Example 3, fifth step


Spaghetti Bowl Method (cont’d.)
 Pull right side of Question D
 Process
E pops up (Figure 2-6)
 Reached the end

Figure 2-6 Untangling Example 3, sixth step


Spaghetti Bowl Method (cont’d.)
 Question D brought together: selection structure
 Question B loose ends brought together: selection
structure

Figure 3 Finished flowchart and


pseudo code for untangling Example 3
Describing Three Special Structures –
Case, While and Do-Until
 Three more forms of basic structures
 Case
 Alternative decision-making structure
 Do-while
 Alternative to while loop
 Do-until loops
 Alternative to while loop
 Sometimes convenient
 All acceptable, legal structures
The Case Structure
 Several possible values exists for single variable
being tested
 Each value requires different course of action
 Flow passes through only one alternative
 Flowchart, pseudo code, program code convenience
 Easier to understand at first glance
 Examples
 Figure 4-1: series of decisions
 Figure 4-2: case structure implementing decisions
The Case Structure (cont’d.)

Figure 4-1 Flowchart and pseudo code of tuition


decisions
The Case Structure (cont’d.)

Figure 4-2 Flowchart and


pseudo code of case
structure
The While Loop

 while loop
 Condition tested at structure beginning
 Condition not met at first test
 Code in while structure body never executed
 Also called a pretest loop

Figure 4-3 The while loop


The Do-While or Do-Until Loops

 do-while loop
 Condition tested at structure end
 Body has executed at least once
 Expressed as a sequence followed by a loop
 Also called a posttest loop

Figure 4-4 The do-while or do-until loop


MAKING DECISIONS
Boolean Expressions and Selection Structure
Using Relational Comparison Operators
Boolean Expressions
 The reason people frequently think computers are smart lies
in the ability of computer programs to make decisions. Like
a program that can offer different potential driving routes
based on your destination.

 Every decision a computer program makes involves


evaluating a Boolean expression - an expression whose
value can be only true or false.

 True/false evaluation is natural from a computer’s


standpoint, because computer circuitry consists of two-state
on-off switches, often represented by 1 or 0.
Boolean Expressions
 Every computer decision yields a true-or-false, yes-or-no, 1-
or-0 result.

 A Boolean expression is used to control every selection


structure.

 The selection structure can only be used by using the


Boolean expression.

 George Boole is considered the founder of mathematical


logic, and Boolean (true/false) expressions are named for
him.
Two Forms of Selection Structure
 Figure 4-1 is a dual-
alternative selection (or
binary selection structure).

 The choices are mutually


exclusive; the logic can
flow to only one of the
two alternatives, never to
both.

 This form of the selection


structure is an if-then-else
selection.
Two Forms of Selection Structure
 Figure 4-2 represents a
single-alternative selection
(or unary selection structure)
in which action is required for
only one outcome of the
question.

Null case  This form of the selection


structure is called an if-then
selection, because no
alternative or else action is
necessary.

 Null Case - The branch of a


decision in which no action is
taken.
The if Statement

The if-then clause is the part of the decision that holds the action or actions that execute when the tested
condition in the decision is true.

The else clause of the decision is the part that executes only when the tested condition
in the decision is false.
Sample Program Concept
 Create a flowchart and pseudo code for program that
displays the weekly pay for each employee at the
same hourly rate ($10.00) and assumes that there are
no payroll deductions.

 The mainline logic calls housekeeping(), detailLoop(),


and finish() modules. The detailLoop() module contains a
typical dual-alternative selection that determines
whether an employee has worked more than a
standard workweek (40 hours), and pays one and one-
half times the employee’s usual hourly rate for hours
worked in excess of 40 per week.
Program Flowchart
Modules

Declarations
string name
num hours
num RATE = 10.00
num WORK_WEEK = 40
num OVERTIME = 1.5
num pay
string QUIT = "ZZZ"

Module

The return keyword terminates the


execution of a module or a
function and returns control to the
calling function.
Program Pseudo code
Program Output
Using Relational Comparison Operators

 Relational Comparison Operators


 Six types supported by all modern programming languages
 Two values compared can be either variables or constants

 Both operands in a comparison expression must be the


same data type: numeric values to other numeric values,
and text strings to other strings.
 Trivial Expressions
 Will always evaluate to the same result
 Examples:
 True for 20 = 20
 False for 30 = 40
Relational Comparison Operators
Operator Name Discussion
Evaluates as true when its operands are
equivalent. Because the assignment operator
in many languages is the equal sign, those
= Equivalency operator languages often use a double equal sign (==)
as the equivalency operator to avoid
confusion with the assignment operator.

Evaluates as true when the left operand is


> Greater than operator greater than the right operand.

Evaluates as true when the left operand is less


< Less than operator than the right operand.
Relational Comparison Operators
Operator Name Discussion
Evaluates as true when the left operand is

>= Greater than or equal


to operator
greater than or equivalent to the right
operand.

Evaluates as true when the left operand is less


<= Less than or equal to
operator
than or equivalent to the right operand.

Evaluates as true when its operands are not


equivalent. Some languages use an
exclamation point followed by an equal sign
<> Not-equal-to operator (!=) as the not-equal-to operator. In a
flowchart or pseudocode, you might prefer to
use the algebraic not equal to symbol (≠) or
to spell out the words ―not equal to.‖
Relational Comparison Operators
 Any decision can be made with only three types of
comparisons: =, > and <.
 The >= and <= makes the code more readable

 The ―Not Equal‖ operator is the most confusing of all


the comparisons.
 Using not equal to in decisions involves thinking in
double negatives.
 It can make you prone to introducing logical
errors into your programs.
Example:
Using the Not Equal To Operator
Example:
Using the Equal To Operator

==

==
Examples:
 Relational Operators with Arithmetic Operators
Type Operator
+ -
Arithmetic
* /
Relational =, <, >, >=, <=, ==, <>

- (A+B)=(B+A)
- (A-B)=(B-A)
- (AB)=(BA)
- (A/B)=(B/A)

Potrebbero piacerti anche