Showing posts with label programming logic. Show all posts
Showing posts with label programming logic. Show all posts

Tuesday, September 7, 2010

Introduction to Pseudocode

First off, what is pseudocode? It is merely a combination of: pseudo (meaning almost, approaching, or trying to be) and code (the language you are writing in; the set of instructions in a program). It is an informal written representation of your program. In pseudocode you don't need to follow syntax requirements, and really as long as it makes sense to you it doesn't matter how you do it. Here are a few guidelines you can follow however to get the most use out of your pseudocode:

  1. Begin and end your code with terminal statements (i.e. begin,end,start,stop).
  2. Don't use terminal statements if you don't like them.
  3. Use indentation freely to increase readability.
  4. Don't overuse indentation.
  5. Make it make sense to you.
Basically it all boils down to, how do you want to do it? As you learn more about if then, while, do while, statements it becomes more clear what to do. Here is a short example using the same program in my Flowchart introduction.

             start
               input phoneNumber
               call phoneNumber
               if phoneAnswered = no
                 call phoneNumber
               else output "Hello"
             end


Does anyone else have some tips or pointers when using pseudocode?

    Thursday, September 2, 2010

    Structured Programming

    Structured programming states that any program can be created through the use of three procedures: sequence, selection, and repetition.

    1. Sequence: One thing happens, then another, then another... Get bowl, Place bowl, fill bowl with 1 cup cereal, put 1/4 cup milk in bowl, scoop cereal, eat cereal. To make this work each process must be done in order, else you may be eating your cereal off of the table.

    2. Selection: Selection is the decision portion of your program. If you don't have a bowl in front of you, then grab a bowl. If you have a bowl to put on the table, then put it on the table. If there is no cereal in your bowl, then put cereal in the bowl... by now I'm sure you get the gist.

    3. Repetition: Repetition tells the program to do something again and again as needed (or specified by you, the programmer). At the end you can add a selection, if hungry repeat. You already have a bowl, so the program continues to the next step. The bowl is filled (because you have only taken one bite), you continue until you are either no longer hungry or the bowl is empty, in which case the bowl would refill and you would eat until no longer hungry.

    Anything else you want to do can be done by placing one or more of these within themselves (i.e. nesting statements) but every programming element is a composition of one or more of these three.

    The examples given are not necessarily complete, there are practically an infinite number of steps that could be inserted (breathing between bites, wait time between bites, chewing, swallowing, etc. etc. etc.), but you should have a general understanding of structured programming.