Pseudocode - Definition Pseudocode is a generic way of describing an algorithm without use of any specific programming language syntax. It is, as the name suggests, pseudo code — it cannot be executed on a real computer, but it models and resembles real programming code, and is written at roughly the same level of detail. Pseudocode, by nature, exists in various forms, although most borrow syntax from popular programming languages (like C, Lisp, or Fortran). Natural language is used whenever details are unimportant or distracting. Computer science textbooks often use pseudocode in their examples so that all programmers can understand them, even if they do not all know the same programming languages. Since pseudocode style varies from author to author, there is usually an accompanying introduction explaining the syntax used. A description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines or language- specific syntax. Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Pseudocode typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system- specific code and subroutines.
languages, and that it may be a compact and environment-independent generic description of the key principles of an algorithm. No standard for pseudocode syntax exists, as a program in pseudocode is not an executable program. Pseudocode resembles, but should not be confused with, skeleton programs including dummy code, which can be compiled without errors. Flowcharts can be thought of as a graphical alternative to pseudocode. Syntax As the name suggests, pseudocode generally does not actually obey the syntax rules of any particular language; there is no systematic standard form, although any particular writer will generally borrow the appearance of a particular language. Popular sources include PASCAL, C, Java, BASIC, Lisp, and ALGOL. Details not relevant to the algorithm (such as memory management code) are usually omitted. Blocks of code, for example code contained within a loop, may be described in a one-line natural language sentence. Depending on the writer, pseudocode may therefore vary widely in style, from a near-exact imitation of a real programming language at one extreme, to a description approaching formatted prose at the other. Flowcharts and Pseudocode The following material was developed by Brent Daviduck of the Computer Systems Technology program at Red Deer College i Alberta, Canada. For more information on the program and for other material, see: http: / /cst.rdc.ab.ca /.A sequence o instructions is called an algorithm. Algorithms are a fundamental part of computing. If you study computing for many years you will study algorithms of frequently used processes. Books have been written on algorithms for such common activities as storing and ordering data. As most problems you get are unique, you will develop your own algorithms. However, you may find standard algorithms for those parts of your programs that do common activities. There are two commonly used tools to help to document program logic (the algorithm). These are flowcharts and Pseudocode. We
The flow of data between steps is indicated by arrows, or flowlines. For example, a flowchart (and equivalent Pseudocode) to compute the interest on a loan is shown below: Read NAME, BALANCE, RATE Compute INTEREST as BALANCE x RATE Write (Display) NAME and INTEREST Note that the Pseudocode also describes the essential steps to be taken, but without the graphical enhancements. Another example of a flowchart and the equivalent Pseudocode is shown below. In this case, the program computes the sum, average and product of three numbers: Flowchart Pseudocode Read X, Y, Z Compute Sum (S) as X + Y + Z Compute Average (A) as S / 3 Compute Product (P) as X x Y x Z
Write (Display) the Sum, Average and Product Decisions (Switching logic) Switching logic consists of two components - a condition and a goto command depending on the result of the condition test. The computer can determine the truth value of a statement involving one of six mathematical relations symbolized in the table below: Meaning Equals Not Equal • Less than • Less than or equal to • Greater than • Greater than or equal to In practice, the computer is presented not with a true/false statement, but with a question having a "Yes" or "No" answer, for example if A = 10, B = 20, K = 5, and SALES = 10000, then:
Condition (Question) Is A == B? Is B > A? Is K Is SALES >= $5000.00? "Answer" No Yes Yes Yes With each question, the computer can be programmed to take a different course of action depending on the answer. A step in an algorithm that leads to more than one possible continuation is called a decision. In flowcharting, the diamond-shaped symbol is used to indicate a decision. The question is placed inside the symbol, and each alternative answer to the question is used to label the exit arrow which leads to the appropriate next step of the algorithm. The decision symbol is the only symbol that may have more than one exit. The example below shows the flowchart for a program that reads two numbers and displays the numbers read in decreasing order:
The equivalent Pseudocode is shown below. Note that with Pseudocode, indentation is used to show the various steps that apply to a decision: Read A, B If A is less than B BIG = B SMALL = A else BIG = A SMALL = B Write (Display) BIG, SMALL Loops Most programs involve repeating a series of instructions over and over until some event occurs. For example, if we wish to read ten numbers and compute the average, we need a loop to count the number of numbers we have read. Count loops are loops where the program must count the number of times operations are completed. The flowchart below illustrates a loop that counts from 1 to 10: Count loop flowchart While count loops work the exact number of times needed, in many cases we do not know how many times we want to do something. It is often dependent on the data provided to the
program. Imagine we change our problem to read and compute the average of a number of numbers. We won't know how many numbers there are but will read numbers until there are no more. Two alternative solutions (using Pseudocode) are shown below: pre-test loop: post-test loop: set average to zero set average to zero The flowchart shown on the left may be simplified to the form shown on the right. set count to zero set total to zero read number while ( not end-of-data ) increment count by 1 total = total + number read number if ( count > 0 ) then average = total / count display average
set count to zero set total to zero Do read a number increment count by 1 total = total + number while ( not end-of-data ) if ( count > 0 ) then average = total / count display average Both these assume that the computer will tell the program when there is no more numbers. This is called an end-of-data or end-of-file test. There is an important difference between the pre-test and post-test loops. The pre-test version will work even if there are no numbers, the post-test version assumes the body of the code will be obeyed at least one time. Both forms of loops are appropriate in different circumstances. Looping with switching and goto's The looping and switching logic above follow well defined rules. In fact, we can implement any of these constructs with a condition and a goto (unconditional branch) instruction. An example of this logic is was illustrated in the loop flowchart shown previously. Early programs were written this way. As the problems became more complex it became impossible to follow the logic when things go wrong.
Imagine trying to sort out code like this step 01: do step 02: go step 03: do step 04: if … step 16: if step 17: go step 18: do step 19: if step 20: go something to step 16 something some event has occurred go to step 19 some event has occurs go back to step 4 to step 1 something something is greater than 10 goto step 1 to step

pseudocode Note(IGCSE Computer Sciences)

  • 1.
    Pseudocode - Definition Pseudocodeis a generic way of describing an algorithm without use of any specific programming language syntax. It is, as the name suggests, pseudo code — it cannot be executed on a real computer, but it models and resembles real programming code, and is written at roughly the same level of detail. Pseudocode, by nature, exists in various forms, although most borrow syntax from popular programming languages (like C, Lisp, or Fortran). Natural language is used whenever details are unimportant or distracting. Computer science textbooks often use pseudocode in their examples so that all programmers can understand them, even if they do not all know the same programming languages. Since pseudocode style varies from author to author, there is usually an accompanying introduction explaining the syntax used. A description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines or language- specific syntax. Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Pseudocode typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system- specific code and subroutines.
  • 2.
    languages, and thatit may be a compact and environment-independent generic description of the key principles of an algorithm. No standard for pseudocode syntax exists, as a program in pseudocode is not an executable program. Pseudocode resembles, but should not be confused with, skeleton programs including dummy code, which can be compiled without errors. Flowcharts can be thought of as a graphical alternative to pseudocode. Syntax As the name suggests, pseudocode generally does not actually obey the syntax rules of any particular language; there is no systematic standard form, although any particular writer will generally borrow the appearance of a particular language. Popular sources include PASCAL, C, Java, BASIC, Lisp, and ALGOL. Details not relevant to the algorithm (such as memory management code) are usually omitted. Blocks of code, for example code contained within a loop, may be described in a one-line natural language sentence. Depending on the writer, pseudocode may therefore vary widely in style, from a near-exact imitation of a real programming language at one extreme, to a description approaching formatted prose at the other. Flowcharts and Pseudocode The following material was developed by Brent Daviduck of the Computer Systems Technology program at Red Deer College i Alberta, Canada. For more information on the program and for other material, see: http: / /cst.rdc.ab.ca /.A sequence o instructions is called an algorithm. Algorithms are a fundamental part of computing. If you study computing for many years you will study algorithms of frequently used processes. Books have been written on algorithms for such common activities as storing and ordering data. As most problems you get are unique, you will develop your own algorithms. However, you may find standard algorithms for those parts of your programs that do common activities. There are two commonly used tools to help to document program logic (the algorithm). These are flowcharts and Pseudocode. We
  • 3.
    The flow ofdata between steps is indicated by arrows, or flowlines. For example, a flowchart (and equivalent Pseudocode) to compute the interest on a loan is shown below: Read NAME, BALANCE, RATE Compute INTEREST as BALANCE x RATE Write (Display) NAME and INTEREST Note that the Pseudocode also describes the essential steps to be taken, but without the graphical enhancements. Another example of a flowchart and the equivalent Pseudocode is shown below. In this case, the program computes the sum, average and product of three numbers: Flowchart Pseudocode Read X, Y, Z Compute Sum (S) as X + Y + Z Compute Average (A) as S / 3 Compute Product (P) as X x Y x Z
  • 4.
    Write (Display) theSum, Average and Product Decisions (Switching logic) Switching logic consists of two components - a condition and a goto command depending on the result of the condition test. The computer can determine the truth value of a statement involving one of six mathematical relations symbolized in the table below: Meaning Equals Not Equal • Less than • Less than or equal to • Greater than • Greater than or equal to In practice, the computer is presented not with a true/false statement, but with a question having a "Yes" or "No" answer, for example if A = 10, B = 20, K = 5, and SALES = 10000, then:
  • 5.
    Condition (Question) Is A ==B? Is B > A? Is K Is SALES >= $5000.00? "Answer" No Yes Yes Yes With each question, the computer can be programmed to take a different course of action depending on the answer. A step in an algorithm that leads to more than one possible continuation is called a decision. In flowcharting, the diamond-shaped symbol is used to indicate a decision. The question is placed inside the symbol, and each alternative answer to the question is used to label the exit arrow which leads to the appropriate next step of the algorithm. The decision symbol is the only symbol that may have more than one exit. The example below shows the flowchart for a program that reads two numbers and displays the numbers read in decreasing order:
  • 6.
    The equivalent Pseudocodeis shown below. Note that with Pseudocode, indentation is used to show the various steps that apply to a decision: Read A, B If A is less than B BIG = B SMALL = A else BIG = A SMALL = B Write (Display) BIG, SMALL Loops Most programs involve repeating a series of instructions over and over until some event occurs. For example, if we wish to read ten numbers and compute the average, we need a loop to count the number of numbers we have read. Count loops are loops where the program must count the number of times operations are completed. The flowchart below illustrates a loop that counts from 1 to 10: Count loop flowchart While count loops work the exact number of times needed, in many cases we do not know how many times we want to do something. It is often dependent on the data provided to the
  • 7.
    program. Imagine wechange our problem to read and compute the average of a number of numbers. We won't know how many numbers there are but will read numbers until there are no more. Two alternative solutions (using Pseudocode) are shown below: pre-test loop: post-test loop: set average to zero set average to zero The flowchart shown on the left may be simplified to the form shown on the right. set count to zero set total to zero read number while ( not end-of-data ) increment count by 1 total = total + number read number if ( count > 0 ) then average = total / count display average
  • 8.
    set count tozero set total to zero Do read a number increment count by 1 total = total + number while ( not end-of-data ) if ( count > 0 ) then average = total / count display average Both these assume that the computer will tell the program when there is no more numbers. This is called an end-of-data or end-of-file test. There is an important difference between the pre-test and post-test loops. The pre-test version will work even if there are no numbers, the post-test version assumes the body of the code will be obeyed at least one time. Both forms of loops are appropriate in different circumstances. Looping with switching and goto's The looping and switching logic above follow well defined rules. In fact, we can implement any of these constructs with a condition and a goto (unconditional branch) instruction. An example of this logic is was illustrated in the loop flowchart shown previously. Early programs were written this way. As the problems became more complex it became impossible to follow the logic when things go wrong.
  • 9.
    Imagine trying tosort out code like this step 01: do step 02: go step 03: do step 04: if … step 16: if step 17: go step 18: do step 19: if step 20: go something to step 16 something some event has occurred go to step 19 some event has occurs go back to step 4 to step 1 something something is greater than 10 goto step 1 to step