Year 1 LESSON 6 DESIGN TOOL PSEUDOCODE Program

  • Slides: 22
Download presentation
Year 1 LESSON 6 DESIGN TOOL PSEUDOCODE ¨ Program Design Language (PDL) ¨ Represent

Year 1 LESSON 6 DESIGN TOOL PSEUDOCODE ¨ Program Design Language (PDL) ¨ Represent logic in English-like manner ¨ Easier to change CS 112/0401/V 1 Lesson 6 - 1

Year 1 Rules for Pseudocode ¨ Language independent ¨ Indentation for readability ¨ Key

Year 1 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 CS 112/0401/V 1 Lesson 6 - 2

Year 1 Advantage of Pseudocode ¨ Bridges gap between human language & computer language

Year 1 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 CS 112/0401/V 1 Lesson 6 - 3

Year 1 SEQUENCE/SELECTION CS 112/0401/V 1 Lesson 6 - 4

Year 1 SEQUENCE/SELECTION CS 112/0401/V 1 Lesson 6 - 4

Year 1 SELECTION CS 112/0401/V 1 Lesson 6 - 5

Year 1 SELECTION CS 112/0401/V 1 Lesson 6 - 5

Year 1 SELECTION (CASE) CS 112/0401/V 1 Lesson 6 - 6

Year 1 SELECTION (CASE) CS 112/0401/V 1 Lesson 6 - 6

Year 1 SELECTION (CASE) DO CASE CHOICE-A DO MOD-E ENDDO CHOICE-B DO MOD-F ENDDO

Year 1 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 CS 112/0401/V 1 Lesson 6 - 7

Year 1 ITERATION CS 112/0401/V 1 Lesson 6 - 8

Year 1 ITERATION CS 112/0401/V 1 Lesson 6 - 8

Year 1 PSEUDOCODE ¨ The three basic program constructs · Sequence · Selection ·

Year 1 PSEUDOCODE ¨ The three basic program constructs · Sequence · Selection · Iteration CS 112/0401/V 1 Lesson 6 - 9

Year 1 PSEUDOCODE ¨ Sequence · A series of statements (instructions) · Pseudocode convention

Year 1 PSEUDOCODE ¨ Sequence · A series of statements (instructions) · Pseudocode convention – READ – WRITE – ACCEPT – DISPLAY – PRINT – INPUT – GET · Consistent use of conventions throughout pseudocode CS 112/0401/V 1 Lesson 6 - 10

Year 1 PSEUDOCODE ¨ Pseudocode convention Comments Statements that are non-executable Indication: /*…*/ e.

Year 1 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 */ CS 112/0401/V 1 Lesson 6 - 11

Year 1 PSEUDOCODE ¨ Pseudocode Convention · Iteration: REPEAT…UNTIL To repeat a given set

Year 1 PSEUDOCODE ¨ Pseudocode Convention · Iteration: REPEAT…UNTIL 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 CS 112/0401/V 1 Lesson 6 - 12

Year 1 PSEUDOCODE ¨ Pseudocode Convention · Do not use equal sign in assignment

Year 1 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. CS 112/0401/V 1 Lesson 6 - 13

Year 1 PSEUDOCODE ¨ Pseudocode Convention · Iteration: FOR loop For fixed number of

Year 1 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 CS 112/0401/V 1 Lesson 6 - 14

Year 1 PSEUDOCODE ¨ Pseudocode Convention · Selection: IF…THEN Carry or ignore commands based

Year 1 PSEUDOCODE ¨ Pseudocode Convention · Selection: IF…THEN Carry or ignore commands based on condition IF condition THEN commands ENDIF /* simple condition */ e. g. IF marks > 90 THEN class ‘First’ ENDIF CS 112/0401/V 1 Lesson 6 - 15

Year 1 PSEUDOCODE /* compound condition */ e. g. IF marks >= 80 and

Year 1 PSEUDOCODE /* compound condition */ e. g. IF marks >= 80 and marks <= 90 THEN class ‘Second’ ENDIF CS 112/0401/V 1 Lesson 6 - 16

Year 1 PSEUDOCODE ¨ Pseudocode Convention · IF-THEN-ELSE…ENDIF To select one of two or

Year 1 PSEUDOCODE ¨ Pseudocode Convention · IF-THEN-ELSE…ENDIF 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 CS 112/0401/V 1 Lesson 6 - 17

Year 1 PSEUDOCODE ¨ Pseudocode Convention · Multi-way selection Further extension of IF-THEN-ELSE construct

Year 1 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 (indentation for easy reading) CS 112/0401/V 1 Lesson 6 - 18

Year 1 PSEUDOCODE ¨ Pseudocode Convention · Selection: Multi-way selection CASE…ENDCASE DO CASE of

Year 1 PSEUDOCODE ¨ Pseudocode Convention · Selection: Multi-way selection CASE…ENDCASE 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 CS 112/0401/V 1 Lesson 6 - 19

Year 1 PSEUDOCODE ¨ Pseudocode Convention Selection: Multi-way / selection CASE…ENDCASE e. g. DO

Year 1 PSEUDOCODE ¨ Pseudocode Convention Selection: Multi-way / selection CASE…ENDCASE 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 · CS 112/0401/V 1 Lesson 6 - 20

Year 1 PSEUDOCODE Exercises 1. Write a pseudocode segment that reads 3 numbers A,

Year 1 PSEUDOCODE Exercises 1. Write a pseudocode segment that reads 3 numbers A, B and C and display the biggest value of the three numbers. CS 112/0401/V 1 Lesson 6 - 21

Year 1 PSEUDOCODE Exercises 2. Write a pseudocode segment for a program that calculates

Year 1 PSEUDOCODE Exercises 2. Write a pseudocode segment for a program that calculates the BONUS on a salesman’s sale. Zero if SALES is less than $2000 Ten percent of SALES if amount is between $2000 to $4000 Five percent of SALES plus $1000 if amount is $4000 or above CS 112/0401/V 1 Lesson 6 - 22