Lecture Notes 82703 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Lecture Notes 82703

Description:

Assign a value to a variable (memory location) Compare two variables and select one of two alternative actions ... Otherwise, I'll press on the accelerator. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 22
Provided by: tyjo
Category:

less

Transcript and Presenter's Notes

Title: Lecture Notes 82703


1
Lecture Notes 8/27/03
  • Psuedocode

2
How to Write Psuedocode
  • A computer can perform 6 basic operations
  • Receive Information
  • Put out Information
  • Perform Arithmetic
  • Assign a value to a variable (memory location)
  • Compare two variables and select one of two
    alternative actions
  • Repeat a group of actions as long as a condition
    is met.

3
Receive Information
  • A computer generally gets information in one of
    two ways
  • From a file
  • From a peripheral device such as a keyboard.

4
Receive Information cont
  • In psuedocode, we will use two keywords to say
    that we are receiving information
  • Read
  • Typically used for receiving information from a
    file
  • Get
  • Typically used for receiving information from the
    keyboard.

5
Receive Information cont
  • Examples of psuedocode written to receive
    information
  • Get file name
  • Read first line from file
  • Get user name
  • Read second line from file

6
Put Out information
  • Computers typically output information in three
    ways
  • Print on a Printer
  • Write to a file
  • Display on a monitor

7
Put Out information cont
  • In psuedocode we will typically use the following
    keywords to indicate that we want the computer to
    output information
  • Print
  • Print information on a printer
  • Write
  • Output information to a file
  • Display
  • Display information on the screen

8
Put Out information cont
  • Examples of psuedocode to output information
  • Print goodbye cruel world
  • Write you have the blaster worm to help.txt
  • Display number_1

9
Perform Arithmetic
  • Most programs will require some arithmetic
    manipulation of data.
  • We can either use actual mathematical symbols or
    words to represent this.
  • Example
  • Add number_1 to total
  • Total Total number_1

10
Perform Arithmetic cont
  • Typical symbols we will be using in this class
    are
  • Addition.
  • Subtraction.-
  • Division./
  • Multiplication.
  • Modulus
  • Parentheses( )

11
Perform Arithmetic cont
  • We can also use the words compute and calculate
  • Example
  • Compute aa bb cc
  • Calculate C(F-32) 5/9

12
Assign a Value to a Variable
  • There are three contexts in which we can assign a
    value to a variable
  • Initialization
  • Assign a value as a result of some processing
  • Keep a piece of info for later use

13
Assign a Value to a Variable cont
  • For each context, there is a keyword/symbol in
    psuedocode.
  • Initialize
  • Setting a variable to some initial value
  • Assigning a value based on some sort of
    processing (arithmetic)
  • Store
  • Keep a value for later use

14
Assign a Value to a Variable cont
  • Examples
  • Initialize IQ to zero
  • IQ (age test_score)/zip_code
  • Store IQ in intelligence_total

15
Compare Two Variables and Select one of two
Actions
  • How is any choice made in life?
  • We make our choices based on conditions
  • Example
  • If there are two outs and the batter hits a
    fly-ball, Im running to the next base.
  • If the light is red, Ill press on the brakes.
    Otherwise, Ill press on the accelerator.
  • If I get really bored in this class, Ill put my
    head on my desk and sleep. Otherwise, Ill
    attentively listen and participate in this most
    excellent class session.

16
Compare Two Variables and Select one of two
Actions cont
  • Just like we make daily decisions in life based
    on conditions, we do the same thing in psuedocode
    using the keywords IF,THEN, ELSE, and
    ENDIF
  • Example
  • IF height gt 5 THEN
  • Add one to tall_people
  • ELSE
  • Add one to short_people
  • ENDIF

17
Repeat a Group of Actions
  • Again, we can draw analogies from real life
  • We often do things repetitively until a condition
    is met
  • Example
  • Hammering in a Nail
  • I hit the nail over and over until it is all the
    way in
  • Mowing your lawn
  • You turn the mower and push it across the lawn
    until all the grass is cut

18
Repeat a Group of Actions cont
  • We do the same thing in psuedocode using the
    keywords DOWHILE and ENDDO
  • Example
  • DOWHILE sheeplt3000
  • sheep sheep 1
  • ENDDO

19
The Structure Theorem Revisited
  • Again, it is possible to write any computer
    program by using only 3 basic control structures
  • Sequence
  • Statement a
  • Statement b
  • Selection
  • IF value_1 equals value_2 THEN
  • Value_1 -1
  • ELSE
  • Value_2-1
  • ENDIF
  • Repetition
  • DOWHILE your_age lt 690
  • add one to your_age
  • ENDDO

20
Summary
  • We now have all the tools necessary to write a
    psuedocode algorithm for just about any problem.
  • It might not be obvious, but by mixing, matching,
    and nesting the three control structures, we can
    do some pretty powerful stuff.
  • Read chapters 3 4 in Simple Program Design

21
Summary cont
Write a Comment
User Comments (0)
About PowerShow.com