Showing posts with label computer logic. Show all posts
Showing posts with label computer 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?

    Monday, September 6, 2010

    Flowchart

    Flowchart Example
    Here is a simplified example of a flowchart for a program that calls a number for you until someone answers and says "Hello" for you. You'll notice at the beginning and the end there is the racetrack shaped symbol:

    Terminal Symbol


    This is called the terminal symbol and denotes the beginning and end of a program.



    After the terminal symbol you should notice the parallelogram shape:



    Input/Ouput Symbol

    This is the input/ouput symbol. Anytime a new constant or variable needs to be input or output, use this symbol. Here we use it to input phoneNumber and ouput the phrase "Hello".




    Next in the flowchart is the plain rectangle shape:


    Processing Symbol

    Here is the processing symbol. This is where something is done, input is used, and/or data is manipulated. Here we use it to call the input phoneNumber.


    The final shape looks like a diamond:

    Decision Symbol

    The diamond shape denotes the decision symbol. Here the computer checks to see if a statement is true or false and acts on that truth value as you have determined. Here we have said if the phone is not answered then call again. If it is, then the program finishes by saying "Hello" and ending.

    Note: The does not equal could have also been written as !=, <>, or simply "does not equal". This is your preference. Sometimes using does not equal makes it easier to understand what the program, rather than using "= no."

    Note 2: Yes and no can be denoted as 1 and 0 respectively.

    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.

    Tuesday, August 31, 2010

    An introduction to Computer Logic

    Logic is defined as the study of arguments. In logic you are finding the proper way to make a statement that is understandable and is a true statement. A computer program is exactly that, a series of arguments, all with a value of true or false. This makes sense when you think that computer code, binary, is a string of ones and zeros. The computer circuit can read the presence of electrical current as a 1 and absence as a 0. Even the most complex programs can be broken down into strings of true and false.