Writing Pseudocode: Algorithms & Examples

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: XML Schema: Definition & Example

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:00 Why Should I Write Pseudocode?
  • 0:52 How Do I Write Pseudocode?
  • 1:52 Examples Of The Pseudocode
  • 2:57 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Login or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Lonny Meinecke

Lonny teaches psychology classes at King University, and has a bachelor's degree in IT and a doctorate in psychology.

In this lesson, we will cover the writing of pseudocode by describing what it is and why we use it, and look at some common techniques. Then, we'll present a few examples to give you a better idea.

Why Should I Write Pseudocode?

A computer program generally tries to solve a well-defined problem using a well-defined algorithm. However, both the problem to be solved and the recipe/algorithm for solving it are initially written in natural language (very similar to day-to-day speaking). We need something much closer to an actual set of computer instructions, but going straight from natural language to computer code can be tedious and complex - especially if you need to do this many times for many different computer languages (and we very often do).

It is often far better to rewrite the algorithm as a set of specific instructions which is very similar to computer code, but not specific to any one computer. That's what we call pseudocode...it looks like computer instructions but cannot be executed on a computer.

How Do I Write Pseudocode?

Writing pseudocode is pretty easy actually:

  • Start with the algorithm you are using, and phrase it using words that are easily transcribed into computer instructions.
  • Indent when you are enclosing instructions within a loop or a conditional clause. A loop is a set of instructions that is repeated. A conditional clause is formed by a comparison and what to do if that comparison succeeds or fails. This technique makes sure it's easy to read.
  • Avoid words associated with a certain kind of computer language.

It turns out that there are some fairly standard words you can use. These include standard looping structures like the following:

  • FOR … ENDFOR
  • WHILE…ENDWHILE

There are also some terms for standard conditional clauses:

  • IF … ENDIF
  • WHILE … ENDWHILE (this is both a loop and a conditional clause by the way)
  • CASE … ENDCASE

There are more, but that's enough for us to present some examples.

Examples of the Pseudocode

For our first example, we will pretend we have a square game board with one or more bombs hidden among the squares. We want to scan the game board and print the number of hidden bombs. Our algorithm methodically checks each row and each column to see if a hidden bomb is there, and if it is, we add 1 to the total number of bombs. This is one way to write that:

code example 1

Code example 1

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support