PLF Lesson 4 5 6
PLF Lesson 4 5 6
PLF Lesson 4 5 6
STRUCTURES
Spaghetti Code
A slang term used to refer to a tangled
programming source code
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
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.
Clarity - As programs get bigger, they get more confusing if they are not structured.
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.
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
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.
Selection
What structures do
you recognize from
the flowchart
segment?
Figure 1
Example 2:
Figure 2 Does the flowchart
segment structured?
What structures do
you recognize from
the flowchart
segment?
Loop
Example 3:
Figure 3
Spaghetti Bowl Method
One way to straighten out an unstructured flowchart
segment is to use the “spaghetti bowl” method.
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
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
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.
Declarations
string name
num hours
num RATE = 10.00
num WORK_WEEK = 40
num OVERTIME = 1.5
num pay
string QUIT = "ZZZ"
Module
==
==
Examples:
Relational Operators with Arithmetic Operators
Type Operator
+ -
Arithmetic
* /
Relational =, <, >, >=, <=, ==, <>
- (A+B)=(B+A)
- (A-B)=(B-A)
- (AB)=(BA)
- (A/B)=(B/A)