Structured%20Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Structured%20Programming

Description:

Programming Structured Programming – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 16
Provided by: Andrew1565
Category:

less

Transcript and Presenter's Notes

Title: Structured%20Programming


1
Programming
  • Structured Programming

2
Structured Programming
  • Structured programing is the set of design and
    implementation processes that yield
    well-structured programs.

3
Structured Programming
  • A disciplined approach to programming
  • Top-down design
  • Step-wise refinement using a restricted set of
    program structures
  • The set of program structures used in
    structured programming is
  • Sequence
  • Choice
  • Loop

4
Top-Down Design
  • A program is divided into a main module and its
    related modules. Each module is in turn divided
    into submodules until the resulting modules are
    understood without further division.

5
Stepwise Refinement
  • Any action can be
  • another
  • Sequence
  • Choice
  • Loop

initialization
condition_x
false
condition
false
true
true
action_x
action_y
update
6
A Sequence
  • It is natural to write a program as a
    sequence of program structures such as sequences,
    choices and loops.

Action 1
Action 2
Action N
7
A Choice Statement
  • Syntax
  • if(condition)
  • action
  • if the condition is true then execute the action.
  • action is either a single statement or a group of
    statements within braces.

condition
false
true
action
8
Another Choice Statement
  • Syntax
  • if (condition)
  • Action_A else Action_B
  • if the condition is true
  • execute Action_A
  • else
  • execute Action_B.

condition
true
false
Action_A
Action_B
9
A Loop Statement
  • Syntax
  • while (condition)
  • action
  • How it works
  • if condition is true then execute action
  • repeat this process until condition evaluates to
    false
  • action is either a single statement or a group of
    statements within braces.

condition
false
true
action
10
Another Loop Statement
  • Syntax
  • for (initialization condition update)
  • action
  • How it works
  • execute initialization
  • statement
  • while condition is true
  • execute action
  • execute update

initialization
condition
false
true
action
update
11
Yet Another Loop Statement
  • Syntax
  • do action
  • while (condition)
  • How it works
  • execute action
  • if condition is true then execute action again
  • repeat this process until condition evaluates to
    false.
  • action is either a single statement or a group of
    statements within braces.

action
condition
true
false
12
Diamond Pattern
  • Print out the following diamond pattern

13
Diamond Pattern
  • Sub-problem
  • print out the upper half
  • print out the lower half
  • Print out upper half
  • row 1 print 4 spaces, 1 star
  • row 2 print 3 spaces, 3 stars
  • row 3 print 2 spaces, 5 stars
  • row 4 print 1 space, 7 stars
  • row 5 print 0 spaces, 9 stars
  • Print out lower half
  • row 4 print 1 space, 7 stars
  • row 3 print 2 spaces, 5 stars
  • row 2 print 3 spaces, 3 stars
  • row 1 print 4 spaces, 1 star

14
Diamond Pattern
  • Algorithm for upper half
  • row 1 print (5-row)spaces, (2row - 1) stars
  • row 2 print (5-row)spaces, (2row - 1) stars
  • row 3 print (5-row)spaces, (2row - 1) stars
  • row 4 print (5-row)spaces, (2row - 1) stars
  • row 5 print (5-row)spaces, (2row - 1) stars
  • Algorithm for lower half
  • row 4 print (5-row)spaces, (2row - 1) stars
  • row 3 print (5-row)spaces, (2row - 1) stars
  • row 2 print (5-row)spaces, (2row - 1) stars
  • row 1 print (5-row)spaces, (2row - 1) stars

15
Diamond Pattern
  • int row, space, star
  • for(row1 rowlt5 row) //top half
  • for(space1 spacelt5-row space)
  • cout ltlt " "
  • for(star1 starlt2row-1 star)
  • cout ltlt ""
  • cout ltlt endl
  • for(row4 rowgt1 row--) //bottom half
  • for(space1 spacelt5-row space)
  • cout ltlt " "
  • for(star1 starlt2row-1 star)
  • cout ltlt ""
  • cout ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com