Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
820 views

How To Write A Pseudo Code

Pseudocode is an informal high-level description of an algorithm or computer program that uses the structural elements of a programming language without adhering to full syntax. It allows programmers to design and plan programs without focusing on specific programming language details. The document provides examples of how to write pseudocode using statements, conditionals like IF/ELSE statements, iteration like FOR loops, and functions to break code into reusable blocks. Pseudocode aims to explain the logic of a program in a simple way that is understandable to anyone regardless of programming experience.

Uploaded by

joni diaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
820 views

How To Write A Pseudo Code

Pseudocode is an informal high-level description of an algorithm or computer program that uses the structural elements of a programming language without adhering to full syntax. It allows programmers to design and plan programs without focusing on specific programming language details. The document provides examples of how to write pseudocode using statements, conditionals like IF/ELSE statements, iteration like FOR loops, and functions to break code into reusable blocks. Pseudocode aims to explain the logic of a program in a simple way that is understandable to anyone regardless of programming experience.

Uploaded by

joni diaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

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?

1. Arrange the sequence of tasks and write the pseudocode accordingly.


2. Start with the statement of a pseudo code which establishes the main
goal or the aim.

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.

explain how a vending machine program worked to a general audience.

WHEN the user inputs money:


IF the bill is too crumpled to read,
provide an error message,
and return the bill.
ELSE,
Add it to the balance
WHEN the user selects an item:
IF they haven't put in enough money,
ask for more.
IF the item is out of stock,
ask them to make a new selection.
ELSE,
determine change,
dispense change,
zero out the balance,
and dispense the item.
WHEN the user hits the "cancel" button:
IF the user has input money without
making a purchase,
return the money,
and zero out the balance
What is Pseudocode?

Pseudocode is an informal high-level description of a computer program or


algorithm. It is written in symbolic code which must be translated into a
programming language before it can be executed.

Are there alternatives to Pseudocode?

There are some alternatives to Pseudocode. Some of them are Flowcharts, drakon-


charts and Unified Modified Language (UML) charts. They will serve the purpose but
they comparatively require more resources.

PART 2: STATEMENTS

A statement is defined as an instruction that directs the computer to perform a


specific action. In writing pseudocode, we will refer to singular instructions as
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

Mathematical operations are integral to solution development. They allow us to


manipulate the values we have stored. Here are common mathematical symbols:

Assignment: ← or :=
Example: c ← 2πr, c := 2πr

Comparison: =, ≠, <, >, ≤, ≥

Arithmetic: +, −, ×, /, mod

Floor/ceiling: ⌊, ⌋, ⌈, ⌉a ← ⌊b⌋ + ⌈c⌉

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.

In Pseudocode, they are used to indicate common input-output and processing


operations. They are written fully in uppercase.

START: This is the start of your pseudocode.


INPUT: This is data retrieved from the user through
typing or through an input device.
READ / GET: This is input used when reading data from a
data file.
PRINT, DISPLAY, SHOW: This will show your output to a
screen or the relevant output device.
COMPUTE, CALCULATE, DETERMINE: This is used to
calculate the result of an expression.
SET, INIT: To initialize values
INCREMENT, BUMP: To increase the value of a variable
DECREMENT: To reduce the value of a variable

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

This is a conditional that is used to provide statements to be executed if a certain


condition is met. This also applies to multiple conditions and different variables.

Here is an if statement with one condition


IF you are happy
THEN smile
ENDIF
Here is an if statement with an else section. Else allows for some statements to be
executed if the “if” condition is not met.
IF you are happy THEN
smile
ELSE
frown
ENDIF

We can add additional conditions to execute different statements if met.


IF you are happy THEN
smile
ELSE IF you are sad
frown
ELSE
keep face plain
ENDIF

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

To emulate a function call in pseudocode, we can use the Call keyword


call: clear monitor
PART 6: PROGRAM WRAPPING

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.

To do this, we wrap our code as a program. A program can be defined as a set of


instructions that performs a specific task when executed.
PROGRAM makeacupoftea
END

PART 7: EXCEPTION HANDLING

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.

You might also like