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 imperatives. 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:
1.. If student's grade is greater than or equal to 60
Print "passed"
else
Print "failed"
2. Set total to zero
Set grade counter to one
While grade counter is less than or equal to ten
Input the next grade
Add the grade into the total
Set the class average to the total divided by ten
Print the class average.
3.
Initialize total to zero
Initialize counter to zero
Input the first grade
while the user has not as yet entered the sentinel
add this grade into the running total
add one to the grade counter
input the next grade (possibly the sentinel)
if the counter is not equal to zero
set the average to the total divided by the counter
print the average
else
print 'no grades were entered'
4.
initialize passes to zero
initialize failures to zero
initialize student to one
while student counter is less than or equal to ten
input the next exam result
if the student passed
add one to passes
else
add one to failures
add one to student counter
print the number of passes
print the number of failures
if eight or more students passed
print "raise tuition"
Some Keywords That Should be Used
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.
Pseudocode
Definition - What does Pseudocode mean?
Pseudocode is an informal program description that does not contain code syntax or underlying
technology considerations. Pseudocode summarizes a program’s steps (or flow) but excludes
underlying details.
[WEBINAR] How to Optimize Use of the Hybrid Cloud
Techopedia explains Pseudocode
To be pseudo is to be fake. In other words, something that is pseudo is pretending that to be
something it's not. Given this, the term pseudocode makes sense -- it isn't code, but it is the
starting point to what the code should look like.
System designers write pseudocode to ensure that programmers understand a software project's
requirements and align code accordingly. It can be anything from a few scribbles on a piece of
paper, to detailed designs using a modeling language.
Structured English is native English language used to write the structure of a program whereas,
Pseudo Code is more close to programming language.
Pseudocode is an informal high-level description of the operating principle of a computer
program or other algorithm. It uses the structural conventions of a programming language, but is
intended for human reading rather than machine reading. On the other hand, Structured English
is a limited form "pseudocode" and consists of the following elements: Operation statements
written as Eng...[ + ]
Structured English is native English language used to write the structure of a program module by
using programming language keywords, whereas, Pseudo Code is more close to programming
language and uses native English language words or sentences to write parts of code.
Programming Tools
During programming analysis and system design, the programmers, system engineers and system
designers work together to build a successful program or a computer-based information system.
Various ideas, components and program modules are integrated to accomplish the task.
System analysts and designers perform the following tasks during these steps.
1. Selection of programming language
2. Selection of the programming tools
3. Design and virtual testing of the program.
The programming tools give the idea how the flow should take place in order to accomplish a
task. Several programming tools are used in this context. Some of the most common
programming tools are:
1. Pseudo code
2. Algorithm
3. Flowchart
Pseudo code
Pseudo code is a combination of two words: Pseudo and Code. 'Pseudo' means imitation and
'code' refer to instruction written in the programming language. Pseudo code is not a real
programming code. It is the generic way of describing an algorithm without using any specific
programming language-related notations.
The features of pseudocode are:
It uses plain english statements.
It emphasizes on the design of the computer program.
It uses structured english.
It is detailed and readable to draw an inference.
It enables the programmers to concentrate on the algorithms.
The pseudo code cannot be compiled. It cannot be executed and there are no real formatting or
syntax rules for writing pseudo codes.
It is simply an important step in producing the final code. Some important terms used in pseudo
code for different activities are:
Input: Read, Obtain, Get and Prompt
Output: Print, Display, and Show
Computer: Computer, Calculate, and Determine
Initialize: Set and Initialize
Add one: Increment
The two major constructions in pseudo code structures are: Sequence and Selection (decision).
Sequence indicates the continuous flow of the program, whereas selection uses logical
comparison or conditional check for making decisions.
Example:
……………
IF (condition> THEN
List of Actions
ELSE
List of Different Actions
END IF
………………
…………………
Example:
READ A, B, AND C
IF A is greater than C
THEN
DISPLAY A
ELSE
DISPLAY C
END IF
ELSE
IF B is greater than C
THEN
DISPLAY B
ELSE
DISPLAY C
END IF
END IF
STOP
Example:
INITIALIZE Count to zero
DO WHILE Count is greater than or equal
to 10
ADD 1 to Count
PRINT Count
ENDDO
STOP
Advantages of Pseudo code:
1. Since, it is language independent, it can be used by most programmers.
2. It helps to design in plain natural language.
3. It is easier to develop the program from pseudo code than with a flowchart.
4. It is easy to translate to the programming language.
5. Its simple structure and readability makes it easier to modify as well.
Disadvantages of Pseudo code:
1. It does not provide the visual representation of the program logic.
2. There are no accepted standards for writing pseudo code.
Algorithm
An algorithm is defined as a finite sequence of explicit instructions that produces an output with
the set of input values. The steps in the algorithm are never ambiguous. It terminates after a finite
number of steps.
Algorithms can have repetitions and logical decisions until a specific task is completed.
Algorithms are not computer programs. They cannot be executed by the computer.
Properties of Algorithm
An algorithm must have the following properties.
1. There should not be any ambiguity in instructions.
2. The instructions should be specific to the task.
3. The description of an algorithm must be finite. It cannot be open-ended.
4. The algorithm must be general enough for writing programs.
5. As far as practicable, program codes should not be included in algorithms.
Example: Write an algorithm to make a telephone call.
Step 1: Remember or read the telephone number.
Step 2: Lift the receiver
Step 3: Is there a dial tone?
If yes, then dial telephone, go to step 4.
{ If no, then put down the receiver.
Go to step 2}
Step 4: Speak
Step 5: Put down the receiver.
Example: Write an algorithm to find the largest number among three input numbers.
Step 1: Start
Step 2: Read 3 numbers say: A, B, C
Step 3: Find the largest number between A and B and store it in
MAXAB
Step 4: Find the largest number between MAXAB and C and store
it in MAX
Step 5: Display MAX
Step 6: Stop
Write the algorithm to calculate the interest on principle amount in N number of Years.
Step 1: Start
Step 2: Read principle, Rate and
Time
Step 3: Multiply principle * Rate *
Time
Step 4: Divide by 100
Step 5: Write the answer
Step 6: Any more calculations?
{ If yes, then go to step 2
If No }
Step 7: Stop
Write algorithm to calculate the total and average marks of 10 students for 7 subjects.
Step 1: Start
Step 2: Subject = 1
Step 3: Input mark for this Subject
Step 4: Add marks and store into a variable
Step 5: Are all subjects (7 subjects) done?
{ If No, then increase the subject number and go to
step 3.
If yes, then go to step 6
Step 6: Print the sum
Step 7: Find average (divide by 7)
Step 8: Print the sum and average
Step 9: Are all (10 students done)?
{ If No, then increase the student number and go to
step 2
If yes, then go to step 9}
Step 9: End
Pseudocode is essentially writing in a high-level language without worrying about the details.
Usually variable and function declarations are left out, the details of certain constructs may be
elided. For example, if you’re writing C-like pseudocode, you may ignore the details of for (),
simply writing something like:
1. for i=1 to j {
2. // code here
3. }
It’s primarily meant as a tool for programmers: a way to start writing out program logic without
immediately getting into the gritty details. Often, though, since programmers dislike waste, the
pseudocode may be placed into files commented out, and fleshed out over time. This avoids
rewriting things that have already been written in pseudocode, and is a reason to keep one’s
pseudocode close to the actual language to be used.
Structured English is meant to be used for program design, particularly when working with non-
programmers. As such, it’s not meant to be close to any particular programming language.
Structured English guides recommend keeping to simple concepts: for example, using loops even
when one might actually prefer to implement with recursion in reality. Blocks of logic are
indicated by indenting, and sometimes by surrounding them with blank lines; things like brackets
or braces are not used. Mathematical notation is generally avoided, favoring descriptive text. To
take an example from Davis and Yen’s Information System Consultant’s Handbook:
1. IF stock-on-hand is less than reorder-point
2. THEN turn on reorder-flag
3. ELSE (stock-on-hand is not less than reorder-point)
4. SO turn off reorder-flag.
Writing this in pseudocode, it would more closely resemble an actual programming language.
For example, for a C++-like pseudocode:
1. if ( item.stockOnHand() < item.reorderPoint() ) {
2. item.setReorderFlag(true);
3. } else {
4. item.setReorderFlag(false);
5. }
The latter might end up being part of the actual code of the system; the former could not.