Pseudocode Examples
Pseudocode Examples
An algorithm is a procedure for solving a problem in terms of the actions to be executed and the
order in which those actions are to be executed. An algorithm is merely the sequence of steps taken to
solve a problem. The steps are normally "sequence," "selection, " "iteration," and a case-type
statement.
In C, "sequence statements" are imperative. The "selection" is the "if then else" statement, and the
iteration is satisfied by a number of statements, such as the "while," " do," and the "for," while the
case-type statement is satisfied by the "switch" statement.
Pseudocode is an artificial and informal language that helps programmers develop algorithms.
Pseudocode is a "text-based" detail (algorithmic) design tool.
The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are
to be indented. These include while, do, for, if, switch. Examples below will illustrate this notion.
Examples:
Print "passed"
else
Print "failed"
3.
else
4.
else
For looping and selection, The keywords that are to be used include Do While...EndDo; Do Until...Enddo;
Case...EndCase; If...Endif; Call ... with (parameters); Call; Return ....; Return; When; Always use scope
terminators for loops and iteration.
As verbs, use the words Generate, Compute, Process, etc. Words such as set, reset, increment, compute,
calculate, add, sum, multiply, ... print, display, input, output, edit, test , etc. with careful indentation tend to
foster desirable pseudocode.
Do not include data declarations in your pseudocode.
Example #1 - Computing Sales Tax : Pseudo-code the task of computing the final price
of an item after figuring in sales tax. Note the three types of instructions: input (get),
process/calculate (=) and output (display)
6. halt
Variables: price of item, sales tax rate, sales tax, final price
Note that the operations are numbered and each operation is unambiguous and effectively
computable. We also extract and list all variables used in our pseudo-code. This will be
useful when translating pseudo-code into a programming language
Example #2 - Computing Weekly Wages: Gross pay depends on the pay rate and the
number of hours worked per week. However, if you work more than 40 hours, you get
paid time-and-a-half for all hours worked over 40. Pseudo-code the task of computing
gross pay given pay rate and hours worked.
4. else
4.1 gross pay = pay rate times 40 plus 1.5 times pay rate times
(hours worked minus 40)
6. halt
This example introduces the conditional control structure. On the basis of the true/false
question asked in line 3, we execute line 3.1 if the answer is True; otherwise if the answer
is False we execute the lines subordinate to line 4 (i.e. line 4.1). In both cases we resume
the pseudo-code at line 5.
2. sum = 0
3. count = 0
6. display average
7. halt
This example introduces an iterative control statement. As long as the condition in line 4
is True, we execute the subordinate operations 4.1 - 4.3. When the condition becomes
False, we resume the pseudo-code at line 5.