1 / 14

Programming

Programming. Structured Programming. 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.

Download Presentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Structured Programming

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

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

  4. Stepwise Refinement initialization Any action can be another: • Sequence • Choice • Loop condition_x false condition false true true action_x action_y update

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

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

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

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

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

  10. Yet Another Loop Statement • Syntax doaction 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

  11. Diamond Pattern • Print out the following diamond pattern * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  12. 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; *

  13. Diamond Pattern • Algorithm for upper half: • row 1: print (5-row)spaces, (2*row - 1) stars; * • row 2: print (5-row)spaces, (2*row - 1) stars; * * * • row 3: print (5-row)spaces, (2*row - 1) stars; * * * * * • row 4: print (5-row)spaces, (2*row - 1) stars; * * * * * * * • row 5: print (5-row)spaces, (2*row - 1) stars; * * * * * * * * * • Algorithm for lower half: • row 4: print (5-row)spaces, (2*row - 1) stars; * * * * * * * • row 3: print (5-row)spaces, (2*row - 1) stars; * * * * * • row 2: print (5-row)spaces, (2*row - 1) stars; * * * • row 1: print (5-row)spaces, (2*row - 1) stars; *

  14. Diamond Pattern int row, space, star; for(row=1; row<=5; row++){ //top half for(space=1; space<=5-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; } for(row=4; row>=1; row--){ //bottom half for(space=1; space<=5-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; }

More Related