Pseudocode: steps in plain language

The four steps to developing a program are:

  1. Completely understand the problem
  2. Devise a plan to solve it
  3. Carry out that plan
  4. Review the results

Flowcharting and Pseudocode helps with deriving a solution that is independent of a programming language. You should derive your algorithm before attempting to pseudocode your program. An algorithm provides a general procedure to acomplishing a programming goal. Some algorithms are very detailed while others are not. Pseudocoding is the step taken to translate an algorithm step into the logic needed to acomplish the algorithm step. Taking the time to complete this step helps with organizing and outlining your coding logic before writing each line of code using the programming language of your choice. Learning about pseudocode will improve the efficiency of your coding process and the quality of your work.

Pseudocode is a way to write a computer program or algorithm without having to remember the syntax of a particular computer language. In a sense, it is a non-specific computer language. Pseudocode primarily uses plain text to describe various coding actions and their correct sequence in the algorithm, although some types of pseudocode also use mathematical symbols to reference data sets. While programming languages like Java exist for computers to process, pseudocode exists for humans to read and understand. Like any language of communication, such as English or Spanish, syntax rules must be applied to Pseudocode in order for others to understand what was written. However, the rules are vary flexible. For example, Khan Academy's introduction to Pseudocode for an AP CSP course uses DISPLAY to indicate output to the user. Whereas, other sources use WRITE, Write, or PRINT, Print to represent the same thing. As you can see, although different words are used, it is clear that the various words chosen mean the same thing: generate output.

Because computers can't interpret pseudocode, programmers need to translate it into a programming language to produce a functional algorithm. Programmers can interpret each line of pseudocode into the technical specifications of their preferred coding language.

It is important to note that pseudocode doesn't actually run anywhere, but represents programming concepts (also know as constructs) that are common across all programming languages. You will always need input, processing, and output. You will always need a way to describe the steps a program, or algorithm, will need to execute to achieve its design specification.

A huge positive side effect to first writing your program in Pseudocode is that once completed, you basically have all the comments (documentation) for your program. A well documented (commented) program is a requirement for all programming efforts.

Main constructs of pseudocode

To properly represent an algorithm, pseudocode includes representations of several elements of programming logic, also known as constructs. These constructs are the structure that explain the various methods used to construct a program. Write pseudocode constructs in uppercase is an easy way to tell a reader that the phrase describes a critical action for the algorithm. The primary constructs you use in pseudocode are:

SEQUENCE

A sequence is the order of tasks in an algorithm. In pseudocode, items in a sequence are each on their own line, one after the other. All tasks are linear in pseudocode, so each task occurs in order and no tasks happen at the same time. Almost any verb can be a task in a sequence depending on the algorithm or program's purpose, but here are several common commands and tasks that programmers use in pseudocode:

IF-THEN-ELSE

In pseudocode, IF-THEN-ELSE is a construct that describes part of an algorithm with two possible outcomes. It's a conditional statement that involves explaining what happens if one event occurs, then explaining what happens if that event does not occur. IF-THEN-ELSE commands are a type of sequence because you first consider the condition, then choose a single outcome.

Example:

    IF the user's age is over 21 THEN
        Display a welcome message
    ELSE
        Display a denied message

CASE

CASE is the construct for conditional statements with multiple outcomes. It comes from the phrase "in the case of." While you use IF-THEN-ELSE for algorithm statements with two possibilities, CASE is for statements with two or more statements. You use the pseudocode to explain what happens when each possibility happens.

Example:

CASE number of posts 
    Under 100: Display novice icon 
    101-1000: Display intermediate icon 
    Over 1000: Display expert icon

FOR

FOR is a command that shows a repeating action, also known as a loop. You use the FOR construct when you want to apply instructions in an algorithm to every item in a data set. To use this action, you define the boundaries of the data set you want to use, such as a list of numbers, then you describe the action you want to apply to each of those numbers.

Example:

FOR each day in the month 
    Calculate number of active daily users

REPEAT-UNTIL

The REPEAT-UNTIL construct is a type of loop that continues until a certain situation happens. It's similar to FOR, but instead of applying actions to a particular data sets, it loops the action until the algorithm achieves a goal. You list the sequence you want to repeat first, then explain when you want the sequence to end.

Example:

REPEAT 
    Email new customers
UNTIL 1000 replies

WHILE

WHILE is another looping element that explains the limits of the loop at the beginning of the statement. While REPEAT-UNTIL is a loop that lists the conditions of the loop at bottom of the code, WHILE is a loop that describes the conditions at the top of the code. When using the WHILE command, the loop only happens if the condition you provide is true. If the condition is false, the loop doesn't happen.

Example:

WHILE the number of daily active users is under 100
        Send weekly reminder emails

Other constructs

Although these six constructs are the most popular elements of pseudocode, there are many other keywords you can use in your work. Because pseudocode is informal and meant for human interpretation, you get to decide which words to use to represent your algorithm. If you're using unique or custom keywords to represent specific actions other than the six main constructs, consider creating a glossary to explain each one.

Suggestions for writing pseudocode?

There are a few best practices for writing pseudocode you can use to improve its function and readability. You can develop your own pseudocode writing style while still following basic guidelines to make it easy for others to interpret your code instructions or collaborate on algorithms. Here are the main pseudocode rules:

How to write pseudocode

The exact process you use to write pseudocode varies depending on the purpose of your algorithm, but it involves the same basic steps.

1. Create a flowchart

Start by analyzing the problem and developing a flowchart of the main functions you want to complete with your algorithm or program. The flowchart can be very basic, including only a few functions with arrows indicating the direction of ideas. By starting with a flowchart, you create a visual representation of your code. Reference your flowchart for inspiration about what constructs to use to complete each action.

2. Write down an action

Start by writing down one action for your code to complete. For example, displaying text on a website, calculating an equation or printing a document are all actions you can initiate with code. Always begin with the first action in your flowchart or sequence to keep your pseudocode organized. That is don't start in the middle of your flowchart. Start at the beginning.

3. Choose Construct

Decide what type of sequence, loop or conditions you need to make your code in this step work. For example, if you want your algorithm to print certain documents, use the WHILE construct to read the pages from a file and print them.

4. Combine constructs

Modify and combine constructs to create custom pseudocode that describes complex operations. Remember to use indents or another form of spacing to organize groups of constructs, especially when nesting one construct within another.

For example, you can start with an IF-THEN-ELSE construct that tells the algorithm to print tests for fourth-grade students on blue paper and tests for fifth-grade students on green paper. You can then add a REPEAT-UNTIL construct that instructs the program to keep printing until you have 100 total tests.

 

Pseudocode keywords

Below are some Pseudocode keywords that you can use for developing or documenting a program.

Declare - make space in memory for a variable and give it a name

Declare varName As varType - make space in memory for a variable of a specific type, such as Integer, Real, Float, or Boolean

Declare num As Integer

Declare arrayName[size] As varType - make space in memory for an array that contains data of a specific type, such as Integer, Real, Float, or Boolean

Declare nums[10] As Integer

Set - place a value into a variable

Set num = 0;

Write - output text to a user - Write "Enter a number: "

Write "Enter a number: "

Input - get text from a user - Input num

Input num

If / Then / Else / End If - evaluate a condition. If the condition is true then execute the Then (true) code block, otherwise execute the Else (false) code block.

If x = 5 Then
    Write "It is five."
Else 
    Write "It is NOT five."
End If

Repeat / Until - looping structure that will repeat lines of code until a condetion is met.

Declare num As Integer
Repeat
    Write "Please enter a number: "
    Input num
    Write "You entered... "
    Write num
Until num = 5

While / End While - looping structure that will repeat lines of code while a condition exists.

Declare num As Integer
Set num = 0
While num < 5
    Write "Please enter a number: "
    Input num
    Write "You entered a number less than 5."
End While 

For (Count = startValue; Count <= endValue; Increment/Decrement) / End For - looping structure that will repeat lines of code based on a counter (iterator). That is, the loop executes a predetermined number of times.

For (count = 0; count < 10; count++)
    Write count
End For

Variable Names - should be meaningful, start with a lowercase a-z character, and be camel case, which is the practice of writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter, such as camelCase.

myVar = 10
yourVar = 20

Constant Names - should be all uppercase where needed spaces are replaced with an underscore (_).

MY_CONSTANT = 10
YOUR_CONSTANT = 20

Pseudocode for equations is the same as a math equation.

valA = 4 * a * c
valB = b * b
valC = 2 * a
valD = valB - ValA
valE = sqrt(valD) - b
valF = valE / valC