MIC 110

Pseudocode

Objectives:

Why use pseudocode?

  • Once pseudocode is created, it is simple to translate it into real programming code.
  • Opportunity to detect any logic error prior to actual coding, which is a lot more expensive and time consuming.
  • Used for planning the programming.

Common words and key words:

  • What can your computer do? Calculate, Read, Print, Convert.....
  • How do you instruct the computer to do what you intend?
  • Keywords: The words that are predefined and reserved to a computer to execute certain instruction. Example: Print, Read, and Compute. Keywords are reserved by a computer programming language, and they contain some particular definition and instruction. Examples in Visual Basic are Private, Sub, End, and Print.
  • Common words: Any other English words to complete the instruction. They include names of variables, constants, file names and so on. Common words are created and defined by a user.
Six basic computer operations:

1. A computer can receive information.

  • When a computer is required to receive information or input from a particular source, whether it be a keyboard, a disk or any other device, the verbs Read and Get are used in pseudocode. Read is usually used when the algorithm is to receive input from a record on a file, while Get is used when the algorithm is to receive input from the keyboard.

 

  • Example:
    • Read student name (from the student master file).
    • Get system date (from the computer system).
    • Read student ID number.
    • Get order.

2. A computer can put out information.

  • When a computer is requested to supply information or output to a device, the verbs Print, Write, Put, Output or Display are used in pseudocode. Print is used to send the output to the printer, while Write is used for writing to a file. For putting the information to a screen, the common keywords are Put, Display or Output.

 

  • Example:
    • Print "End of the Output"
    • Write student record to master file
    • Put out name, address and post code
    • Output grade; Display "an input error occurred, please re-enter."

     

  • In each example, the data to be put out is described concisely using mostly lower-case letters. (Why?)

 

3. A computer can perform arithmetic operation.

  • A programmer may use actual mathematical symbols or the words for those symbols.
  • For example: "Add score to total_score" is same as "total_score = total_score + score" (programming languages use mathematical equations to assign values to memory locations. In this case, a new value of total_score is assigned to a memory location named total_score by adding score to the current (not new) value of total_score)

 

  • The following symbols can be used in pseudocode: + for Add, - for Subtract, * for Multiply, / for Divide, ( ) for Parentheses
  • For more example:
    • Divide total_score by student_count
    • class_average = total_score / student_count
    • Compute C = (F - 32) * 5 / 9

     

  • When writing mathematical calculation for the computer, the order of operation should be considered; otherwise you may end up with incorrect values.
  • The order of operators are following:
    1. ( ) : Values within parentheses are always evaluated first. Ex:
    2. ^ : Exponentiation (raising a number to a power) is second Ex:
    3. - : Negation (creating a negative number) is third. Ex:
    4. * and / : Multiplication or division is fourth. Ex:
    5. \ : Integer division (a.k.a. Div) is fifth. Ex:
    6. Mod : Remainder division is sixth. Ex:
    7. + or - : Addition and subtraction are last.
  • Example: For an expression Total = 10 + 15 * 2 / 4 ^ 2 -(2 + 3) , the order of computation is following:
    1. Total = 10 + 15 * 2 / 4 ^ 2 -(2 + 3)
    2. Total = 10 + 15 * 2 / 4 ^ 2 -(5)
    3. Total = 10 + 15 * 2 / 16 -(5) ( 5 as a negative value)
    4. Total = 10 + 15 * 2 / 16 - 5
    5. Total = 10 + 30 / 16 - 5
    6. Total = 10 + 1.875 - 5
    7. Total = 11.875 - 5
    8. Total = 6.875

4. A computer can assign a value to a variable or memory location.

  • There are three cases where you may write pseudocode to assign a value to a variable or memory locations
    1. To give data an initial value. The verbs Initialize or Set are used.
    2. To assign a value as a result of some processing, the symbol " = " is used.
    3. To keep a piece of information for later use, the verbs Save or Store is used.

     

  • Example:
    • Initialize total_score to 0: total_score = 0
    • Set student_count to 0: student_count = 0
    • total_score = total_score + score1
    • student_count = student_count + 1
    • class_average = total_score / student_count
    • Store class_average in class_average_quiz1

5. A computer can compare two variables and select one of two alternative actions.

  • For this operation, special keywords are used: If..Then or If..Then..Else.
  • The comparison of data is established in the If clause, and the choice of alternatives is determined by the Then or Else options. Only one of these alternatives will be performed.
  • Example:

    If student is part_time Then

    add 1 to part_time_count

    Else

    add 1 to full_time_count

    EndIf

    If the student is a part-time student, then "add 1 to part_time_count" is performed. Otherwise, the computer skips to the Else clause to perform "add 1 to full_time_count" instruction.

6. A computer can repeat a group of actions.

  • When there is a sequence of processing steps that need to be repeated, keywords Do While and EndDo, are used. And processing stems in between those keywords should be repeated as long as the condition for initiation is met.

 

  • Example:

    Do While student_total < 30

    Read student record

    Print student name, address

    Add 1 to student_total

    EndDo

    In this example, the set of instruction in the Do While loop will be performed repeatedly as long as student_total is less than 30. And each time computer goes through the loop, the value of student_total will be incremented by 1, in which eventually will make the student_total to be equal to 30, and terminate the loop.

 

The Structure Theorem: How does a computer know what to do first and to do next? Go to www.wiley.com for graphical samples.
  • Sequence:
    • The sequence structure controls the straightforward execution of one processing step after another. For example, within a module, a computer executes the first line of instruction first before the second. And it processes the following instruction in the order of appearance unless it encounters a selection (If..Then) or a loop (WhileDo).
    • Example:

      Statement 1

      Statement 2

      Statement 3 .

  • Selection:
    • The selection structure (If .. Then) represents the decision-making abilities, and allows a programmer to set what will be the next step depends on the condition.
    • Example:

      If condition k is true Then

      step x is the next

      Else

      step y is the next

      EndIf

      As a result, only one step, either x or y, will be performed and the other step will be skipped.
  • Repetition:
    • A loop (Do While) executes a same set of instruction as long as the initial condition for the loop is met. At the end of a loop, a computer goes back to the beginning of the loop and tests the condition. If the condition is met, it goes through the loop one more time. If the condition is not met, then it terminates the loop and continues to execute instructions from the end of the loop.

     

    • Usually, a loop contains a counter which increments or decrements each time the loop is executed. And the counter with the new value is used in determining whether the look should be repeated or terminated.

     

    • Example:
      The variable student_data acts as a counter.

      student_total = 0

      Do While student_total < 25

      Read student_record

      Print student.name

      Print student.address

      student_total = student_total + 1

      EndDo

    • Question: Based on the above codes, if there were 25 students in the file, would this algorithm print all 25 students? Count how many time the loop occure.

      Modification:

      student_total = 1

      DoWhile student_total <= 25 And student_record <> " " Read student_record

      Print student.name

      Print student.address student_total = student_total + 1

      EndDo

Convert the following flowchart to pseudocode.

 

 

 

Pseudocode Example

This is the pseudocode for a Game of Monopoly, including one person's move as a procedure:

Main Procedure

Monopoly_Game Hand out each player's initial money. Decide which player goes first. Repeat Call Procedure Monopoly_Move for next player. Decide if this player must drop out. Until all players except one have dropped out. Declare the surviving player to be the winner.

Procedure Monopoly_Move

Begin one's move. Throw the dice. Move the number of spaces on the board shown on the dice. If the token landed on "Go to Jail," then go there immediately. Else if the token landed on "Chance" or "Community Chest," then draw a card and follow its instructions. Else follow the usual rules for the square (buying property, paying rent, collecting $200 for passing "Go", etc.). End one's move.