Writing Pseudocode: 15 Steps (with Pictures)

Table of contents:

Writing Pseudocode: 15 Steps (with Pictures)
Writing Pseudocode: 15 Steps (with Pictures)
Anonim

This wikiHow teaches you how to create a pseudocode document for your computer program. Pseudocode essentially means that you make an outline of the intention of your code without a programming language.

Steps

Part 1 of 3: Understanding the basics of pseudocode

Write Pseudocode Step 1

Step 1. Learn to understand pseudocode

Pseudocode is a step-by-step written draft of your code that you can gradually transfer into a programming language of your choice. Many programmers use it to map out the function of an algorithm before embarking on the more technical task of coding.

Pseudocode serves as an informal guide, a tool for thinking through program problems, and a communication tool that can help you explain your ideas to other people.

1494423 2

Step 2. Understand the usefulness of pseudocode

Pseudocode is used to show how a computer algorithm should work. Programmers often use pseudocode as an intermediate step in programming between the initial planning stage and the stage of writing the actual executable code. Some other uses of pseudocode include the following:

  • Describe how an algorithm should work. Pseudocode can illustrate where a particular construct, mechanism, or technique could or should exist in a program.
  • Explaining a computer process to less technical users. Computers require very strict input syntax to run a program, but people (especially non-programmers) may find it easier to understand a more fluent, subjective language that clearly states the purpose of each line of code.
  • Designing code in groups. High-level software architects will often incorporate pseudocode into their designs to solve a complex problem faced by their programmers. If you are developing a program with other programmers, pseudocode can help clarify the purpose of a program.
Write Pseudocode Step 3

Step 3. Remember that pseudocode is subjective and non-standard

There is no fixed syntax that you absolutely must use for pseudocode, but it is a common professional courtesy to use standard pseudocode structures that other programmers can easily understand. If you're coding a project on your own, the most important thing is that the pseudocode helps you structure your thoughts and execute your plan.

  • If you're working on a project with others - whether that be your fellow students, junior programmers, or non-technical staff - it's important to use at least some standard structures so that everyone can easily understand what you mean.
  • If you take a programming course at a university, a coding camp, or a company, you will probably be tested against a "standard" pseudocode to be learned. This standard often varies between institutions and teachers.

Clarity is a primary goal of pseudocode, and can help if you work within accepted programming conventions. If you're developing your pseudocode into real code, you'll need to convert it into a programming language -- so it may help to structure your setup with this in mind.

Write Pseudocode Step 4

Step 4. Focus on the main purpose of pseudocode

It can be easy to fall back on a programming language once you're up and running. Remember the purpose of your pseudocode - explain what each line of the program should do - this will keep you grounded while creating the pseudocode document.

Part 2 of 3: Writing good pseudocode

1494423 5

Step #1. Use a plain text editor

It can be tempting to use a word processor (eg Microsoft Word) or similar program to create a rich text document, but pseudocode needs as little formatting as possible to keep it simple.

Plain text editors include Notepad (Windows) and TextEdit (Mac).

1494423 6

Step 2. Start writing down the purpose of the process

Dedicate a line or two to explaining the purpose of your code -- this will help draft the rest of the document, as well as save you the task of explaining the program's function to each person to whom you pseudo code shows.

1494423 7

Step 3. Write only one statement per line

Each statement in your pseudocode should only express one action for the computer. In most cases, if the task list is properly compiled, each task will correspond to one line of pseudocode. Consider writing out your to-do list, then converting that list into pseudocode, and then gradually developing that pseudocode into actual, computer-readable code.

1494423 8

Step 4. Use white space and indent effectively

Using blank lines between 'blocks' of text helps distinguish different components of your pseudocode, and indenting different chunks of each block will indicate that those chunks of pseudocode fall within a less indented section.

For example, a part of the pseudocode that discusses entering a number should all be in the same block, while the next part (eg, the part that discusses the output) should be in a different block

1494423 9

Step 5. Write important commands in capital letters, if necessary

Depending on your pseudocode requirements or the environment in which you publish the pseudocode, you may need to capitalize commands that will remain in the actual code.

For example, if you use the commands 'if' and 'then' in your pseudocode, you could change them to 'IF' and 'THEN' (eg, 'IF input number THEN output result')

1494423 10

Step 6. Use simple terminology

Remember, you're writing about what the project will "do", not the code itself. This is especially important if you're writing pseudocode to serve as a demonstration for a client who doesn't understand coding, or as a project for a novice programmer.

You may want to omit all the coding commands yourself and just define the process of each line in plain language. For example, "if input is odd, output is 'Y'" might change to "if user inputs an odd number, display 'Y'".

1494423 11

Step 7. Keep the pseudocode in the correct order

While the language you use to modify your pseudocode should be simple, you should still keep every bit of your pseudocode in the order in which it should be executed.

1494423 12

Step 8. Leave nothing to the imagination

Everything that happens in the process must be fully described. Pseudocode statements are close to simple Dutch or English statements. Pseudocode usually doesn't use variables, but instead describes what the program should do, using (almost) real objects, such as account numbers, names, or transaction amounts.

1494423 13

Step 9. Use standard programming structures

Even if there is no standard for pseudocode, it will be easier for other programmers to understand your steps if you use structures from existing (sequential) programming languages. Use terms like 'if', 'then', 'while', 'else' and 'loop' the same way you would in a programming language. Consider the following structures:

  • IF condition THEN statement - This means that a particular statement will only be executed if a particular condition is true. "Instruction" in this case means a step that the program will perform, while "condition" means that the data must meet a certain set of criteria before the program performs an action.
  • WHILE condition DO statement - This means that the statement must be repeated over and over until the condition is no longer true.
  • DO statement WHILE condition - This is very similar to "WHILE condition do statement". In the first case, the condition is checked before the statement is executed, but in the second case, the statement is executed first. So, in the second case, 'instruction' is executed at least once.
  • FUNCTION name (arguments): statement - This means that whenever a particular name is used in the code, it is an abbreviation for a particular statement. 'Arguments' are lists of variables that you can use to clarify the statement.
1494423 14

Step 10. Organize your pseudocode into blocks

If you have large blocks of pseudocode that define other pieces of pseudocode within the same block, you can use parentheses or other characters to distinguish everything from each other.

  • Brackets - both square brackets (eg, [code]) and curly braces (eg, {code}) can help enclose long segments of pseudocode.
  • When coding, you can add comments by typing '//' to the left of the comment (for example:

    //This is a temporary step.

    ). You can use the same method when writing pseudocode, to leave comments that don't fit in the coding text.
Write Pseudocode Step 15

Step 11. Double-check your pseudocode for readability and clarity

At the end of the document you should be able to answer the following questions:

  • Would this pseudocode be understood by someone unfamiliar with the process?
  • Is the pseudocode written in such a way that it can be easily translated into a computer language?
  • Does the pseudocode describe the entire process without omitting anything?
  • Is each object name used in the pseudocode clearly understood by the target audience?
  • If you feel some of the pseudocode needs to be fleshed out or doesn't explicitly describe a step that someone else might forget, go back and add the necessary information.

Part 3 of 3: Creating a sample pseudocode document

1494423 16

Step #1. Open a plain text editor

You can use Notepad (Windows) or TextEdit (Mac) if you don't want to install a new program.

1494423 17

Step 2. Define your program

Although not strictly necessary, writing a line of one or two sentences at the top of the document will immediately clarify the intent of the program:

This program asks for a greeting from the user. If the greeting matches a particular response, the response will be delivered. If not, a rejection will be given.

1494423 18

Step 3. Write the opening sequence

Your first command - that is, the first thing your program should do when it runs - should be the first line:

print greeting "Hello stranger!"

1494423 19

Step 4. Add the following line

Put a space between the last line and the next by pressing ↵ Enter, then write the next line of code. In this example, the user must request the following line of dialog:

print prompt press "Enter" to continue  
1494423 20

Step 5. Insert the call to action

In this example, the user is prompted for a greeting:

print call to action "How are you?"

1494423 21

Step 6. Show the user a list of answers

Again, after pressing ↵ Enter in this example, the user should see a list of possible answers:

Show possible answers "1. Good." "2. Awesome!" "3. Not good."

1494423 22

Step 7. Get input from the user

Here the program will ask the user to enter an answer:

Print request for input "Enter the number that best describes you:"

1494423 23

Step 8. Create "if" or "IF" statements for user input

Since there are multiple answers for the user to select, you will want to return multiple results based on their selected answer:

IF "1" print answer "Dandy!" IF "2" print answer "Fantastic!" IF "3" print answer "Be a little happier, buttercup!"

1494423 24

Step 9. Add an error message

In case the user chooses an answer incorrectly, you can display an error message:

IF entry not recognized print response "You don't follow instructions very well, do you?"

1494423 25

Step 10. Add any other parts of the program

Go through your document and add or complete details to ensure that both you and anyone reading the document will understand its meaning. Following the example of this method, your final pseudocode document should look something like this:

This program asks for a greeting from the user. If the greeting matches a specific response, the response will be delivered. If not, a rejection will be given. Greeting Print "Hello Stranger!" Print prompt press "Enter" to continue  Print call to action "How are you today?" Show possible answers "1. Good." "2. Awesome!" "3. Not good." Print request for input "Enter the number that best describes you:" IF "1" print answer "Dandy!" IF "2" print answer "Fantastic!" IF "3" print answer "Come on, buttercup!" IF entry not recognized print response "You don't follow instructions very well, do you?" 
1494423 26

Step 11. Save your document

Press Ctrl+S (Windows) or ⌘ Command+S (Mac), enter a name and click Save to do this.

Tips

Popular by topic