IT108  Data Structures
Instructor: Ms. Rosalie P. Alejandro
PSEUDOCODE
Program Design Language (PDL)  Represent logic in English-like manner  Easier to change
Rules for Pseudocode
Language independent
Indentation for readability
Key words in capital letters
Punctuation optional
IF ends with ENDIF
Ends DO, DO WHILE, DO FOR with ENDDO
Main routine shown first
Advantage of Pseudocode
Bridges gap between human language & computer language Logic expressed in straightforward way
Easy to understand
Good design tool
Easier to make changes
Allow structured walkthroughs
SEQUENCE/SELECTION
A DO B A B C ENDDO
N A Y B IF A THEN DO B ENDDO ENDIF
SELECTION
Y A N
IF COND-A THEN DO MOD-B ENDDO ELSE DO MOD-C ENDDO ENDIF
SELECTION (CASE)
Y A E IF COND-A THEN DO MOD-E ENDDO ELSE IF COND-B THEN DO MOD-F ENDDO ELSE IF COND-C THEN DO MOD-G ENDDO ELSE IF COND-D THEN DO MOD-H ENDDO ENDIF ENDIF ENDIF ENDIF
Y B F
Y C G
Y D H
SELECTION (CASE)
DO CASE CHOICE-A DO
MOD-E ENDDO CHOICE-B DO
MOD-F
ENDDO CHOICE-C DO MOD-G ENDDO CHOICE-D DO MOD-H ENDDO ENDCASE
ITERATION
N A Y B
DO WHILE COND-A DO MOD-B ENDDO ENDDO REPEAT DO MOD-B ENDDO UNTIL COND-A
B N A Y
PSEUDOCODE
The three basic program constructs
 Sequence  Selection  Iteration
PSEUDOCODE
Sequence
 A series of statements (instructions)  Pseudocode convention
       READ WRITE ACCEPT DISPLAY PRINT INPUT GET
 Consistent use of conventions throughout pseudocode
PSEUDOCODE
Pseudocode convention
Comments Statements that are non-executable
Indication: /**/
e.g. /* Comment aids legibility */
e.g. /* This is an example of a long
comment */
PSEUDOCODE
Pseudocode Convention
 Iteration: REPEATUNTIL
To repeat a given set of commands Condition is checked after actions REPEAT
commands UNTIL condition e.g. REPEAT ACCEPT number cube  number ** 3 DISPLAY number, cube UNTIL number = 0
PSEUDOCODE
Pseudocode Convention
 Do not use equal sign in assignment statements.
 Use  in assignment statements or words set to
 Use = sign only in conditional statements.
PSEUDOCODE
Pseudocode Convention
 Iteration: FOR loop For fixed number of iterations FOR index IN RANGE min TO max DO commands ENDDO e.g. FOR I IN RANGE 1 to 10 DO PRINT I, I**2 ENDDO
PSEUDOCODE
Pseudocode Convention
 Selection: IFTHEN Carry or ignore commands based on condition IF condition THEN
commands
ENDIF /* simple condition */ e.g. IF marks > 90 THEN class  First ENDIF
PSEUDOCODE
/* compound condition */ e.g. IF marks >= 80 and marks <= 90 THEN class  Second ENDIF
PSEUDOCODE
Pseudocode Convention
 IF-THEN-ELSEENDIF To select one of two or more alternative commands depending on a given condition IF condition THEN command sequence 1 ELSE command sequence 2 ENDIF e.g.IF marks >= 50 THEN remarks  Pass ELSE remarks  Fail ENDIF
PSEUDOCODE
Pseudocode Convention
 Multi-way selection Further extension of IF-THEN-ELSE construct to allow multiple selection IF condition 1 THEN
command sequence 1
ELSE IF condition 2 THEN command sequence 2 ELSE IF condition 3 THEN command sequence 3 ENDIF ENDIF ENDIF (indentation for easy reading)
PSEUDOCODE
Pseudocode Convention
 Selection: Multi-way selection CASEENDCASE DO CASE of index CASE index condition 1 command sequence 1 CASE index condition 2 command sequence 2 CASE index condition 3 command sequence 3 OTHERWISE command sequence ENDCASE
PSEUDOCODE
Pseudocode Convention
 Selection: Multi-way / selection CASEENDCASE e.g. DO CASE OF menu_choice CASE E DO enquiry routine ENDDO CASE U DO update routine ENDDO CASE X EXIT menu ENDDO OTHERWISE DISPLAY error message ENDCASE
PSEUDOCODE
Exercises
Use pseudocode to show the logic of a program which will output the total of a list of non-negative numbers input from the keyboard. The list of numbers is terminated with any negative number.
End of Slide