How to write pseudocode

Author: Gregory Harris
Date Of Creation: 14 August 2021
Update Date: 20 June 2024
Anonim
How Do I Write Pseudocode?
Video: How Do I Write Pseudocode?

Content

Pseudocode is a simple tool that can be used to schedule algorithms to run. When you have to write complex code, you are unlikely to be able to keep the entire program in your head before starting work. Imagine pseudocode is a consistent verbal description that you later turn into a programming language. It is a combination of a human language and a programming language: pseudocode uses the syntax of computer code, but its main purpose is to be readable.

Steps

Method 1 of 5: Understanding the pseudocode

  1. 1 Find out what pseudocode is. Pseudocode is a consistent verbal description of code that can be gradually transferred into a programming language. Many programmers use it to plan the function of an algorithm before starting more technical work on the code. Pseudocode is a loose plan, a tool for thinking through program problems, and a communication tool that allows you to communicate your thoughts to other people.
  2. 2 Find out why pseudocode is useful. Pseudocode is used to demonstrate how a computer algorithm can and should work. Engineers often use pseudocode as an intermediate stage in programming - between the planning stage and the stage of writing working code. Good pseudocode can turn into comments on the final version of the program and will help the programmer fix bugs in the future or correct the code. The pseudocode is also useful:
    • To describe how the algorithm should work. Pseudocode shows how a certain part of the program, mechanism or technique should be displayed in the program. Experienced programmers often use pseudocode to explain their development steps to junior programmers.
    • To explain the process of the program to people who are not well versed in programming. Computers need very strict code for a program to work, but people, especially those who are not involved in programming, find it easier to understand a simpler and more subjective language that clearly describes the purpose of each line of code.
    • To develop code in a group. Highly skilled engineers often incorporate pseudocode into their work to help programmers solve a difficult problem they are about to face. If you are working in a group with other programmers, the pseudocode will help you explain what you are doing to others.
  3. 3 Remember that pseudocode is subjective and not standardized. It lacks a clearly defined syntax - there is only an unspoken rule to use standard constructs that other programmers could understand without too much trouble. If you write code yourself, pseudocode can help you organize your thoughts and develop a plan. If you work with other engineers (and it doesn't matter what their level of proficiency is), it is important to use at least basic constructs so that everyone else understands what you wanted to do.
    • If you are learning programming in an institution, most likely, you will be offered a test of knowledge of the so-called pseudocode standards. The standard may vary from teacher to teacher and from school to institution.
    • Understandability is the main criterion for pseudocode, so pseudocode will be useful if you use standard constructs in your work. You will need to turn the pseudocode into a programming language, and the pseudocode will allow you to organize the entire structure in your head.
  4. 4 Learn to understand algorithms. An algorithm is a procedure for solving a problem in a manner familiar to the program, and the order in which actions will be performed. An algorithm is simply a set of steps that allow you to solve a problem: sequence of actions, selection, iteration, and type of call.
    • In the C programming language, sequence operators are always present.
    • Choice is an "if then else" structure.
    • Iteration is performed using a set of calls: "while", "do", "for."
    • The type of call is selected using the "switch" operator.
  5. 5 Know which three elements govern the algorithm. If you can use a sequence function, a while function, and an if-then-else function, you have all the basic elements to write a working algorithm.
    • SEQUENCE is a linear progression in which one task is executed after another in a specific sequence. For example:
      • READ the height of the rectangle
      • READ the width of the rectangle
      • COMPUTE area as height x width
    • WHILE is a looping (repeating) condition check at the beginning. The beginning and end of the cycle are indicated by the words WHILE (for now) and ENDWHILE (end of action for now). The loop ends only if the condition is met. For instance:
      • WHILE population limit
        • Compute population as population + birth - death
      • ENDWHILE
    • IF-THEN-ELSE (if ... then ... otherwise ...) is a selection function that chooses between two options. Binary choice is defined by four keywords: IF, THEN, ELSE, and ENDIF. For example:
      • IF (if) working hours> norm max THEN (then)
        • Show recycling times
      • ELSE (otherwise)
        • Show opening hours
      • ENDIF (end)

Method 2 of 5: Pseudocode Example

  1. 1 Consider a simple program example. Imagine that a program has to replace the letter combination "foo" in a text file. The program will read each line in this file, look for the desired combination in each line and replace it with another. Repeating steps start with spaces - ideally, this is how it should be in real code. An initial sketch of the pseudocode might look like this:
    • open file
    • on each line of the file:
      • find a combination
      • remove combination
      • insert another combination
    • close file
  2. 2 Write pseudocode iteratively:write it once and then change the data in it... One of the benefits of pseudocode is that you can only sketch out the basics and leave the hard stuff for later. Note that in the example above, there is no indication of what the letter combination should be. As a programmer, you can rewrite the pseudocode to include algorithms for removing individual letters and replacing them with others. The second sketch might look like this:
    • open file
    • on each line of the file:
      • find a word like this:
        • read a character in a string
        • if the character matches, then:
          • if all of the following characters match
          • then this is the right choice
          • remove word characters
          • insert new word characters
    • close file
  3. 3 Use the code to add new features. Pseudocode helps programmers think through a solution to a problem. This can be compared to the intermediate calculations in the equation. Used correctly, pseudocode can make a complex task simple. You can modify the pseudocode little by little, one step at a time:
    • open file
    • request replacement word
    • request replacement word
    • on each line of the file:
      • find a word like this:
        • read a character in a string
        • if the character matches, then:
          • if all of the following characters match
          • then this is the right choice
      • count the number of repetitions of a word
      • remove word characters
      • insert new word characters
      • show the number of repetitions of a word
    • close file

Method 3 of 5: Standard Process for Writing Pseudocode

  1. 1 Write only one hit per line. Each pseudocode call should only give the computer one action. Most often, with a correct description of the task, each task will correspond to one line of pseudocode. Write a to-do list, then turn it into pseudocode, and then turn the pseudocode into real executable code.
    • Task list:
      • Read name, cost of an hour, number of hours
      • Perform calculations
      • amount before deduction = cost per hour * number of hours
      • deduction = amount before deduction * deduction factor
      • amount after deduction = amount before deduction - deduction
      • Write down name, amount before deduction, deduction, amount after deduction
    • Pseudocode:
      • READ name, value of the hour, number of hours, coefficient of Deduction
      • AmountUnderDeduction = cost of Hour * number of Hours
      • Deduction = AmountDeduction * Deduction Factor
      • Amount After Deduction = Amount Before Deduction - Deduction
      • WRITE name, Amount Before Deduction, Deduction, Amount After Deduction
  2. 2 Write the first word of the main function in capital letters. In the example above, READ and WRITE are in capital letters because they are the main functions of the program. Important keywords can be READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE, REPEAT, and UNTIL.
  3. 3 Write what you mean, don't program. Some programmers write pseudocode as a program - for example, "if a% 2 == 1 then". However, those who read pseudocode will find it difficult to understand abstract symbols. It will be much easier to understand a phrase like "if an odd number then". The clearer you write, the easier it will be for people to understand what you mean.
  4. 4 Write down absolutely everything. Everything that happens within one process should be described in as much detail as possible. The pseudocode resembles simple instructions. Variables are rarely used in pseudocode - much more often it describes what a program should do with more understandable objects: account numbers, names, transaction amounts.
    • Here's an example of good pseudocode:
      • If the account number and password are appropriate, then show the basic account information.
      • Calculate the total cost in proportion to the invoiced amount for each shipment.
    • Here's an example of unsuccessful pseudocode:
      • let g = 54 / r (Don't use variables. Better describe what is hidden underneath.)
      • do the main processing until the process ends (It is necessary to clarify what the main processing is and what will indicate the end of the process.)
  5. 5 Use standard programming language tools. Even though there are no standards for pseudocode, it will be easier for other programmers to understand what you are doing if you use constructs from existing programming languages ​​(those with sequences). Use "if", "then", "while", "else" and "loop" or their analogs in Russian just as you would do in a programming language. Pay attention to the following constructs:
    • if CONDITION then INSTRUCTION. This means that a separate statement will only fire if a separate condition is met. In this case, an instruction is a step that the program will have to perform. The condition means that the data must meet a certain set of requirements, after checking which the program will be able to work.
    • while CONDITION do INSTRUCTION. This means that the statement must be repeated over and over until the condition is no longer met.
    • do INSTRUCTION while CONDITION. This construct is similar to while CONDITION do INSTRUCTION. In the first case, the condition is checked before the statement takes effect, but in this case, the statement is run first, and the INSTRUCTION task will be triggered at least once.
    • for a = NUMBER1 to NUMBER2 do INSTRUCTION. This means that the variable "a" will automatically take on the value NUMBER1. "a" will increase by one in each step until the variable reaches NUMBER2. Any other letter can be used to denote a variable.
    • function NAME (ARGUMENTS): INSTRUCTION. Every time a certain combination of letters is used in the code, it serves as a name for some instruction. Arguments are a list of variables that are used to refine the statement.
  6. 6 Separate steps in blocks. Blocks are syntax elements that link multiple statements into one. Using blocks, you can organize information (for example, steps from block 1 are always performed before steps in block 2) or combine it (for example, instruction1 and instruction2 have the same subject matter). In general, all requests should be separated to show their dependence on others. There are two ways to do this.
    • With curly braces:
      • {
      • INSTRUCTION1
      • INSTRUCTION2
      • ...}
    • Using spaces. When using spaces, each instruction in the same block will have to start at the same distance from the left edge of the screen. Blocks within blocks will be located further. The top-level block instruction closes the sub-block, even if there is an instruction below with the same number of leading spaces.
      • BLOCK1
      • BLOCK1
        • BLOCK2
        • BLOCK2
          • BLOCK3
        • BLOCK2
          • BLOCK3
      • BLOCK1

Method 4 of 5: Practice Writing Pseudocode

  1. 1 First, describe the purpose of the process. This will help you figure out if your pseudocode is complete. If the pseudocode can solve the problem, it is considered complete. Describe the process. If it's simple, you need very few lines. Reread what you wrote and think:
    • Will anyone with at least minimal knowledge of the process understand this pseudocode?
    • Can the pseudocode be easily turned into real computer code?
    • Does the pseudocode describe the entire process and have any details been overlooked?
    • Will the target audience be able to understand each object name in pseudocode?
  2. 2 Write the first steps to prepare you for instructions. Usually the first part of the code defines the variables and other elements that make the algorithm work.
    • Include variable values. Specify in the code how each variable and each data unit will be used.
    • Define controls. You will need to describe them in pseudocode language (text and images in object-oriented programming languages ​​and simpler tools in other languages) just as you would with real code.
  3. 3 Write functional pseudocode. Rely on pseudocode principles by creating event-driven or object-oriented code after specifying program "settings". Each line of code should describe a query, loop, select, or any other function.
  4. 4 Add comments as needed. In real computer code, comments explain to the reader the role of tasks and pieces of code. This should be detailed in pseudocode in simple natural language, because you won't use comments until you turn the pseudocode into real code.
    • Many programmers prefer to turn pseudocode into regular code with comments. This allows other programmers, who are also working on this project, analyzing it or learning something, to understand what the developer wanted to do with each specific line.
    • Start comments with / / to prevent the computer from reading them. Slashes must be separated by a space. For example:
      • / / IF the robot has no obstacle ahead THEN
        • / / Move the robot
        • / / Add the shift command to the command history
        • / / RETURN is true
      • / / ELSE
        • / / RETURN falsely do not move the robot
      • / / END IF
  5. 5 Re-read the finished work and look for errors in logic and syntax. The syntax doesn't have to be perfectly correct, but the pseudocode should look logical. Try to put yourself in the shoes of the person reading this code and consider whether your commands are as clear as possible.
    • Rate code modules according to the elements they cover. For example, key computer operations include reading and retrieving information from a file, writing to a file or displaying it on a screen, mathematical calculations, evaluating variable data, comparing one or more elements. All of these processes have their place in the computer code, as well as in the pseudocode that you create for this program.
    • Embed specific tasks in pseudocode. When you separate each new task with spaces, present this information in pseudocode, imitating the real programming language, but not adhering to the hard rules of the programming language.
    • Check if all the required elements are present in the pseudocode.Even if some of the technical details, such as variable explanations, are not needed, every task and every element should be clearly spelled out.
  6. 6 Reread the pseudocode. When your pseudocode describes the process without significant errors, reread it with any of the contributors to this project. Ask your colleagues to point out to you which parts need improvement. Programmers often do not describe the processes in detail, so at this stage you can add everything you need. If you are working on the code yourself, reread what you wrote and have someone review your work.
    • If your colleagues are unhappy with the pseudocode, rewrite it more clearly. Ask your coworkers what you didn’t manage: do the steps generally seem incomprehensible, or did you forget to include some important piece of the process in the pseudocode?
  7. 7 Save the pseudocode. When you've reviewed the code and colleagues have approved your work, save the pseudocode to an archive. When you write real code, include the pseudocode with code comments. Start comments with / / to prevent the computer from trying to execute them as a program.

Method 5 of 5: Transforming Pseudocode into Programming Language Code

  1. 1 Trace the pseudocode and understand how it works. The pseudocode gives you an algorithm. For example, the code might sort the list alphabetically. The pseudocode will help you figure out how to build an algorithm in the programming language you are working with.
  2. 2 Use programming elements appropriate for your programming language. These elements can include variable declarations, if and loop statements. Each line can be brought to life in different ways. Everything will depend on the level of the programming language you are using.
    • For example, try displaying certain data on the screen. To do this, you can use a special window or an existing graphical interface with which you are working.
  3. 3 Implement pseudocode. If the pseudocode is written simply, competently, and clearly, the entire algorithm will work more efficiently and without errors when the program is run.
  4. 4 Re-trace and compare the working code with the pseudocode. Check if the working code follows the pseudocode logic. For example, if your pseudocode provides input and output, try all possible input methods and compare the output from the code with the output from the pseudocode. You can ask a colleague to trace or recommend a way to fix the code.

Tips

  • Try to understand the basic operation of the computer. The code should instruct the computer to perform operations. Understanding the principles of these operations will help you write pseudocode that tracks what the main code is doing.
  • Use spaces as efficiently as possible. Whitespace can be used to separate code elements, and this is especially important in pseudocode to make it easier for people to read. Imagine the space is a separate block. Lines that start with the same number of spaces are in the same block, and they have approximately the same importance to the process in the algorithm.