Professional Documents
Culture Documents
Common Words and Keywords Six Basic Computer Operations Structure Theorem Converting Flowchart To Pseudocode Vice Versa
Common Words and Keywords Six Basic Computer Operations Structure Theorem Converting Flowchart To Pseudocode Vice Versa
Common Words and Keywords Six Basic Computer Operations Structure Theorem Converting Flowchart To Pseudocode Vice Versa
Pseudocode
Objectives:
Example:
o Read student name (from the student master file).
o Get system date (from the computer system).
o Read student ID number.
o Get order.
Example:
o Print "End of the Output"
o Write student record to master file
o Put out name, address and post code
o 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?)
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:
o Divide total_score by student_count
o class_average = total_score / student_count
o 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:
0. Total = 10 + 15 * 2 / 4 ^ 2 -(2 + 3)
1. Total = 10 + 15 * 2 / 4 ^ 2 -(5)
2. Total = 10 + 15 * 2 / 16 -(5) ( 5 as a negative value)
3. Total = 10 + 15 * 2 / 16 - 5
4. Total = 10 + 30 / 16 - 5
5. Total = 10 + 1.875 - 5
6. Total = 11.875 - 5
7. Total = 6.875
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:
o Initialize total_score to 0: total_score = 0
o Set student_count to 0: student_count = 0
o total_score = total_score + score1
o student_count = student_count + 1
o class_average = total_score / student_count
o Store class_average in class_average_quiz1
5. A computer can compare two variables and select one of two alternative actions.
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.
Example:
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:
o 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).
o Example:
Statement 1
Statement 2
Statement 3 .
Selection:
o 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.
o Example:
Else
o As a result, only one step, either x or y, will be performed and the other step
will be skipped.
Repetition:
o 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.
o Example:
Read student_record
Print student.name
Print student.address
student_total = student_total + 1
EndDo
o 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
o
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.