Pseudocode - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Pseudocode

Description:

An algorithm is a sequence of steps taken to solve a problem. Definition. pseudo ... SEQUENCE one task is perfomed sequentially after another ... – PowerPoint PPT presentation

Number of Views:1542
Avg rating:3.0/5.0
Slides: 21
Provided by: CSU67
Category:

less

Transcript and Presenter's Notes

Title: Pseudocode


1
Pseudocode
  • notes from http//www.csc.calpoly.edu/jdalbey/SW
    E/pdl_std.html

2
What is Pseudocode
  • structured English for describing algorithms
  • simplified, half-English, half-code outline of a
    computer program
  • ________________________________
  • An algorithm is a sequence of steps taken to
    solve a problem.

3
Definition
  • pseudo
  • prefix, meaning similar
  • Pseudocode should not resemble any particular
    programming language
  • ignore syntax and particulars of a language
  • structured, formalized, condensed English
  • You can design solutions without knowing a
    programming language

4
Basic Constructs
  • SEQUENCE one task is perfomed sequentially after
    another
  • IF-THEN-ELSE decision between two alternative
    courses of action
  • CASE multiway branch decision based on value of
    an expression
  • WHILE loop with conditional test at beginning
  • FOR loop for counting loops
  • REPEAT-UNTIL loop with conditional test at bottom
    of loop

5
Common Action Keywords
  • Input READ, OBTAIN, GET
  • Output PRINT, DISPLAY, SHOW
  • Compute COMPUTE, CALCULATE, DETERMINE
  • Initialize SET, INIT
  • Add One INCREMENT, ADD one

6
IF-THEN-ELSE Structure
  • IF condition THEN
  • sequence 1
  • ELSE
  • sequence 2
  • ENDIF

7
IF-THEN-ELSE Example
  • IF HoursWorked gt NormalMax THEN
  • DISPLAY overtime message
  • ELSE
  • DISPLAY regular time message
  • ENDIF

8
CASE Structure
  • CASE expression OF
  • condition 1 sequence 1
  • condition 2 sequence 2
  • condition n sequence n
  • OTHERS default sequence
  • ENDCASE

9
CASE Example
  • CASE Title OF
  • Mr Print Mister
  • Mrs Print Missus
  • Miss Print Miss
  • Dr Print Doctor
  • OTHERS Print M.
  • ENDCASE

10
WHILE Structure
  • WHILE condition
  • sequence
  • ENDWHILE

11
WHILE Example
  • WHILE Population lt Limit
  • COMPUTE Population as Population Births -
    Deaths
  • ENDWHILE

12
WHILE Example
  • WHILE employee.type NOT EQUAL manager AND
    personCount lt numEmployees
  • INCREMENT personCount
  • CALL employeeList.getPerson with personCount
    RETURNING employee
  • ENDWHILE

13
FOR Structure
  • FOR interation bounds
  • sequence
  • ENDFOR

14
FOR Example
  • FOR each month of the year
  • DISPLAY number of days
  • ENDFOR
  • This psuedocode is great for a high-level
    description, but doesnt help us much with the
    implementation.
  • How could you write the psuedocode better?

15
REPEAT-UNTIL Structure
  • SET total to zero
  • REPEAT
  • READ Temperature
  • IF Temperature gt Freezing THEN
  • INCREMENT total
  • ENDIF
  • UNTIL Temperature lt zero
  • PRINT total

16
Calling Methods
  • CALL keyword
  • Examples
  • CALL AvgAge with StudentAges
  • CALL Swap with CurrentItem and TargetItem
  • CALL getBalance RETURNING aBalance
  • CALL Account.debit with CheckAmount

17
Examples
  • SET moveCount to 1
  • FOR each row on the board
  • FOR each column on the board
  • IF gameBoard position (row, column) is occupied
    THEN
  • CALL findAdjacentTiles with row, column
  • INCREMENT moveCount
  • ENDIF
  • ENDFOR
  • ENDFOR

18
Examples
  • GET number of bits in the binary number (HINT
    how many characters in the string?)
  • SET power to start as the number of bits in the
    binary number minus 1
  • SET sum to zero
  • LOOP for as many number of bits in the number
  • LET bit be the value at the next position
  • LET term be the value of the bit 2power
  • ADD term to the sum
  • SET power to power minus 1
  • ENDLOOP
  • RETURN decimal value of sum

19
Examples
  • SET total to zero
  • SET grade counter to zero
  • OPEN file
  • WHILE not at end of file
  • GET grade
  • ADD grade to total
  • INCREMENT counter
  • ENDWHILE
  • CLOSE file
  • SET average to total divided by counter
  • PRINT average

20
What do you do with it?
  • Each method and class should have a flowerbox
    that states the pseudocode for that particular
    method/class.
  • / Method calculates average of a list
  • Inputs list of grades as integers
  • Returns double-valued average
  • FOR size of list
  • GET current grade from list
  • ADD current grade to total
  • ENDFOR
  • SET average to total divided by number in list
  • RETURN average
  • /
  • public double calculateAverage( int listOfGrades
    )
  • double total 0
  • for( int i0 iltlistOfGrades.length i )
  • total listOfGradesi
  • double average total / listOfGrades.length
  • return average
Write a Comment
User Comments (0)
About PowerShow.com