This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Introduction to PHP

About Pseduocode

Matthew Buscemi's picture
Sun, 2011-01-16 06:16

by Matt Buscemi - January 15-16, 2011

Writing code means writing instructions for your computer, orchestrating exactly how it will behave.  This would be a easy task if computers had "thought" in the same way that we humans do.  Humans communicate instructions to each other quite easily rapidly, using the power of human speech.  However, computers don't think like we do, and they have no innate mechanism for processing human speech.  Hence, code.

It's quite incredible to think that everything on your computer screen right now -- the browser, the mouse cursor, the applications and desktop, documents, everything, is, underneath, one enormous collection of ones and zeros.  That's it.  Ones and zeros.  And every time you move the mouse, or click, or you browse to a new webpage, or some web server somewhere sends your computer a response, those ones and zeros change in flurry, your screen gets updated (probably thirty times or more per second, actually) and the whole thing is managed by layer after layer or human generated code, itself also reduced down to ones and zeros at its core.

Us humans usually don't bother with programming in zeros and ones.  Well, those of us who want to keep our sanity don't bother with it, anyway.  Us sane coders write instructions that the computer can understand in a kind of half-way language.  It's not really a human language, but certainly easier to read than ones and zeros.  There are many such computer programming languages.

Learning Programming Languages

Lots of people have different philosophies about the best way to learn a programming language.  I'm not going to pretend I know the best one or any kind of definitive solution.  Suffice it to say, one solution that has been tossed out there is the act of writing pseudocode before you write real code.  I happen to think it's a pretty good practice.  I may be compelled otherwise if I see evidence to the contrary.

What's pseudocode though?  Well, if you've never programmed before, and if you go and look at a computer program of any decent length, it will probably look like a horrific mess of gobbledegook.  It might as well be ancient Sanskrit or pre-Han Dynasty Chinese script for all you -- a jumbled mess of punctuation and nonsense words.  Needless to say, the learning curve will seem repressively steep.  Pseudocode exists in order to help you with that.  When you look at pseudocode, you see a visual layout of instructions, including paths to show which to step to execute, one after another, until the program completes.

Pseudocode is a visual representation of actual code that leaves out the difficult syntax, the part of the programming language that's hard to learn in the first place.  It strips down a computer program into just its basic instructions, and shows them in a way that is clear and immediately understandable to humans, the computer and its ones and zeros be damned.

The List and the Flowchart

There are two basic ways you could write pseudocode.  The first, and simplest, is a list of instructions.  Here's an example:

  1. Print some text asking the user how old s/he is.
  2. Get some input from the user.
  3. Store the user's input under the name "age".
  4. Figure out if the user typed a number.
    1. If the user didn't type a number, print an error and exit the program
    2. Otherwise, continue
  5. Subtract the value in "age" from 80, and store that value under the name "years to eighty"
  6. Figure out what year it is now, add the value in "years to eighty", and store that value under the name "eighty year"
  7. Print the following text:
    1. "You will be eighty years old in "
    2. the value in "eighty year"

See?  No actual code whatsoever.  This pseudocode describes a computer program that asks the user how old he or she is, then figures out the year in which the user will be eighty years old and displays that value.

It's still not the easiest thing to understand.  The steps get even clearer if we visualize them in a flowchart.  The same program described above looks like this in flowchart form.

Decision Points

Step number four in the list above is an example of a decision point in the program.  In actual code, these are more commonly called "conditionals".  The basic idea is this: the program tests whether a given statement is true or false (in the example above, the statement is "the user input is a number"), and then follows one set of instructions if the statement turns out to be true, and another set of instructions if the statement turns out to be false.

Can you find the decision point or conditional statement in the example flowchart from the sign up task?

Loops

It is also possible to create loops, where the same chunk of instructions is repeated over and over again, usually with a different value in some piece of data each time.

It is important that something changes each time through the loop even if that something is just counting the number of times the loop has occurred.  Otherwise, the loop will continue looping infinitely.  I probably don't need to tell you that this is very, very bad for a computer.

Can you find the looping structure in the example flowchart from the sign up task?

Comments

what's an example where a

maddox bayer's picture
maddox bayer
Wed, 2011-01-19 23:49

what's an example where a loop would be needed?

Loops are never really

Matthew Buscemi's picture
Matthew Buscemi
Thu, 2011-01-20 06:06

Loops are never really needed, but they are infinitely more efficient than the obvious alternative. Think about which is easier for the computer in a case like this: You want to print the numbers one to four each on a new line.

The first way to do this would involve printing each number one at a time:
1. print "1" and a line break
2. print "2" and a line break
3. print "3" and a line break
4. print "4" and a line break

The second way involved a loop:
1. store the value 1 somewhere and name it "count"
2. print the value in "count" and a line break
3. increase the value in "count" by 1
4. go back to step 2 if "count" is less than or equal to five

Both ways take four steps in this example. But, imagine what happens if you want to print the numbers one to ten. Or how about one to one hundred? In each of these cases, the second way still only take four steps, where as the first way will take a huge number of steps.

Looping mechanisms are more efficient whenever you want to do the same thing many times, or when you want to do many similar things. Printing HTML lists (<ul> and <ol>) to the screen dynamically is usually accomplished with loops.

Hi Matt In your example of an

Hasan Hasan's picture
Hasan Hasan
Sat, 2011-01-22 15:56

Hi Matt

In your example of an mp3 download site, your flowchart does not show how the program prints the "track numbers" as you've described in the comment above. I assumed that for that site this data is already included in the data set along with "name of song" and "mp3 link".

In the wiki sign-up task however, it's not clear to me whether you expect the flowchart to show specifically how the program prints content item "numbers" (i.e. like in the example above) in the table of contents. In your instructions you've specified that each content item has "title" and "content" data only - you do not specify content item "number" - so I've assumed we must incorporate a loop in order to show specifically how the program would print a numbered list in the table of contents. I hope I have interpreted this correctly?

My apologies if this should have been address to you directly, but for some reason I was not able to input text into the 'message' field on your contact page.

I think (in the HTML) does

Michelle Chinese's picture
Michelle Chinese
Sat, 2011-01-22 21:11

I think "ol" (in HTML) does the numbering for you ("ol" = ordered list; such as numbered), so as long as you put the PHP code that prints content item titles between "ol" tags (to be precise, within "li" tags nested inside of the "ol" tags), whatever it prints will be auto-numbered. I think.