Problem Solving: Pseudo code

From Wikibooks, open books for an open world
Jump to: navigation, search

UNIT 1 - ⇑ Problem Solving ⇑

← Trace tables Pseudo code Searching and sorting →

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
    output_number ← true
    if i mod 3 equals 0
        output "Bizz"
        print_number ← false
    if i mod 5 equals 0
        output "Buzz"
        print_number ← false
    if print_number equals true
        output i
    output 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()

Structured English[edit]

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 English Pseudo Code Executable 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?

Answer :

There are no set rules, but the code should provide clear descriptions of the algorithms being outlined.

What is the difference between pseudocode and a programming language such as javascript?

Answer :

Pseudocode cannot create executable code, whilst javascript can.

For the following pseudocode write a VB.NET equivalent:

for x from 1 to 7 inclusive
  add x to total
loop
print total

Answer :

dim total as integer = 0
for x = 1 to 7
  total = total + x
next
console.writeline(total)

Write pseudocode for the following problem:

Find the average of 4 numbers and display it

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