Learn to Program—Stage 3 Pseudocode examples Part 1


In this post, I will show you some examples of pseudocode.  I will also demonstrate some of the tips that were in my Cheat Sheet post.

Basic Pseudocode with an input and output:

START
     num numberOne
     num numberTwo
     num answer
     PRINT “Enter number 1”
     INPUT numberOne
     PRINT “Enter number 2”
     INPUT numberTwo
     PRINT “The answer is “, numberOne + numberTwo
STOP

An example of pseudocode which uses a constant:

START

     string HEADER = “This is a constant.  It won’t be changed at all”
     num numberOne
     num numberTwo
     num answer
     PRINT HEADER
     PRINT “Enter number 1”
     INPUT numberOne
     PRINT “Enter number 2”
     INPUT numberTwo
     PRINT “The answer is “, numberOne + numberTwo
STOP

An example of a simple if then else statement:

START
     num numberOne
     num numberTwo
     PRINT “Enter number 1”
     INPUT numberOne
     PRINT “Enter number 2”
     INPUT numberTwo
     IF numberOne > numberTwo THEN
         PRINT “Number 1 is larger”
     ELSE
         PRINT “Number 1 is not larger”
     ENDIF
STOP

An example of nested if statements:

START
     num numberOne
     num numberTwo
     PRINT “Enter number 1”
     INPUT numberOne
     PRINT “Enter number 2”
     INPUT numberTwo
     IF numberOne > numberTwo THEN
         PRINT “Number 1 is larger”
     ELSE
         IF numberOne == numberTwo THEN
              PRINT “The numbers are equal”
         ELSE
             PRINT “Number 1 is not larger”
         ENDIF
     ENDIF
STOP

You should notice two things in the previous pseudocode:  1) The blocks are lined up with each other vertically. 2)  LIFO (the second if-then-else statement is ended before the first).  Your pseudocode and code (including HTML code for webpages) should look like this.  Typically everything is indented 3 or 4 spaces.

Also, you will notice that I did a print statement before each input.  Get in the habit of this now—as your users are not psychic, so you need to tell them what you want before you ask for it (the input statement).

An example of a while statement:

START
     string name
     num age
     string telephoneNumber
     GET name, age, telephoneNumber
    WHILE NOT EOF
          PRINT name, age, telephoneNumber
          GET name, age, telephoneNumber
    ENDWHILE
    PRINT “Finished processing the data file”
STOP

In this example, when the flow hits the endwhile, it will check to see if the condition is met from the data it has.  This is why you always get a new piece of data before you hit the endwhile.  Otherwise, you’ll compare the first line and print it continually (infinite loop).

An example of a simple FOR Loop:

START
     num count
     num LASTNUMBER = 5
     FOR count = 1 to LASTNUMBER
          PRINT count
     ENDFOR
STOP

You don’t need to increment count, as the for loop does this for you.  If you don’t list a STEP at the end of the FOR statement, then it assumes you want to add 1.

An example of the proper way to break a FOR Loop early:

START
     num count
     num LASTNUMBER = 5
     num TESTNUMBER = 4
     num average
     num totalScore
     num score
     count = 0
     FOR COUNT = 1 to TESTNUMBER
          PRINT “Enter the score of the “, count, “ test”
          INPUT score
          totalScore = totalScore + score
     ENDFOR
     count = 0
     average = totalScore / TESTNUMBER
     FOR count = 1 to LASTNUMBER
           IF average > 70
                PRINT “You passed”
                count = LASTNUMBER + 1
           ENDIF
     ENDFOR
STOP

This isn’t the best example, as it really doesn’t do anything.  However I will show this again later on with an array in it.  Then it will make a little more sense.  For now, just understand that if the average is > 70, it will print out “You passed” and quit checking (by setting count to a number outside of the range of the FOR loop).  This is because once you have an answer, you don’t need (or want) to continue looking.

An example of a FOR Loop with a step other than one:

START
     num count = 0
     FOR count = 1 TO 30 STEP 2
          PRINT count
     ENDFOR
STOP

This will print 1, 3, 5, 7, 9, 11, 13,….,27, 29 and end.  You don’t need to increment count because the STEP 2 sets count = count + 2.

In my next post, I will demonstrate arrays and introduce you to methods.

Have a great day:)
Patrick.

Leave a comment

Your email address will not be published. Required fields are marked *