| adapted from http://www.csc.calpoly.edu/~jdalbey/SWE/pdl_std.html | ||
Pseudocode Standard
|
||
How to write pseudocodePseudocode is a kind of structured english for describing algorithms. It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax. At the same time, the pseudocode needs to be complete. It describe the entire logic of the algorithm so that implementation becomes a rote mechanical task of translating line by line into source code. In general the vocabulary used in the pseudocode should be the vocabulary of the problem domain, not of the implementation domain. The pseudocode is a narrative for someone who knows the requirements (problem domain) and is trying to learn how the solution is organized. E.g.,
Note that the logic must be decomposed to the level of a single loop or decision. Thus "Search the list and find the customer with highest balance" is too vague because it takes a loop AND a nested decision to implement it. It's okay to use "Find" or "Lookup" if there's a predefined function for it such as String.indexOf(). Each textbook and each individual designer may have their own personal style of pseudocode. Pseudocode is not a rigorous notation, since it is read by other people, not by the computer. There is no universal "standard" for the industry, but for instructional purposes it is helpful if we all follow a similar style. The format below is recommended for expressing your solutions in our class. The "structured" part of pseudocode is a notation for representing six specific structured programming constructs: SEQUENCE, WHILE, IF-THEN-ELSE, REPEAT-UNTIL, FOR, and CASE. Each of these constructs can be embedded inside any other construct. These constructs represent the logic, or flow of control in an algorithm. It has been proven that three basic constructs for flow of control are sufficient to implement any "proper" algorithm.
A set of useful constructsSEQUENCE Sequential control is indicated by writing one action after another, each action on a line by itself, and all actions aligned with the same indent. The actions are performed in the sequence (top to bottom) that they are written. Example (non-computer)
Example
Common Action Keywords
IF-THEN-ELSE Binary choice on a given Boolean condition is indicated by the use of four keywords: IF, THEN, ELSE, and ENDIF. The general form is:
The ELSE keyword and "sequence 2" are optional. If the condition is true, sequence 1 is performed, otherwise sequence 2 is performed. Example
WHILE The WHILE construct is used to specify a loop with a test at the top. The beginning and ending of the loop are indicated by two keywords WHILE and ENDWHILE. The general form is:
The loop is entered only if the condition is true. The "sequence" is performed for each iteration. At the conclusion of each iteration, the condition is evaluated and the loop continues as long as the condition is true. Example
Example
CASE A CASE construct indicates a multiway branch based on conditions that are mutually exclusive. Four keywords, CASE, OF, OTHERS, and ENDCASE, and conditions are used to indicate the various alternatives. The general form is:
indicating the value of "expression", but they can be English statements or some other notation that specifies the condition under which the given sequence is to be performed. A certain sequence may be associated with more than one condition. Example
CASE Title OF Example
CASE grade OF REPEAT-UNTIL This loop is similar to the WHILE loop except that the test is performed at the bottom of the loop instead of at the top. Two keywords, REPEAT and UNTIL are used. The general form is:
The "sequence" in this type of loop is always performed at least once,
because the test is peformed after the sequence is executed. At the
conclusion
of each iteration, the condition is evaluated, and the loop repeats if
the condition is false. The loop terminates when the condition becomes
true.
FOR This loop is a specialized construct for iterating a specific number of times, often called a "counting" loop. Two keywords, FOR and ENDFOR are used. The general form is:
In cases where the loop constraints can be obviously inferred it is best to describe the loop using problem domain vocabulary. Example
The constructs can be embedded within each other, and this is made clear by use of indenting. Nested constructs should be clearly indented from their surrounding constructs. Example
In the above example, the IF construct is nested within the REPEAT
construct,
and therefore is indented.
INVOKING SUBPROCEDURES Use the CALL keyword. For example:
Sample Pseudocode"Adequate" "Better"
Set moveCount to 1
FOR all the number at the back of the array
"Good Enough (not perfect)" SET Carry to 0
COMPUTE Total as sum of
FirstNum[DigitPosition] and
SecondNum[DigitPosition] and Carry
IF Total > 10 THEN
STORE Total in Result[DigitPosition] END LOOP
IF Carry = 1 THEN
"Pretty Good" This example shows how pseudocode is written as comments in the source file. Note that the double slashes are indented. public boolean moveRobot (Robot aRobot)
Example Java Implementation
public boolean moveRobot (Robot aRobot)
|