You are on page 1of 12

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

You might also like