THEORY OF STRUCTURED PROGRAM DESIGN
. WANT TO SHOW HOW PROGRAMS ARE, IN FACT, DESIGNED.
. "PROGRAM DESIGN" ===> THE DEVELOPMENT OF A PROGRAM SO THAT ITS ELEMENTS FIT TOGETHER LOGICALLY AND IN AN INTEGRATED WAY.
. PROGRAMS PLANNED BEFORE CODING WILL BE BETTER
DESIGNED!
... AND MINIMIZE ERRORS.
. PLANNING TOOLS:
.. FLOWCHARTS
.. PSEUDOCODE - STRUCTURED PSEUDOCODE
STANDARD PSEUDOCODE
.. HIERARCHY CHARTS - STRUCTURE CHARTS
A MODULE IS A SET OF INSTRUCTIONS THAT PERFORMS A SINGLE FUNCTION.
WE PERFORM MODULES -
CONTROL IS TRANSFERRED
MODULE IS EXECUTED
CONTROL RETURNED TO NEXT SEQUENTIAL STATEMENT
1
THERE ARE OTHER "TRANSFERS OF CONTROL" STATEMENTS,
SUCH AS THE GOTO, CALL.
. WILL MINIMIZE THE USE OF THE GOTO:
. BE AWARE HOW IT WORKS
. YOU WILL RUN INTO IT AND THE PROBLEMS IT CAUSES...
MODULES SHOULD BE CODED IN A HIERARCHICAL MANNER -
. MAIN MODULES WRITTEN FIRST
. SECONDARY MODULES PERFORMING SPECIFIC DETAILED
FUNCTIONS.
. CODING MODULES IN A HIERARCHICAL MANNER IS CALLED
TOP DOWN PROGRAMMING
. THE TOP DOWN APPROACH IS SOMETIMES CALLED STEPWISE REFINEMENT OR FUNCTIONAL DECOMPOSITION.
1
THE NOTION OF MODULARITY AND ABSTRACTION
IF AM1-1 = AMT-2
.... <------DETAILS
....
ELSE
.... <------DETAILS
....
END-IF
MODULAR APPROACH:
IF AMT-1 = AMT-2
PERFORM 1000-EQUAL<== AN ‘ABSTRACTION’
ELSE
PERFORM 2000-UNEQUAL<== AN ‘ABSTRACTION’
END-IF
WE ARE ‘ABSTRACTING’ OUT THE DETAILS.
MAIN FOCUS HERE IS TO NOTE THE CONTROL!!
LEAVE THE DETAILS (AS PERFORMED IN CITED PARAGRAPHS)
UNTIL ‘THOSE’ PARAGRAPHS ARE DEVELOPED.
MAIN MODULE FOCUS ON PRIMARY ISSUE:
ARE THE TWO FIELDS ARE EQUAL.
HERE, MAIN MODULE FOCUS ON CONTROL
THE DETAILS OF WHAT TO DO IF UNEQUAL OR UNEQUAL ARE ABSTRACT AND NOT INCLUDED HERE, WHERE THEY MIGHT OBSCURE THE REAL FUNCTIONS OF THE IF AND CONTROL.
PRINCIPLE OF ABSTRACTION IS VERY IMPORTANT IN ANY PROGRAMMING LANGUAGE.
BUT KNOWING THE LANGUAGE IS NECESSARY, BUT NOT ENOUGH.!
SYNTAX IS NOT ENOUGH.
PROGRAM DESIGN TECHNIQUESTRANSCENDLANGUAGE CONSTRUCTS
1
LOGICAL CONTROL STRUCTURES:
THE SEQUENCE
ONE STATEMENT AFTER ANOTHER
THE SELECTION
EXECUTE THIS STATEMENT OR THAT STATEMENT
DEPENDING ON THE EVALUATIN OF A ‘CONDITION.’
THE ITERATION
REPEATEDLY EXECUTE THIS STATEMENT (UNTIL SOME ‘CONDITION’ BECOMES TRUE OR FALSE)
(OTHERS: THE CASE....)
WELL-DESIGNED PROGRAMS CAN BE DESIGNED AND IMPLEMENTED IN CODE USING ONLY THESE THREE LOGICAL CONTROL STRUCTURES.
THESE THREE CONSTRUCTS: LANGUAGE INDEPENDENT
SO, HOW DO WE MODEL OUR ALGORITHMS USING LANGUAGE INDEPENDENT MEANS?
DESIGN TOOLS:
1. PROGRAM FLOWCHARTS
2. PSEUDOCODE
3. STRUCTURE CHARTS (HIERARCHY CHARTS)(AS STATED BEFORE)
BUT NOW, IN MORE DETAIL....
1
- A PROGRAMFLOWCHART IS A DIAGRAM OR PICTORIAL REPRESENTATION OF THE INSTRUCTIONS AND LOGICAL CONTROL STRUCTURES THAT WILL BE USED IN THE PROGRAM.
- GRAPHICS-BASED TOOL FOR ILLUSTRATING YOUR ALGORITHM
- PSEUDOCODE IS A SET OF STATEMENTS THAT SPECIFIES THE INSTRUCTIONS AND LOGICAL CONTROL STRUCTURES THAT WILL BE USED IN A PROGRAM.
PSEUDOCODE IS A TEXT-BASED TOOL USED TO ILLUSTRATE THE ALGORITHM.
FLOWCHARTING CONVENTIONS:
1.. EACH SYMBOL DENOTES A TYPE OF OPERATION
2..A NOTE IS WRITTEN INSIDE EACH SYMBOL TO INDICATE THE SPECIFIC FUNCTION TO BE PERFORMED.
3..THE SYMBOLS ARE CONNECTED BY FLOWLINES
4..FLOWCHARTS ARE DRAWN AND READ FROM TOP TO BOTTOM UNLESS A SPECIFIC CONDITION IS MET THAT ALTERS THE PATH.
5. A SEQUENCE OF OPERATIONS IS PERFORMED UNTIL A TERMINAL SYMBOL DESIGNATES THE SEQUENCE'S END OR THE END OF THE PROGRAM.
6. SOMETIMES SEVERAL STEPS OR STATEMENTS ARE COMBINED IN A SINGLE PROCESSING SYMBOL FOR EASE OF READING.
REMEMBER: A FLOWCHART REPRESENTS AN ALGORITHM – AN APPROACH TO SOLVING A PROBLEM.
STATEMENTS IN YOUR ALGORITHM SHOULD NOT BE IN 1-1
CORRESPONDENCE WITH YOUR ULTIMATE PROGRAM
INSTRUCTIONS !!!!!!
1
1
EXPANSION OF THE PERFORM:
SEQUENCE OF STATEMENTS
FLOWCHARTS - OLDER TECHNIQUE
TRIED AND TRUE
VISUAL AND EASY TO WORK WITH
IN GENERAL, MORE DETAILED THAN PSEUDOCODE
GREAT FOR SHOWING COMPLICATED LOGIC...
STRUCTURED CODING IS MORE SIMILAR TO PSEUDOCODE.
1
PSEUDOCODE
. DESIGNED SPECIFICALLY FOR REPRESENTING LOGIC IN A
STRUCTURED PROGRAM.
. HAS INDENTATION AND STRUCTURE DESIGNED
TO SUPPORT IMPLEMENTING THE PROGRAM
LOGIC IN A STRUCTURED LANGUAGE.
. CAN ABBREVIATE AND NOT SHOW ALL PROCESSING DETAILS
. IS A LANGUAGE-INDEPENDENT TOOL
. LOGICAL CONTROL CONSTRUCTS ARE EASILY SPECIFIED
USING PSEUDOCODE
(SEQUENCE, SELECTION, ITERATION, THAT IS…)
CONSIDER: the PSEUDOCODE FOR IN-LINE PERFORM
PERFORM
....
...<----STATEMENTS
....
ENDPERFORM
. CALLED THE INLINE PERFORM SINCE ALL INSTRUCTIONS APPEAR DIRECTLY AFTER THE WORD PERFORM.
THEY ARE … ‘IN-LINE.’
I’D RATHER YOUR IN-LINE PSEUDOCODE BE:
LOOP
….
….
ENDLOOP
WHICH MAKES THIS QUITE LANGUAGE INDEPENDENT AND THUS CAN BE IMPLEMENTED IN ANY LANGUAGE READILY.
1
STANDARD PSEUDOCODE RESEMBLES COBOL-85 EXTREMELY CLOSELY
STANDARD PSEUDOCODE HAS THE SCOPE TERMINATORS IN IT.
TO MAKE THE STANDARD PSEUDOCODE LESS COBOL-LIKE,
AVOID USING WORDS LIKE PERFORM…
USE LOOP OR
DO OR EVEN
CALL.
USING WORDS SUCH AS PERFORM IN YOUR PSEUDO-CODE IMPLIES A COBOL IMPLEMENTATION.
But PSEUDO-CODE SHOULD NOT CONSTRAIN A LANGUAGE IMPLEMENTATION OF THE DESIGN.
HENCE THE USE OF ‘LOOP’ OR ‘DO’, ETC.
1
STRUCTURED PSEUDOCODE
IN LIEU OF THE INLINE PERFORMS, WE CAN USE STRUCTURED PSEUDOCODE, WHERE THE INLINE PERFORMS ARE ABSTRACTED OUT TO A SEPARATE MODULE....RATHER THAN
HAVING THE PSEUDOCODE ‘IN-LINE.’
EXAMPLE:
START
OPEN EMPLOYEE FILE
READ EMPLOYEE FILE RECORD
....
COMPUTE ....
DO PRINT MODULE
MORE COBOL CODE....
STOP
PRINT MODULEPERFORMED MODULES EXECUTED
WRITE ....ELSEWHERE.
WRITE ...
WRITE...
RETURN
YOUR TEXT MAKES A BIG DEAL OF STANDARD PESUDO-CODE
VERSUS STRUCTURED PSEUDO-CODE.
ONLY THE BASIC DIFFERENCES ARE NECESSARY.
STANDARD PSEUDOCODE AND COBOL 85 ARE VERY CLOSE.
STRUCTURED PSEUDOCODE CAN BE REALIZED IN EITHER COBOL 85, 2002, OR, OF COURSE, THE OLDER VERSIONS
1
CONTROL STRUCTURES.
WE KNOW WHAT SEQUENCE IS.
MERELY THE EXECUTION OF IMPERATIVE STATEMENTS OR STATEMENTS THAT ARE CONSIDERED SEQUENTIAL.
NO TRANSFERS OF CONTROL
SELECTION:
SELECTION IS A LOGICAL CONTROL CONSTRUCT THAT EXECUTES INSTRUCTIONS DEPENDING UPON THE TRUTH VALUE OF A CONDITION.
SOMETIMES CALLED: THE IFTHENELSE.
IN COBOL:
IF (CONDITION)
...
...<---- INDICATES ACTIONS IF CONDITION IS TRUE
ELSE
...
... <--- INDICATES ACTIONS IF CONDITION IS FALSE END-IF
1
CONSIDER: COBOL 74 CODE:
IF AMT < ZERO
ADD 1 TO ERR-COUNTER
ELSE
WRITE NEW-RECORD.(NOTE: PERIOD ENDS IF)
COBOL 85 AND NEWER CODE:
IF AMT < ZERO
ADD 1 TO ERR-COUNTER
ELSE
WRITE NEW-RECORD
END-IF.(NOTE: SCOPE TERMINATOR)
PSEUDOCODE FORMAT:
IF (CONDITION)EXAMPLE:
THENIF AMT IS LESS THAN ZERO
.....THEN
.... ADD 1 TO ERROR COUNTER
ELSEELSE
.... WRITE A NEW RECORD
....ENDIF
ENDIF
(NO HYPHENS; SPELL OPERATORS; ENGLISH-LIKE)
1
ITERATION
. NEXT LOGICAL CONTROL STRUCTURE...
. IN COBOL, IMPLEMENTED AS A PERFORM...UNTIL
. E.G., PERFORM 200-CALC-RTN
UNTIL EOF = 1.
(STRUCTURED; TRADITIONAL)
OR, PERFORM UNTIL EOF=1
…..
END-PERFORM.
(STANDARD…)
IN EITHER EVENT, WE ARE ‘LOOPING’
WE ARE EXECUTING CODE OVER AND OVER UNTIL SOME CONDITION
BECOMES TRUE.
BOOK:
EXAMPLE OF STANDARD PSEUDOCODE FOR INLINE PERFORM:
PERFORM UNTIL condition
......
ENDPERFORM
EXAMPLE OF STRUCTURED PSEUDOCDE FOR PERFORM:
100 MAIN MODULE
....
PERFORM 200 CALCULATE SALARY
UNTIL END OF FILE REACHED
.....
200 CALCULATE SALARY
....
.... <------INSTRUCTIONS TO BE PERFORMED
RETURN
1
I PREFER:
LOOP CALCULATE SALARY REPEATEDLY
UNTIL END OF FILE IS REACHED.
LOOP UNTIL END OF FILE IS REACHED
….
….
END LOOP
REMEMBER: PSEUDO-CODE SHOULD BE LANGUAGE
INDEPENDENT AND SHOULD THUS NOT CONSTRAIN A LANGUAGE IMPLEMENTATION (SOLUTION) OF YOUR ALGORITHM (DESIGN).
THE INFINITE LOOP:
AN ERROR TO BE AVOIDED.
1.. HOW TO GET THEM:
NO TERMINATING CONDITION FOR LOOP
GET PRIMING READ OKAY,
FORGET READ AS LAST STATEMENT OF PERFORMED PARAGRAPH
2. SAY, PERFORM UNTIL COUNT > 10.
INITIALIZE COUNT BEFORE LOOP, BUT DON'T INCREMENT COUNT WITHIN LOOP.
3. REINITIALIZE CONTROL VARIABLE WITHIN BODY OF LOOP EACH ITERATION.
CAN YOU PROVIDE AN EXAMPLE OF EACH????
THE CASE STRUCTURE:
THE CASE STRUCTURE IS A SPECIAL LOGICAL CONTROL STRUCTURE USED WHEN THERE ARE SEVERAL PATHS TO BE FOLLOWED DEPENDING ON THE CONTENTS OF A GIVEN FIELD.
EXAMPLE:
AN INPUT CODE CAN RESULT IN ANY OF SEVERAL ACTIONS TO BE TAKEN:
FOR EXAMPLE,
IF CODE = 1, PERFORM 2000-UPDATE,
IF CODE = 2, PERFORM 3000-NEW-HIRE;
IF CODE = 3, PERFORM 4000-TERMINATE;
IF CODE IS NOT 2, 3, OR 4, PERFORM 5000-ERROR
BETTER: USE THE COBOL EVALUATE VERB: (COBOL-85)
1
EVALUATE UPDATE-CODE (AN 88 LEVEL???)
WHEN 1
PERFORM 2000-UPDATE
WHEN 2
PERFORM 3000-NEW-HIRE
WHEN 3
PERFORM 4000-TERMINATE
WHEN OTHER
PERFORM 5000-ERROR
END-EVALUATE
. NOTE THE ABSTRACTION (AS IMPLEMENTED)
. GREAT FOR DESIGNING AND PROCESSING MENUS
. GREAT FOR VALIDATION OF ACCEPTABLE VALUES
FOR VARIABLES READ INTO THE PROGRAM
SO WHAT WOULD THE PSEUDO-CODE LOOK LIKE?
STRUCTURED PSEUDOCODE:
CASE UPDATE CODE
WHEN 1
DO UPDATE
WHEN 2
DO NEW-HIRE
WHEN 3
DO TERMINATE ROUTINE
WHEN OTHER
DO ERROR ROUTINE
ENDCASE
UPDATE
...
NEW-HIRE
...
TERMINATE
...
ERROR
...
1
STANDARDPSEUDOCODE
CASE UPDATE-CODE
WHEN 1
DO
SALARY UPDATE RTNE
ENDDO
WHEN 2
DO
NEW HIRE PROCEDURE
ENDDO
WHEN 3
DO
TERMINATE PROCEDURE
ENDDO
ENDCASE
NOTE:
. NO HYPHENS
. ALL LOGIC IN LINE
. MODULAR
1
FLOWCHARTS AND PSEUDOCODE - RULES REVIEW:
FLOWCHART RULES:
1. A FLOWCHART IS DRAWN AND READ FROM TOP TO BOTTOM UNLESSA SPECIFIC CONDITION ALTERS THE PATH.
2. DIFFERENT SYMBOLS ARE USED TO DENOTE DIFFERENT FUNCTIONS
3. ALL SYMBOLS HAVE EXPLANATORY NOTES INDICATING THE SPECIFIC OPERATIONS TO BE PERFORMED.
THE SYMBOL ITSELF DENOTES THE TYPE OF OPERATION SUCH AS INPUT/OUTPUT OR PROCESSING, AND A NOTE WITHIN THE SYMBOL DESCRIBES SPECIFIC OPERATION TO BE PERFORMED SUCH AS READ A RECORD OR ADD AMOUNT TO TOTAL.
PSEUDOCODE RULES:
1. PSEUDOCODE IS WRITTEN AND READ FROM TOP TO BOTTOM
2. THE LOGICAL CONTROL STRUCTURE OF PSEUDOCODE IS DEFINED WITH THE USE OF KEY TERMS SUCH AS (BOOK PERFORM...ENDPERFORM, PLEASE USE DO…ENDDO OR
LOOP…END LOOP OR
CALL….RETURN,
IFTHENELSE,...ENDIF, AND
CASE ...ENDCASE.
ALL HAVE SCOPE TERMINATORS
NO HYPHENS OR LANGUAGE-DEPENDENT FEATURES
HAS A ‘THEN’
3. THE OPERATIONS TO BE EXECUTED WITHIN A LOOP,
IFTHENELSE OR CASE CAN BE CODED IN SEQUENCE OR IN A
EPARATE MODULE.
1
HEURISTICS:
STRUCTURED PSEUDOCODE (SEPARATE MODULES)
BETTER FOR DETAILED OR COMPLEX SETS OF INSTRUCTIONS.
BETTER TO COMMUNICATE (THEREFORE) ABSTRACTION
OFTEN CLEANER
TRADITIONAL WAY TO CODE (OLDER; MAINTENANCE!)
CORRESPONDS TO STRUCTURE CHARTS MUCH MORE READILY THAN STANDARD PSEUDOCODE.
STANDARD PSEUDOCODE
STANDARDPSEUDOCODE (WITH INLINE PERFORMS) IS PREFERRED.
BETTER FOR LESS COMPLICATED LOGIC
BETTER FOR FEWER NUMBERS OF STATEMENTS
NO TRANSFERS OF CONTROL
ALL LOGIC IS CLEARLY VISIBLE
1
HIERARCHY CHARTS FOR TOP DOWN PROGRAMMING
FLOWCHARTS AND PSEUDOCODE USED TO MODEL OUR DESIGN USING THE STRUCTURED DESIGN PARADIGM.
USED FOR DETAIL DESIGN ONCE MODULES ESTABLISHED....
ARCHITECTURAL DESIGN (PRELIMINARY DESIGN)
. BUILDING BLOCKS
. A HIERARCHY OR STRUCTURE CHART PROVIDES A
GRAPHICAL METHOD FOR SEGMENTING A PROGRAM
INTO MODULES.
. ITS MAIN PURPOSE IS TO PROVIDE A VISUAL/GRAPHICAL
OVERVIEW OF THE RELATIONSHIPS AMONG
MODULES IN A PROGRAM.
. A MODULE IS A WELL-DEFINED SEGMENT THAT PERFORMS
A SPECIFIC FUNCTION.
MODULES MAY BE:
HEADING ROUTINES
COMPUTATIONAL ROUTINES
ROUTINES THAT DO NOTHING BUT ACONTROL@
PRINT ROUTINES
COMMUNICATION ROUTINES, ETC.
COMMON MODULES?? SHOW.
WELL-WRITTEN PROGRAMS MAY WELL HAVE A MIXTURE OF BOTH STRUCTURED CODE AND STANDARD.
ARCHITECTURAL DESIGN IS FOLLOWED BY DETAIL DESIGN.
DRIVERS AND STUBS
GIVEN A STRUCTURE CHART (INFERRED FROM CODE BELOW…)
CAN SAY: (DISCUSS!)
0000-MAIN.
...
PERFORM 1000-FUNCT
PERFORM 2000-FUNCT
PERFORM 3000-FUNCT
PERFORM 4000-FUNCT.
1000-FUNCT.
DISPLAY “GOT TO 1000-FUNCTION”.
2000-FUNCT.
PERFORM 2100-FUNCT
PERFORM 2300-FUNCT
PERFORM 2500-FUNCT.
3000-FUNCT.
DISPLAY “GOT TO 3000-FUNCTION”.
4000-FUNCT.
DISPLAY “GOT TO 4000-FUNCTION”.
2100-FUNCT.
DISPLAY “GOT TO 2100 FUNCTION”.
2300-FUNCT.
DISPLAY “GOT TO 2300 FUNCTION”.
2500-FUNCT.
...ETC.
1
DISCUSS DRIVERS AND STUBS.
ITERATIVE DEVLEOPMENT; STEPWISE VERIFICATION; ISOLATION OF
PROBLEMS… DIVIDE AND CONQUER!!
NES...
1