Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Programming Programming. COMP102 Prog Fundamentals I: Structured Programming /Slide 2 Structured Programming l Structured programing is the.

Similar presentations


Presentation on theme: "Structured Programming Programming. COMP102 Prog Fundamentals I: Structured Programming /Slide 2 Structured Programming l Structured programing is the."— Presentation transcript:

1 Structured Programming Programming

2 COMP102 Prog Fundamentals I: Structured Programming /Slide 2 Structured Programming l Structured programing is the set of design and implementation processes that yield well- structured programs.

3 COMP102 Prog Fundamentals I: Structured Programming /Slide 3 Structured Programming l A disciplined approach to programming l Top-down design l Step-wise refinement using a restricted set* of program structures * The set of program structures used in structured programming is: n Sequence n Choice n Loop

4 COMP102 Prog Fundamentals I: Structured Programming /Slide 4 Top-Down Design l 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 COMP102 Prog Fundamentals I: Structured Programming /Slide 5 Stepwise Refinement Any action can be another: n Sequence n Choice n Loop condition action_x true false initialization update condition_x action_y true false

6 COMP102 Prog Fundamentals I: Structured Programming /Slide 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 COMP102 Prog Fundamentals I: Structured Programming /Slide 7 A Choice Statement l Syntax if(condition) action l if the condition is true then execute the action. l action is either a single statement or a group of statements within braces. condition action true false

8 COMP102 Prog Fundamentals I: Structured Programming /Slide 8 Another Choice Statement l Syntax if (condition) Action_A else Action_B l if the condition is true execute Action_A else execute Action_B. condition Action_A Action_B truefalse

9 COMP102 Prog Fundamentals I: Structured Programming /Slide 9 A Loop Statement l Syntax while (condition) action l How it works: n if condition is true then execute action n repeat this process until condition evaluates to false l action is either a single statement or a group of statements within braces. condition action true false

10 COMP102 Prog Fundamentals I: Structured Programming /Slide 10 Another Loop Statement l Syntax for (initialization; condition; update) action l How it works: n execute initialization statement n while condition is true –execute action –execute update condition action true false initialization update

11 COMP102 Prog Fundamentals I: Structured Programming /Slide 11 Yet Another Loop Statement l Syntax do action while (condition) l How it works: n execute action n if condition is true then execute action again n repeat this process until condition evaluates to false. l action is either a single statement or a group of statements within braces. action true false condition

12 COMP102 Prog Fundamentals I: Structured Programming /Slide 12 Diamond Pattern l Print out the following diamond pattern * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

13 COMP102 Prog Fundamentals I: Structured Programming /Slide 13 Diamond Pattern l Sub-problem: n print out the upper half n print out the lower half l Print out upper half: n row 1: print 4 spaces, 1 star; * n row 2: print 3 spaces, 3 stars; * * * n row 3: print 2 spaces, 5 stars; * * * * * n row 4: print 1 space, 7 stars; * * * * * * * n row 5: print 0 spaces, 9 stars; * * * * * * * * * l Print out lower half: n row 4: print 1 space, 7 stars; * * * * * * * n row 3: print 2 spaces, 5 stars; * * * * * n row 2: print 3 spaces, 3 stars; * * * n row 1: print 4 spaces, 1 star; *

14 COMP102 Prog Fundamentals I: Structured Programming /Slide 14 Diamond Pattern l Algorithm for upper half: n row 1: print (5-row)spaces, (2*row - 1) stars; * n row 2: print (5-row)spaces, (2*row - 1) stars; * * * n row 3: print (5-row)spaces, (2*row - 1) stars; * * * * * n row 4: print (5-row)spaces, (2*row - 1) stars; * * * * * * * n row 5: print (5-row)spaces, (2*row - 1) stars; * * * * * * * * * l Algorithm for lower half: n row 4: print (5-row)spaces, (2*row - 1) stars; * * * * * * * n row 3: print (5-row)spaces, (2*row - 1) stars; * * * * * n row 2: print (5-row)spaces, (2*row - 1) stars; * * * n row 1: print (5-row)spaces, (2*row - 1) stars; *

15 COMP102 Prog Fundamentals I: Structured Programming /Slide 15 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 ; }


Download ppt "Structured Programming Programming. COMP102 Prog Fundamentals I: Structured Programming /Slide 2 Structured Programming l Structured programing is the."

Similar presentations


Ads by Google