PsuedoCode

I believe that before you can even begin to write programs you need to be able to think like a programmer. Basically you need to be able to break down a problem into the simplest of ideas and thoughts. You can do this before you ever learn a programming language by learning how to write the problem’s solution out in English or as we tend to call it psuedocode.

Psuedocode is an informal high-level description of the operating principle of a computer program or other algorithm.

The first few lessons will be focusing on turning everyday simple machines into psuedocode. By the end we will be implementing simple games like guess my number and hangman in psuedocode.

But before we begin trying our hand at some psuedocode we first must learn how to write psudeocode so that it can easily be read and in turn translated into an actual program.

Psuedocode

The first thing you need to do is break down the problem into smaller pieces until you have a basic statement. As an example we will draw a simple house with one door, two windows and a chimney, like the one below.

full-house

How would you draw this house?

Let’s break the house down into it’s base components

  • 1 square for the house
  • 1 triangle for the roof
  • 1 rectangle for the door
  • 2 rectangles for the windows
  • 4 lines for the window frames
  • 1 partial rectangle for the chimney
  • 1 circle for the door knob

Does it matter which order you draw the shapes in? Probably not. But in this case let’s assume we have to draw the base pieces first.

For example:

  1. First draw the square for the base of the house.
  2. Next draw the triangle for the roof of the house.
  3. Next draw the rectangle for the door.
  4. Next draw the circle for the door knob.
  5. Now draw a rectangle for one window.
  6. Draw a vertical line over the window.
  7. Draw a horizontal line over the window.
  8. Now draw a rectangle for the second window.
  9. Draw a vertical line over the window.
  10. Draw a horizontal line over the window.
  11. Finally draw the partial rectangle for the chimney

The above example is a sequential example. This is the simplest of programs to illustrate. However most programs are not sequential, they require loops or some kind of decision making branch.

A simple example of both a loop and a decision making branch is that of an on/off switch.

While there is power attached to the switch
    if the switch is turned on
        apply power to the device
    if the switch is turned off
       turn off power to the device

This example is missing a few things. What state is the switch in? How do you know if there is still power attached? But before we go here, lets take a look at what was written.

First you will notice that I have indented my lines. This indicates that everything indented done on the same iteration of the loop or branch.

Currently this while look will either run continuously as we have not made the while statement false or removed the power from the device.

Here is a more through example of the above psuedocode.

Power is attached
Switch state is off

While there is power attached to the switch
    Flip the switch
    if the switch is turned on
        apply power to the device
        turn switch state to on
    if the switch is turned off
       turn off power to the device
       turn switch state to off
    if the power is not attached 
        Power is not attached (end while)

The first two lines of code initialize some variables that are used with in the while loop. These are assumptions that need to be made for the code to work. We are assuming that a power source is attached to our device, and that the device is currently off.

Because there is power to our device now when we flip the switch, we can now turn on and off our device until the power source is removed.

This is just some basic examples. We will delve further into writing our own psuedocode in the next few lessons.

For some additional reading feel free to check out:
Psuedocode Standard
Psuedocode
Psuedocode or Code?

Advertisements
Tagged

One thought on “PsuedoCode

  1. Bekah says:

    Thank you! I’ll have William read this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: