Loop structure

Understanding the Three Basic
Structures
• Structure: a basic unit of programming logic
• Any program can be constructed from only three
basic types of structures
– Sequence
– Selection
– Loop
Programming Logic and Design, Introductory, Fourth Edition
1
Control Structures
• Sequence –in sequential order.
– The simplest of control structures – start at the
beginning and continue in sequential order.
• Repetition – repeat statements more than once
– Also called a loop, it needs a stop condition, i.e, the
program will continue to loop until some condition is
met.
• Selection – selectively execute statements
– Called a branch, it requires a condition to determine
when to execute statements.
Programming Logic and Design, Introductory, Fourth Edition
2
Structure Theorem
1. Sequence
Instruction
Sequential
instructions
Instruction
3. Repetition
Do While / Do Until
Instruction
Instruction
Instruction
Instruction
2. Selection
IF … THEN … ELSE
CASE
Instruction
Instruction
Instruction
?
Instruction
Instruction
Instruction
Programming Logic and Design, Introductory, Fourth Edition
3
Understanding the Three Basic
Structures (continued)
• Sequence structure
– A set of instructions, performed sequentially with no
branching
Programming Logic and Design, Introductory, Fourth Edition
4
Understanding the Three Basic
Structures (continued)
• Selection structure
– Asks a question, then takes one of two possible
courses of action based on the answer
– Also called a decision structure or an if-then-else
Programming Logic and Design, Introductory, Fourth Edition
5
Flowchart for a Sequence
START
Instructions follow each
other sequentially
Sale
Input sales amount
from customer
Sequential instructions
Computer total
amount
Print report
Hard Drive
Sales amount x .06
Sale report
Printed
Report
Sale
data
Save in file
END
Programming Logic and Design, Introductory, Fourth Edition
6
Flowchart for a Decision
Decision or selection structure flowchart:
IF --- THEN --- ELSE
CASE statement
Answer is “YES”
(true)
Programming Logic and Design, Introductory, Fourth Edition
Question
Answer is “NO”
(false)
7
Understanding the Three Basic
Structures (continued)
• Dual-alternative if: contains two alternatives
The
Problem
Pauedocode
If the hours an employee has worked is greater than 40 hours then calculate
their pay as regular hours multiplied by their regular time pay mount added
to the overtime pay amount which is overtime hours multiplied by 1 ½
time the regular pay amount.
IF the hours worked is more than 40 THEN (question)
total pay will be = regular pay amount plus overtime hours multiplied
by 1 ½ times regular pay amount (action if true)
ELSE
total pay is regular hours times regular pay amount (action if false)
END IF
Flowchart
Programming Logic and Design, Introductory, Fourth Edition
8
•
Understanding the Three Basic
Structures (continued)
Single-alternative if: contains one alternative
If the hours an employee has worked is greater than 40 hours then calculate
their pay as regular hours multiplied by their regular time pay mount added
The
Problem to the overtime pay amount which is overtime hours multiplied by 1 ½
time the regular pay amount.
Pauedocode
Total pay = regular hours multiplied by regular pay
IF the hours worked is more than 40 THEN (question)
total pay will be = total pay amount plus overtime hours multiplied
by 1 ½ times regular pay amount (action if true)
END IF
Print to printer the total pay amount (action if true or false)
FALSE path
Flowchart
Question TRUE path
Programming Logic and Design, Introductory, Fourth Edition
TRUE or FALSE path
9
Understanding the Three Basic
Structures (continued)
•
Single-alternative if
If
End If
•
•
Else clause is not required
Null case: situation where nothing is done
Programming Logic and Design, Introductory, Fourth Edition
10
Flowchart for a Decision
ASK THE QUESTION
IF condition THEN
instruction1
TRUE path if the questions answer is true (YES)
instruction2
as many instructions as needed
as many structures (decision, sequential, looping) as needed
ELSE
instruction1
instruction2
as many instructions as needed
as many structures (decision, sequential, looping) as needed
ENDIF
Continuation of the program (instructions and structures)
IF condition THEN
TRUE path if the questions answer is true (YES)
as many instructions as needed
as many structures (decision, sequential, looping) as needed
ENDIF
FALSELogic
pathand
if the
questions
answer
false (NO)
Programming
Design,
Introductory,
Fourthis
Edition
11
Flowchart for a Decision
CONDITIONS
A < B
(A & B are the same data type (numeric or alphanumeric)
X + 5 >= Z (X and Z are numeric data types)
E < 5 (E is a numeric data type)
F > 10 (F is a numeric data type)
IF A < B THEN
instructions/structures
ELSE
IF X + 5 >= Z THEN
instructions/structures
ENDIF
ENDIF
Nesting
Programming Logic and Design, Introductory, Fourth Edition
12
Example 1
Flowchart for a Decision
Somewhere before this decision, data is
placed in the variables HOURS and RATE
IF HOURS > 40 THEN
PAY = RATE * (40 + 1.5 * (HOURS – 40))
ELSE
start
PAY = RATE * HOURS
Data is put into
ENDIF
HOURS and RATE
IF
HOURS > 40
PAY= RATE *
(40 + 1.5 *
(HOURS – 40))
PAY = RATE *
HOURS
Programming Logic and Design, Introductory, Fourth Edition
end
13
Example 2
Nested Decisions (IF – THEN – ELSE)
ENDIF
ENDIF
Programming Logic and Design, Introductory, Fourth Edition
14
Example 3
ENDIF
Range Check
ENDIF
ENDIF
Programming Logic and Design, Introductory, Fourth Edition
15
Example 4
ENDIF
ENDIF
ENDIF
Programming Logic and Design, Introductory, Fourth Edition
16
Understanding the Three Basic
Structures (continued)
• Loop structure
– Repeats a set of actions based on the answer to a
question
– Also called repetition or iteration
– Question is asked first in the most common form of loop
Programming Logic and Design, Introductory, Fourth Edition
17
Understanding the Three Basic
Structures (continued)
• Loop structure
WHILE testcondition
(check if testcondition is true)
do however many instructions are required (testcondition is true)
END LOOP (end of loop – go back to beginning and check condition)
Continue with whatever processing is necessary
DO WHILE or DO UNTIL
Check Condition
here or here
Question
TRUE (repeat)
FALSE
Programming Logic and Design, Introductory, Fourth Edition
18
Flowchart for a Loop
Loop or repetition structure flowchart:
Ask a question
Answer is “Yes”
Execute the loop
Question
Answer is “NO”
Exit the loop
Programming Logic and Design, Introductory, Fourth Edition
19
Understanding the Three Basic
Structures (continued)
• All logic problems can be solved using only these
three structures
• Structures can be combined in an infinite number of
ways
• Stacking: attaching structures end-to-end
• End-structure statements
– Indicate the end of a structure
– endif: ends an if-then-else structure
– endwhile: ends a loop structure
Programming Logic and Design, Introductory, Fourth Edition
20
Understanding the Three Basic
Structures (continued)
Programming Logic and Design, Introductory, Fourth Edition
21
Understanding the Three Basic
Structures (continued)
• Any individual task or step in a structure can be
replaced by a structure
• Nesting: placing one structure within another
• Indent the nested structure’s statements
• Block: group of statements that execute as a
single unit
Programming Logic and Design, Introductory, Fourth Edition
22
Understanding the Three Basic
Structures (continued)
Programming Logic and Design, Introductory, Fourth Edition
23
Understanding the Three Basic
Structures (continued)
Programming Logic and Design, Introductory, Fourth Edition
24
Understanding the Three Basic
Structures (continued)
Programming Logic and Design, Introductory, Fourth Edition
25
Understanding the Three Basic
Structures (continued)
• Each structure has one entry and one exit point
• Structures attach to others only at entry or exit points
Programming Logic and Design, Introductory, Fourth Edition
26
Using the Priming Read
•
Priming read (or priming input):
– Reads the first input data record
– Outside the loop that reads the rest of the records
– Helps keep the program structured
•
•
Analyze a flowchart for structure one step at a time
Watch for unstructured loops that do not follow this
order:
1. First ask a question
2. Take action based on the answer
3. Return to ask the question again
Programming Logic and Design, Introductory, Fourth Edition
27
Using the Priming Read (continued)
• Unstructured loop:
Programming Logic and Design, Introductory, Fourth Edition
28
Using the Priming Read (continued)
• Structured but nonfunctional loop
Programming Logic and Design, Introductory, Fourth Edition
29
Using the Priming Read (continued)
• Corrrect
Programming Logic and Design, Introductory, Fourth Edition
30
Using the Priming Read (continued)
• Functional and structured loop
Programming Logic and Design, Introductory, Fourth Edition
31
Using the Priming Read (continued)
• Priming read sets up the process so the loop can be
structured
• To analyze a flowchart’s structure, try writing
pseudocode for it
Programming Logic and Design, Introductory, Fourth Edition
32
Using the Priming Read (continued)
• What is wrong with this design?
Programming Logic and Design, Introductory, Fourth Edition
33
Understanding the Reasons for
Structure
• Advantages of structure:
–
–
–
–
–
Provides clarity
Professionalism
Efficiency
Ease of maintenance
Supports modularity
Programming Logic and Design, Introductory, Fourth Edition
34
Understanding the Reasons for
Structure (continued)
Programming Logic and Design, Introductory, Fourth Edition
35
Recognizing Structure (continued)
• Next, pull up the flowline on the right side of B
Programming Logic and Design, Introductory, Fourth Edition
36
Recognizing Structure (continued)
• Now pull up the flowline on the right side of D
Programming Logic and Design, Introductory, Fourth Edition
37
Recognizing Structure (continued)
• Bring together the loose ends of D and of B
Programming Logic and Design, Introductory, Fourth Edition
38