mpp-upnvj

29
Structured Programming A VERY important Philosophy for Writing Programs

Upload: m-misdianto-msc

Post on 26-Jun-2015

596 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MPP-UPNVJ

Structured Programming

A VERY important Philosophy for Writing Programs

Page 2: MPP-UPNVJ

This copy of Presentation can be found At

http://www.slideshare.net/Misdianto

Page 3: MPP-UPNVJ

Bad Programming Habits

• Foggy idea about what is to be done • Write program with no planning; Start from the

beginning and write to the end• No systematic debugging; Considered it finished if it

works on one example

• Undesirable Results !o Do not know how to programo Programs with numerous bugs that take extremely long time

to debug, or even failure to completeo Any change of requirement invites rewriting of the entire

program again

Page 4: MPP-UPNVJ

Benefits of Structured Programming• Programs that meet the needs of the

customer• Though initially take longer time to generate

code, often result in code with runs with no bugs the first time it’s run

• Easy to handle change in program specifications in the future

Page 5: MPP-UPNVJ

Structured Programming

• A tool that becomes popular since the 70’s• Should have been learnt by student that have

taken any programming course• Absolutely essential for handling large

programs that involve a team of programmers and huge number of man hours.

• The other popular philosophy is “object oriented programming”, but many programmers prefers structured programming

Page 6: MPP-UPNVJ

Seven Important Concepts of Structured Programming: 1. Structured Walkthrough• Before writing any program, the programming team

must sit down with the customer and find out the requirement

• Extremely important• Customer’s requirement is often imprecise• Iterative: several rounds of talks• Must result in a specifications that is

o very preciseo Understandable by programmer in programming terms

Page 7: MPP-UPNVJ

2. Stepwise Refinement

• A “DIVIDE and CONQUER” strategy• When given a large job, divide it into smaller jobs.• Given any job, it is useful to divide it into

o Inputo Processo Output

• Draw a tree• Refine each job level by level (Breadth first)• Use pseudo code to describe each job• Decision on data structure is delayed as much as

possible

Page 8: MPP-UPNVJ

Extremely Complex Job

Input Process Output Level 1

Input from user Initialize

… …

Level 2

Initialize securitysettings

Level 3

Page 9: MPP-UPNVJ

3. Modular Design

• Each ellipse is a module• A module is a self contained block:

o It only receives inputs from its immediate ancestoro It only outputs to its immediate ancestoro Its computation should only require calling functions that are

its immediate children and them only• The input variables and output variables of each

module should be specified when defining the module

• Each module must be “programmable” – no magic block should exist

Page 10: MPP-UPNVJ

4. Bottom Up Coding

• When the refinement has reached a simple function, code the simple function

• You can test the simple function independently of the rest of the program

• This gives you achievement and satisfaction, sustaining you through the long project

• Project Manager exercises division of labour here, ask a member to be responsible solely for that function

Page 11: MPP-UPNVJ

5. Testing Using Stubs

• A structured Programming project can be field tested before everything finishes

• Stubs - for unfinished modules, use a human being to emulate it, act on the test inputs, she fits in the correct output data by hand

• then other programmers can test their work • Meanwhile she continues to program her own

module (according to MS Project timelines)

Page 12: MPP-UPNVJ

6. White Box and Black Box Testing• For each module and whole program

• White Boxo Input something for which you know the desired

result, it should give your expected output• Black Box

o Treat it as a black box, input some data, is the result reasonable?

Page 13: MPP-UPNVJ

7. Structured Programming Documents

• A structured programming document is generated along with the program

• When requirement of customer changes, go to the document

• Does not need to rewrite the whole program, just find which modules need to rewrite and rewrite the module and the sub-tree under it

• Programmer usually forgets their code in 2 months; the structured document helps her to refresh her work quickly

Page 14: MPP-UPNVJ

Review

• 3 structures of structured programmingo Sequence

Perform an actiono Branch

Select an action (or no action)o Loop

Repeat an action

Page 15: MPP-UPNVJ

Actions

• Declareo Need storage space for our actions/results

• Assigno Random valueo Mathematical expressiono User input

• Output

Page 16: MPP-UPNVJ

Decisions

• Branching selects from two paths• Relational Operators turn integers into

true/false• AND – the expression is TRUE if and only if

both input variables are TRUE• OR – the expression is TRUE if either input

variable is TRUE

Page 17: MPP-UPNVJ

Two Branches

• Did you roll a doublet?o die1 == die2 double moveo Normal move

Page 18: MPP-UPNVJ

Multiple Branches

• What is your grade classification?o >= 80 honourso >= 60 passo Not pass

Page 19: MPP-UPNVJ

Sample Program

• Write a program that tells a user if their input matches a random number between 1 and 10o Declare and get inputo Declare and get random valueo If input equals random value

Output “good guess”

Page 20: MPP-UPNVJ

Sample Program II

• Write a program that takes two inputs and outputs the largest valueo Declare and get first inputo Declare and get second inputo If first value is greater than second value

Output first value Otherwise, output second value

Page 21: MPP-UPNVJ

Sample Program III

• Write a program that takes three inputs and outputs the largest value

Page 22: MPP-UPNVJ

Looping

• In Sequence and Branching, each action is performed at most once

• Looping allows certain actions to be repeated• Keeps programs from getting large and

unmanageable

Page 23: MPP-UPNVJ

Example of Looping

• Program specificationo Take user inputs until they guess correctly

• Programo Declare and initialize numbero Declare guesso Input and compare guesso Input and compare guesso …

Page 24: MPP-UPNVJ

Looping Icon

• Loops have same components as branches

• Condition selects one of two paths• Optional path “loops back” to condition

Page 25: MPP-UPNVJ

Sample Program

• Write a program that outputs the values from 1 to 10o Declare and initialize valueo While value is less than or equal to 10

Output value Increment value

Page 26: MPP-UPNVJ

This copy of Presentation can be found At

http://www.slideshare.net/Misdianto

Page 27: MPP-UPNVJ

Sample Program II

• Write a program that uses repeated subtraction to get the remainder and quotient of an input dividend and divisoro Declare and get inputso Declare and initialize outputso While more subtraction to perform

Update outputso Output results

Page 28: MPP-UPNVJ

Kelas C

• Rio Efency – 209512094• Monica Amadia – 209512096• Made Wisnu Darmadi – 209512101• Sovianna Samosir – 209512116• Alip Rinjat Moko – 209512120

Page 29: MPP-UPNVJ

Kelas D

• Maulana Gita Nugraha – 209512128• Ariyadi Nugroho – 209512129• Eko Wicaksono Putra – 209512136• Mochamad Ridwan – 209512146• Hafid Wijayanto – 209512149• Inggil Rinekso – 208512074