Understanding the Three Basic Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Understanding the Three Basic Structures

Description:

Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program can be constructed from only three basic types of structures – PowerPoint PPT presentation

Number of Views:219
Avg rating:3.0/5.0
Slides: 47
Provided by: mfu65
Category:

less

Transcript and Presenter's Notes

Title: Understanding the Three Basic Structures


1
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

2
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.

3
Structure Theorem
3. Repetition
Sequential instructions
Do While / Do Until
IF THEN ELSECASE
4
Understanding the Three Basic Structures
(continued)
  • Sequence structure
  • A set of instructions, performed sequentially
    with no branching

5
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

6
Flowchart for a Sequence
START
Instructions follow each other sequentially
Input sales amount from customer
Sale
Sequential instructions
Computer total amount
Sales amount x .06
Print report
Sale report
Hard Drive
Printed Report
Sale data
Save in file
END
7
Flowchart for a Decision
  • Decision or selection structure flowchart

IF --- THEN --- ELSE CASE statement
Question
Answer is NO (false)
Answer is YES (true)
8
Understanding the Three Basic Structures
(continued)
  • Dual-alternative if contains two alternatives

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.
The Problem
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
Pauedocode
Flowchart
9
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 to the overtime pay amount which is
overtime hours multiplied by 1 ½ time the regular
pay amount.
The Problem
Total pay regular hours multiplied by regular
payIF 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
IFPrint to printer the total pay amount
(action if true or false)
Pauedocode
FALSE path
TRUE path
Question
Flowchart
TRUE or FALSE path
10
Understanding the Three Basic Structures
(continued)
  • Single-alternative if
  • Else clause is not required
  • Null case situation where nothing is done

If
End If
11
Flowchart for a Decision
ASK THE QUESTION
IF condition THEN instruction1
instruction2 as many instructions as needed
as many structures (decision, sequential,
looping) as neededELSE instruction1
instruction2 as many instructions as needed
as many structures (decision, sequential,
looping) as neededENDIFContinuation of the
program (instructions and structures)
TRUE path if the questions answer is true (YES)
IF condition THEN as many instructions as
needed as many structures (decision,
sequential, looping) as needed ENDIF
TRUE path if the questions answer is true (YES)
FALSE path if the questions answer is false (NO)
12
Flowchart for a Decision
CONDITIONS
A lt B (A B are the same data type
(numeric or alphanumeric) X 5 gt Z (X and Z
are numeric data types) E lt 5 (E is a numeric
data type) F gt 10 (F is a numeric data type)
IF A lt B THEN instructions/structuresELSE
IF X 5 gt Z THEN instructions/struct
ures ENDIFENDIF
Nesting
13
Flowchart for a Decision
Example 1
Somewhere before this decision, data is placed in
the variables HOURS and RATE IF HOURS gt 40
THEN PAY RATE (40 1.5 (HOURS
40))ELSE PAY RATE HOURSENDIF
14
Nested Decisions (IF THEN ELSE)
Example 2
ENDIF
ENDIF
15
Example 3
ENDIF
Range Check
ENDIF
ENDIF
16
Example 4
ENDIF
ENDIF
ENDIF
17
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

18
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
19
Flowchart for a Loop
  • Loop or repetition structure flowchart

Ask a questionAnswer is Yes Execute the
loop Answer is NO Exit the loop
Question
20
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

21
Understanding the Three Basic Structures
(continued)
22
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 structures statements
  • Block group of statements that execute as a
    single unit

23
Understanding the Three Basic Structures
(continued)
24
Understanding the Three Basic Structures
(continued)
25
Understanding the Three Basic Structures
(continued)
26
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

27
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
  • First ask a question
  • Take action based on the answer
  • Return to ask the question again

28
Using the Priming Read (continued)
  • Unstructured loop

29
Using the Priming Read (continued)
  • Structured but nonfunctional loop

30
Using the Priming Read (continued)
  • Corrrect

31
Using the Priming Read (continued)
  • Functional and structured loop

32
Using the Priming Read (continued)
  • Priming read sets up the process so the loop can
    be structured
  • To analyze a flowcharts structure, try writing
    pseudocode for it

33
Using the Priming Read (continued)
  • What is wrong with this design?

34
Understanding the Reasons for Structure
  • Advantages of structure
  • Provides clarity
  • Professionalism
  • Efficiency
  • Ease of maintenance
  • Supports modularity

35
Understanding the Reasons for Structure
(continued)
36
Recognizing Structure (continued)
  • Next, pull up the flowline on the right side of B

37
Recognizing Structure (continued)
  • Now pull up the flowline on the right side of D

38
Recognizing Structure (continued)
  • Bring together the loose ends of D and of B

39
Three Special Structures Case, Do While, and Do
Until
  • Many languages allow three additional structures
  • case structure
  • do-while structure
  • do-until structure
  • Case Structure
  • Decisions with more than two alternatives
  • Tests a variable against a series of values and
    takes action based on a match
  • Nested if-then-else statements will do what a
    case structure does

40
Three Special Structures Case, Do While, and Do
Until (continued)
  • Using nested if-then-else for multiple
    alternatives

41
Three Special Structures Case, Do While, and Do
Until (continued)
  • Using a case structure for multiple alternatives

42
Three Special Structures Case, Do While, and Do
Until (continued)
  • do-while and do-until loops
  • Question is asked at the end of the loop
    structure
  • Ensures that the loop statements are always used
    at least once

43
Three Special Structures Case, Do While, and Do
Until (continued)
  • do-while loop executes as long as the questions
    answer is Yes or True Test checked at
    beginning May not be executed
  • do-until loop executes as long as the questions
    answer is No or False (until it becomes Yes or
    True) Test checked at end of loop Will
    always execute loop at least once

44
Three Special Structures Case, Do While, and Do
Until (continued)
  • while loop with question at beginning is called a
    pretest loop
  • do-until with question at end are called posttest
    loops

45
Three Special Structures Case, Do While, and Do
Until (continued)
46
Three Special Structures Case, Do While, and Do
Until (continued)
Write a Comment
User Comments (0)
About PowerShow.com