Download presentation
Presentation is loading. Please wait.
1
CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009
2
2 Design Deficiencies Many design methods missing key constructs: –No inherent looping or recursion ability Looping is the process of repeating the same instructions multiple times in a row –Limited ability to define where data comes from
3
3 Design Needs Inexperienced programmers needed more guidance Insufficient to review –Lack of detail made it too difficult to gauge correctness and efficiency
4
4 New Design Called “pseudocode” –Literally means “Fake code” Uses verbal descriptions and code-like structures –Code used is based on Basic, which evolved into Visual Basic Pseudocode often used in addition to other design methods –Pseudocode is an example of Low-level design
5
5 Pseudocode constructs Execution block Input and Output Operations Decision Looping Branching
6
6 Execution Block BEGIN to start END to conclude Used to start a pseudocode application, as well as separate repeated or recursion blocks Each BEGIN must have a corresponding END
7
7 Execution Block Example BEGIN MyProgram : (These are ellipses. They denote that : statements exist, but I am not listing them) BEGIN RepeatBlock : END RepeatBlock : END MyProgram
8
8 Input and Output READ … [FROM source] –Can read directly into variables –If you need to specify source, use the optional FROM keyword WRITE … [TO location]
9
9 I/O Examples READ Num1, Num2 FROM keyboard WRITE Total TO File “Out.txt” WRITE Pay as Format Currency –This last version starts blurring the line between design and code, as you will see later in the course –Best to keep pseudocode DESCRIPTIVE, but not just like code
10
10 Operations SET – Used to put a value into a variable –Not used so much anymore –Mostly use equation sign as equation CALL – Invoke a stored routine –A stored routine is one that has been previously created and stored in the system Examples: –SET Ave = Sum/Count –CALL 3Sort(High, Med, Low)
11
11 Decision IF statement Condition must be stated to have only TRUE or FALSE as the answer Must have statement or statements to run if condition is TRUE Must close with ENDIF
12
12 IF statement forms Minimum: –IF condition THEN action Provide alternative –IF condition THEN action ELSE alternative Multiple checks IF condition THEN action ELSEIF new-condition THEN new-action ELSE alternative ENDIF
13
13 Decision Examples Check for higher value: IF A >= B THEN High = A ELSE High = B ENDIF Equal special case: IF A > B THEN PRINT “A Higher” ELSEIF A < B THEN PRINT “B Higher” ELSE PRINT “Equal” ENDIF
14
14 Looping DO Statement Two types: –DO WHILE(condition) … ENDWHILE –DO … UNTIL(condition) ENDDO WHILE tests condition before performing action(s) between DO and ENDWHILE UNTIL performs actions then tests –Guarantees that statements run at least once
15
15 DO WHILE Example Add first ten values: SET Count = 0 SET Sum = 0 DO WHILE(Count< 10) READ Value FROM file Sum = Sum + Value Count = Count + 1 ENDWHILE
16
16 DO UNTIL Example Read items from file: DO READ Element FROM file CALL ProcessElement(Element) UNTIL(EOF) ENDDO
17
17 Branching ON condition GOTO label Most often used for Error Handling: ON Error GOTO Cleanup : Cleanup: READ Error type from Error structure Note Cleanup: is a label. Labels are immediately followed by a colon
18
18 Pseudocode Example Let’s revisit the user login process This time, since we have a loop structure, we can give the user 3 chances We’ll use functions to check for valid userid and password
19
19 Algorithm Initialize count and validity flag Loop 3 times or until valid: –Get userid and password –Check for userid existence –Check for valid password –Both OK, set flag –Otherwise, ask user to try again
20
20 Solution BEGIN login SET Count = 0, valid = False DO READ userid, password FROM user exists = UserExists(userid) IF exists THEN valid = CheckPwd(userid, password) ENDIF
21
21 Solution, cont Count = Count + 1 IF valid = False THEN PRINT “Invalid login information. Try again” ENDIF UNTIL(valid OR Count = 3) ENDDO
22
22 Solution, concluded IF valid = False THEN BEGIN Terminate PRINT “Invalid login attempt” Terminate user END Terminate ENDIF END login
23
23 Another Example Let’s design the Bubble Sort algorithm discussed in Lecture #3 Takes a list of numbers, compares two at a time, and switches their positions if necessary Loops through list again if a switch was made
24
24 Bubble Sort design BEGIN BubbleSort SET switched to False Loop: Item = 1 DO If Item > Item(+1) Then switch Items SET switched to True End If
25
25 Bubble Sort Design, cont Increase Item by 1 UNTIL all items have been compared to its neighbor ENDDO ON (switched set to True) GOTO Loop END BubbleSort
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.