|
Home | Résumé | Courses | Contact | Useful Links | Favorite Links | USC - Homepage |
Introduction to Programming - (CPTR125) - Lectures
Lecture Outline
- How to write pseudocode
- Meaningful names
- The Structure Theorem
Pseudocode
.
How to write pseudocode
Pseudocode is writing an indented outline in some people language such as English. It has added key words to show the structure of the outline.
Six basic Computer operations
1. A computer can receive information
2. A computer can put out information
3. A computer can perform arithmetic
4. A computer can assign a value to a variable or memory location
5. A computer can compare two variables and select one of two alternative actions
6. A computer can repeat a group of actions
1. A computer can receive information
Computers receive information or input from some source when required, such as a keyboard, disk, or mouse. Some common commands for input are: INPUT, READ, GET, SCAN, etc. For example:
Read Student name
or
Get Today's date
2. A computer can output information
A computer can output some information to some device, such as a display, printer, or disk. Some common commands for output are: DISPLAY, PRINT, TYPE, PUT, PAINT, etc. For example:
Display "End of Data"
or
Print pay check
3. A computer can perform arithmetic
Computers were invented to perform arithmetic. If a person does arithmetic for hours at a time, he/she get bored. When bored, he/she gets creative and produces wrong answers. Computers don't get bored! Computers can do arithmetic and algebraic type calculations. Some common commands for arithmetic are: +, -, *, /, COMPUTE, CALCULATE, etc. For example:
Bill = Price + Tax
4. A computer can assign a value to a variable or memory location
Computers can assign or change the value of a variable. Some common command for assignment are: SET, =, STORE, INITIALIZE, etc. For example:
Set Total to 0
5. A computer can compare two variables and select one of two alternative actions
Computers can made decisions! A computer can compare two variables and use the result of the comparison to select between one of two alternative actions. Pseudocode uses the keywords: IF, THEN, and ELSE for decisions. If the question in the IF clause evaluates to True, the statements in the THEN path are executed. Otherwise the statements in the ELSE path are executed. Some common commands for making decisions are: IF/THEN, IF/THEN/ELSE, SWITCH, COMPARE, etc. For example: IF it is less than 65 degrees F, wear a sweater.
6. A computer can repeat a group of actions
The ability of a computer to execute a group of instructions 0 or more times, makes it worth while to write computer programs. Pseudocode uses the keywords: DOWHILE and ENDDO for defining the repeating instructions. Some common commands for repeat are: DOWHILE/ENDDO, WHILE/ENDWHILE, REPEAT/UNTIL, PERFORM, etc. For example:
WHILE there are uncounted students DO
Count the next student
ENDWHILE
Meaningful names
When designing a solution algorithm, a programmer must introduce some unique names to represent variables or objects.
All names should be meaningful. For example: hoursworked and hourlyrate are more meaningful than A and B!
The Structure Theorem
It is possible to write any program using only three control structure types. The three basic control structures are: sequence, selection, and repetition.
1. Sequence
A sequence is a list of instructions to be followed in the order written. Pseudocode shows the sequence control structure as:
instruction a
instruction b
instruction c
2. Selection
A selection presents a condition and uses the result of the condition to choice between two actions. Pseudocode shows the selection control structure as:
IF condition p is true THEN
The ELSE statement list can be empty.statement(s) (in true case)
ELSEstatement(s) (in false case)
ENDIF
3. Repetition
The repetition control structure is a set of instructions to be repeated while a certain condition is true. Pseudocode shows repetition as:
DOWHILE condition p is true
orstatement(s)
ENDDOWHILE condition p is true DO
statement(s)
ENDWHILE
Required reading: Simple Program Design - Lesley Anne Robertson - Chapter 2.
Used with permission - Copyright © 2005 by James L. Fuller - Simple Program Design - Lesley Anne Robertson - Course Technology.
|
Home | Résumé | Courses | Contact | Useful Links | Favorite Links | USC - Homepage |