Sunday, May 19, 2013

Pseudocode


Pseudocode - informal high-level description of a computer program or other algorithm, intended for human reading rather than machine reading.

Pseudocode uses a combination of programming terminology and plain English to describe algorithms in a form that is easier for people to understand than conventional programming language code. There are no standards for pseudocode and a program in pseudocode is not an executable program. Psuedocode is used in textbooks and scientific publications to describe various algorithms, and also in planning of computer program development, for sketching out the structure of the program before the actual coding takes place. It typically leaves out details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and some subroutines.
Example: Pseudocode
Below is an example of a pseudocode algorithm
For i = 1 to 100
    set print_number to true
    if i mod 3 equals 0
        print "Bizz" and set print_number to false
    if i mod 5 equals 0
        print "Buzz" and set print_number to false
    if print_number is true, print i
    print a newline
Converting this into VB.NET, we get:
dim print_number as boolean
For i = 1 to 100
    print_number = true
    if i mod 3 = 0 then
       console.writeline("Bizz")
       print_number = false
    end if
    if i mod 5 = 0 then
       console.writeline("Buzz")
       print_number = false
    end if
    if print_number then
       console.writeline(i)
    end if
    console.writeline()

[edit]Structured English

Structured English - A restricted part of the English language used to describe algorithms

Structured English is very similar to Pseudo code, but it tends not to use so many mathematical symbols. Often people start with Structured English, convert it to Pseudo Code and then write Executable Code. Examples of common keywords: START, BEGIN, END, STOP, DO, WHILE, DO WHILE, FOR, UNTIL, DO UNTIL, REPEAT, END WHILE, END UNTIL, END REPEAT, IF THEN, IF, ELSE, IF ELSE, END IF, THEN, ELSE THEN, ELSE IF, SO, CASE, EQUAL, LT, LE, GT, GE, NOT, TRUE, FALSE, AND, OR, XOR, GET, WRITE, PUT, UPDATE, CLOSE, OPEN, CREATE, DELETE, EXIT, FILE, READ, EOF, EOT, WITH, RETURN
Structured EnglishPseudo CodeExecutable Code
BEGIN 
 READ name
 IF name EQUAL "Harry" THEN
  WRITE "Why don't you marry Pippa?"
 ELSE
  WRITE "Are you Royal enough?"
 END IF
END
BEGIN 
 INPUT name
 IF name == "Harry" THEN
  OUTPUT "Why don't you marry Pippa?"
 ELSE
  OUTPUT "Are you Royal enough?"
 END IF
END
dim name as string
name = console.readline()
if name = "Harry" then
  console.writeline("Why don't you marry Pippa?")
else
  console.writeline("Are you Royal enough?")
End if
Exercise: Pseudocode
What are the rules when writing pseudocode?
 [Expand
Answer :
What is the difference between pseudocode and a programming language such as javascript?
 [Expand
Answer :
For the following pseudocode write a VB.NET equivalent:
for x from 1 to 7 inclusive
  add x to total
loop
print total
 [Expand
Answer :
Write pseudocode for the following problem:
Find the average of 4 numbers and display it
 [Collapse
Answer :
There are many ways to answer this correctly as long as it is clear you got it right. Ask your friend to check it.
input 4 numbers
sum = add numbers together
avg = sum / 4
print avg

No comments:

Post a Comment