How To Write A Pseudo Code
How To Write A Pseudo Code
Pseudo code is a term which is often used in programming and algorithm based
fields. It is a methodology that allows the programmer to represent the
implementation of an algorithm. Simply, we can say that it’s the cooked up
representation of an algorithm. Often at times, algorithms are represented with the
help of pseudo codes as they can be interpreted by programmers no matter what
their programming background or knowledge is. Pseudo code, as the name
suggests, is a false code or a representation of code which can be understood by
even a layman with some school level programming knowledge.
Algorithm: It’s an organized logical sequence of the actions or the approach
towards a particular problem. A programmer implements an algorithm to solve a
problem. Algorithms are expressed using natural verbal but somewhat technical
annotations.
Pseudo code: It’s simply an implementation of an algorithm in the form of
annotations and informative text written in plain English. It has no syntax like any of
the programming language and thus can’t be compiled or interpreted by the
computer.
Advantages of Pseudocode
Improves the readability of any approach. It’s one of the best approaches
to start implementation of an algorithm.
Acts as a bridge between the program and the algorithm or flowchart.
Also works as a rough documentation, so the program of one developer
can be understood easily when a pseudo code is written out. In
industries, the approach of documentation is essential. And that’s where
a pseudo-code proves vital.
The main goal of a pseudo code is to explain what exactly each line of a
program should do, hence making the code construction phase easier for
the programmer.
How to write a Pseudo-code?
Example:
This program will allow the user to check the number
whether it's even or odd.
3. The way the if-else, for, while loops are indented in a program, indent the
statements likewise, as it helps to comprehend the decision control and
execution mechanism. They also improve the readability to a great
extent.
Example:
if "1"
print response
"I am case 1"
if "2"
print response
"I am case 2"
4. Use appropriate naming conventions. The human tendency follows the
approach to follow what we see. If a programmer goes through a pseudo
code, his approach will be the same as per it, so the naming must be
simple and distinct.
5. Use appropriate sentence casings, such as CamelCase for methods,
upper case for constants and lower case for variables.
6. Elaborate everything which is going to happen in the actual code. Don’t
make the pseudo code abstract.
7. Use standard programming structures such as ‘if-then’, ‘for’, ‘while’,
‘cases’ the way we use it in programming.
8. Check whether all the sections of a pseudo code is complete, finite and
clear to understand and comprehend.
9. Don’t write the pseudo code in a complete programmatic manner. It is
necessary to be simple to understand even for a layman or client, hence
don’t incorporate too many technical terms.
So What is "Pseudocode"?
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.
Pseudocode is a useful thing to learn because it focuses on the building block
concepts of programming languages without you having to worry about whether you
are using the "right" words. Once you've nailed the logic of coding, the rest is just
filling in your pseudocode with the syntax of a particular programming language. In a
lot of ways, pseudocode is similar to the mockups you created in the Design mini-
course: it basically scaffolds the solution you'll be implementing later.
PART 2: STATEMENTS
When writing pseudocode, we assume that the order of execution of the statements is
from top to bottom. This changes when using control structures, functions and
exception handling.
Mathematical operations
Assignment: ← or :=
Example: c ← 2πr, c := 2πr
Arithmetic: +, −, ×, /, mod
Logical: and, or
Sums, products: Σ Π
Example: h ← Σa∈A 1/a
Keywords
A keyword is a word that is reserved by a program because the word has a special
meaning. Keywords can be commands or parameters. Every programming language
has its own keywords (reserved words). Keywords cannot be used as variable names.
PART 3: CONDITIONALS
During algorithm development, we need statements which evaluate expressions and
execute instructions depending on whether the expression evaluated to True or False.
Here are some common conditions used in Pseudocode:
IF — ELSE IF — ELSE
CASE
Case structures are used if we want to compare a single variable against several
conditions.
INPUT color
CASE color of
red: PRINT "red"
green: PRINT "green"
blue: PRINT "blue"
OTHERS
PRINT "Please enter a value color"
ENDCASE
The OTHERS clause with its statement is optional. Conditions are normally numbers
or characters
PART 4: ITERATION
To iterate is to repeat a set of instructions in order to generate a sequence of
outcomes. We iterate so that we can achieve a certain goal.
FOR structure
The FOR loop takes a group of elements and runs the code within the loop for each
element.
FOR every month in a year
Compute number of days
ENDFOR
WHILE structure
Similar to the FOR loop, the while loop is a way to repeat a block of code as long as a
predefined condition remains true. Unlike the FOR loop, the while loop evaluates
based on how long the condition will remain true.
To avoid a scenario where our while loop runs infinitely, we add an operation to
manipulate the value within each iteration. This can be through an increment,
decrement, et cetera.
PRECONDITION: variable X is equal to 1
WHILE Population < Limit
Compute Population as Population + Births — Deaths
ENDWHILE
PART 5: FUNCTIONS
When solving advanced tasks it is necessary to break down the concepts in block of
statements in different locations. This is especially true when the statements in
question serve a particular purpose. To reuse this code, we create functions. We can
then call these functions every-time we need them to run.
Function clear monitor
Pass In: nothing
Direct the operating system to clear the monitor
Pass Out: nothing
Endfunction
After writing several functions in our pseudocode, we find the need to wrap
everything into one container. This is to improve readability and make the execution
flow easier to understand.
An exception is an event which occurs during program execution that disrupts the
normal flow of the instructions. These are events that are non-desirable.
We need to observe such events and execute code-blocks in response to them. This is
called exception handling.
BEGIN
statements
EXCEPTION
WHEN exception type
statements to handle exception
WHEN another exception type
statements to handle exception
END
PART 8: CONCLUSION
There are no technical rules for Pseudocode. It is meant to be human readable and
still convey meaning and flow.
There are different guide and tutorials which lean more towards language-specific
pseudocode, examples of such are Fortran style pseudo code, Pascal style pseudo
code, C style pseudo code and Structured Basic style pseudo code.