PSEUDOCODE
Last Update October 27, 2006 17:38 by G. Grant
Pseudocode is a structured form of English that is used to describe algorithms. It allows designers to concentrate on the logic of the algorithm. Although pseudocode resembles some high level programming languages, it should be independent of any specific programming language. You could think of it as generic code that can be translated to any programming language. Pseudocode is easy to write and read. It lets the program designer concentrate on the logic of a problem without the distraction of conforming to the syntax of any specific programming languge. Algorithms in pseudocode should completely describe the logic for the specific program/function/method/procedure. They should also clearly state any formulas that are part of the algoritm. Another programmer should be able to implement the algorithms without having to consult the designer. It is not necessary to have knowledge of any specific programming language to write pseudocode. An individual who understands the problem, but does not necessarily understand programming should be able to read and understand the pseudocode. Textbooks, designers, and instructors have different styles 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 software developers, instructors and classes often will agree on a convention that they will all use in order to be consistent. Students in CIS290 and CIS254 should follow the conventions shown here. |
Program control flow can be reduced to three basic control structures. SEQUENCE is a series
of processing steps where one step follows the previous. SELECTION is the presentation of a condition and a choice between two actions, the choice depends on whether the condition is true or false.The condition most often involves comparing two variables. REPETITION a structure where a sequence of instructions are performed continually as long as a condition is true. |
Some terms to use in Pseudocode
|
|
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) in which they are written. Note that the word begin and end indicate the beginning and end of the block of code. A label startTheDay is used to differentiate the block of code from other blocks of code. It may be the begin and end of a specific program, function, method, or procedure. General form for the sequence control structure
Examples:
Note that the block of statements between BEGIN and END is indented. |
|
IF-THEN Binary choice on a given Boolean condition is indicated by the use of four keywords: IF, THEN,and ENDIF. The general form is:
Note that the code within the if structure is indented, more than one line of code can be contained in the body of the if statement Example
IF-THEN-ELSE Binary choice for a given Boolean condition is indicated by the use of four keywords: IF, THEN, ELSE, ENDIF or ENDIFELSE. The general form is:
Example
Switch or Case Statement ( another selection structure) A CASE structure is a multiway branch based on conditions that are mutually exclusive. The singleVariable must always be a discrete ordinal type such as integer or char Four keywords, CASE, OF, OTHER, and ENDCASE, and conditions are used to indicate the various alternatives. The general form is:
Example:
|
|
WHILE The WHILE construct is used to specify a loop with
a condition or test at the beginning of the loop. It is sometimes referred
to as a Pre-Test loop. The loop is executed as long as the condition is
evaluated to true. The general form is: initializer WHILE
condition statementBlock ENDWHILE Example:
numberOfStudents = 0; WHILE numberOfStudents
< 35 accept testScore ENDWHILE Note: DO-WHILE
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.
The DO-WHILE loop is sometimes called a Post-test loop.
Two keywords, DO
and WHILE are used to specify this
loop. In some languages this structure uses the key word REPEAT instead
of DO, and UNTIL instead of WHILE and is called a REPEAT-UNTIL structure.
The general form is: DO statementBlock WHILE condition
Note:
Example: DO accept input from user WHILE input
is not blank FOR loop This loop is a specialized control structure for
iterating a specific number of times. It is often called a "counting"
loop. Two keywords, FOR and ENDFOR are used. The heading or first line
of the for structure includes an initialization statement, a test condition,
and an update.
The general form is: FOR (initialization,
test condition, updater) statementBlock ENDFOR Example FOR (month
= 1; month <= 12; month+1) display monthName ENDFOR or FOR month
= 1 through 12 display monthName ENDFOR The control structures can be embedded within each
other, and this is made clear by use of the key words and indenting. Example SET totalFreezeDays to zero READ Temperature INCREMENT totalFreezeDays END IF
WHILE Temperature
< zero |
| Reference: Simple Program Design by Lesley Anne Robertson |