Pseudo Code
Pseudo Code
Pseudo Code
Pseudocode is a logic development tool that uses English-like statements or clauses to present the logical steps necessary to solve a problem. Pseudo technically means false, so pseudocode, taken literally, means false code. As used in program development, pseudocode is made up of statements written to depict the steps, in the correct sequence, required to solve a specific problem. These statements can then be directly translated into computer programming language instructions. Pseudocode is false code only in the sense that it is not the programming language statements or code that is used to direct the action of the computer. Pseudocode is a kind/type of structured English for describing algorithms. It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax. At the same time, the pseudocode needs to be complete. It describes the entire logic of the algorithm so that implementation becomes a rote mechanical task of translating line by line into source code. Pseudocode isn't a language; it's more of a thought process. The focus isn't on writing code in a particular programming language. It's more like drawing a map through a problem. Once you have a clear idea of what to do, it's easy to write the code to fill in the blanks, and actually do the work. There are no special symbols required with pseudocode, bit there are certain guidelines and elements of structure associated with using this methodology. Pseudocoding is a relatively easy technique to master and is a very common logic development tool in the commercial business environment. Some of the advantages of pseudocode are: It is easy to use and create with a text editor because it uses English-like statements. No special symbols are used to represent specific functions. Very little specific syntax is required. Statements can generally be translated on a one-for-one basis to any high-level language. Pseudocoding the problem logic first drastically reduces time spent in coding, testing, and debugging systems. Pseudocode is composed of words, clauses and sentences. The following rules should be followed: No actual programming code should appear in the pseudocode. You are writing down the logical steps at this stage, not coding the program. All statements should be presented in enough detail so the reader can clearly understand the activity or action being described in each statement. The names of variables being used to solve the problem should be totally descriptive of what they represent, not shortened versions you use in your source code.
Each activity or action being depicted should be presented in a single line. Appropriate indentation should be used if the activity or action being described cannot fit on one line. Indentation should be used where appropriate to show the logical grouping of related activities or actions. Most of your pseudocode should not be done with capital letters, but certain key structures should be capitalized. They are: IF, THEN, ELSE and ENDIF in decision structures. DO WHILE and ENDDO in pre-test loop structures. DO UNTIL and ENDDO in post-test loop structures. CASE and ENDCASE in a case structure. Sequence Structures Execution of simple steps, in order, with no loops or decisions. Problem: Calculate a paycheck Inputs: Hours worked, Hourly salary Outputs: Total pay Formulas: Total pay = Hours worked * Hourly salary Pseudocode: start processing get hours worked get hourly salary calculate pay = hours worked * hourly salary display pay stop processing Decision Structures A question is asked and the processing path depends on the answer to that question (true or false) Problem: Determine if order can be filled and update inventory amount. Inputs: Quantity on hand (inventory amount), order amount Outputs: New quantity on hand Formulas: New quantity on hand = quantity on hand order amount Pseudocode (note capitals and indentation of the range of the decision): start processing get quantity on hand get order amount IF order amount is greater than quantity on hand THEN display not enough stock ELSE calculate new quantity on hand = quantity on hand order amount display new quantity on hand ENDIF stop processing
Loop Structures: Two different types of structures are used as loop structures DO WHILE (a pre-test loop) DO UNTIL (a post-test loop) Note capitals and indentation of the range of the loop Problem: Add up daily deposits and find end of day balance Inputs: Beginning balance, deposit amounts Formulas: Ending balance = beginning balance + deposits DO WHILE Structure: start processing get beginning balance DO WHILE there are more deposits get deposit amount balance = balance + deposit ENDDO display ending balance stop processing DO UNTIL Structure: start processing get beginning balance DO UNTIL all deposits are processed get deposit amount balance = balance + deposit ENDDO display ending balance stop processing When you're finished writing the pseudo code, you'll fill in the blanks with programming statements, and you'll have a fully documented program. You can leave the pseudo code in your program, and each line of pseudo code will then become a comment line. This is an easy way to document your program as you write it. Comments are extremely important in writing source code. As a program becomes more complex, comments allow the person reading your source code to understand your logic. You will find that many times you may refer back to existing code or need to make modifications to a program or function that was you wrote some time ago. Having comments allows you to scan through the source code and fully understand your original intentions.