Download presentation
Presentation is loading. Please wait.
1
Lecture 3 Pseudocode (S&G, §2.2)
5/4/2019 Lecture 3 Pseudocode (S&G, §2.2) 5/4/2019 CS Lecture 3 CS 100
2
Representation of Algorithms
Natural languages (English, Spanish, Chinese, …) Formal programming languages (Java, Pascal, C++, …) Pseudocode “a special set of English language constructs modeled to look like the statements available in most programming languages” compromise between natural & formal languages 5/4/2019 CS Lecture 3
3
Sequential Operations
Lecture 3 5/4/2019 Sequential Operations computing agent interface input computation output Note that Input xlates from physical quantities into arbitrtary media of computation, and Output vice versa external world 5/4/2019 CS Lecture 3 CS 100
4
Input Operation Example: Get a value for r, the radius of the circle
Lecture 3 5/4/2019 Input Operation Example: Get a value for r, the radius of the circle Get a value for r 25 17 25 r Note that we are not fussy about syntax, so long as meaning is clear Note that input can also come from a file, or a device (mouse, thermometer, etc.) General form: Get values for “variable”, “variable”, … “variable” means “any variable” (e.g., r) 5/4/2019 CS Lecture 3 CS 100
5
Output Operation Example: Print the value of Area
Lecture 3 5/4/2019 Output Operation Example: Print the value of Area Print the value of Area 26 26 Area Note that can also print to display, write to file, control a device, etc. General form: Print the values of “variable”, “variable”, … 5/4/2019 CS Lecture 3 CS 100
6
Print the message ‘Sorry, no answer’
Lecture 3 5/4/2019 Output Operation (2) Example: Print the message ‘Sorry, no answer’ Print the message ‘Sorry, no answer’ Sorry, no answer Note that can also print to display, write to file, control a device, etc. General form: Print the message ‘message’ 5/4/2019 CS Lecture 3 CS 100
7
Computational Operation
Lecture 3 5/4/2019 Computational Operation Example: Set the value of Area to r2 = 3.14… compute 3.14… (252) = 1963 1963 177 25 Area r Note that we assume that we can do computer-like operations, such as arithmetic. 2 General form: Set the value of “variable” to “arithmetic expression” 5/4/2019 CS Lecture 3 CS 100
8
Computation is Physically Instantiated
Lecture 3 5/4/2019 Computation is Physically Instantiated (2 cm/2)2 3 cm g/cm3 = g 2 cm 3 cm Hg Note that “3 cm” need not be 3 cm long, nor “127.7 g” weight g The physical quantities in real world are real, the numbers in the calculation are representations Nevertheless, for calculation to take place, they mut be physically instantiated (as pencil marks, for example) 127.7 g 5/4/2019 CS Lecture 3 CS 100
9
Computational Processes in General
Computation takes place in a medium in which patterns can be represented by means of which they can be manipulated Example computational media: electrical signals & charges light intensity, fluid pressure, molecular structure, quantum state, gear position (old), abacus beads, paper, blackboard, etc. 5/4/2019 CS Lecture 3
10
Input Processes in General
An input process translates an external physical situation into the computer’s internal representational medium Example physical situations: position of mouse or joystick position of key (depressed or not) signal from light sensor etc. 5/4/2019 CS Lecture 3
11
Output Processes in General
An output process translates the computer’s internal medium of representation into an external physical effect Example physical effects: characters on display screen or printer graphics images position of robot arm etc. 5/4/2019 CS Lecture 3
12
Conditional Operation
Lecture 3 5/4/2019 Conditional Operation true/false condition do one set of operations if true do another set of operations if false Called “true branch” (or consequent) and “false branch” (or alternate) Also, if one alternate is “do nothing” then in effect we have the choice of whether to do something or not (We could pick which to do by tossing a coin, but that sort of non-decision can be reduced to this form) continue 5/4/2019 CS Lecture 3 CS 100
13
Conditional Statement
Lecture 3 5/4/2019 Conditional Statement General: If “true/false condition” is true then “first set of algorithmic operations” Else (or Otherwise) “second set of algorithmic operations” (rest of algorithm) Example: If (ci 10) then Set the value of ci to (ci – 10) Set the value of carry to 1 Else Set the value of carry to 0 Note indenting to make structure clearer 5/4/2019 CS Lecture 3 CS 100
14
One-Branch Conditional
Lecture 3 5/4/2019 One-Branch Conditional true/false condition If “true/false condition” is true then “first set of algorithmic operations” (rest of algorithm) do one set of operations if true do nothing if false Called “true branch” (or consequent) and “false branch” (or alternate) Also, if one alternate is “do nothing” then in effect we have the choice of whether to do something or not (We could pick which to do by tossing a coin, but that sort of non-decision can be reduced to this form) continue 5/4/2019 CS Lecture 3 CS 100
15
Computer-Inspired Conditionals in Legal Drafting
Lecture 3 5/4/2019 Computer-Inspired Conditionals in Legal Drafting Subsection (a). IF AND ONLY IF (1) (A) A person has threatened or attempted suicide or to inflict serious bodily harm on himself, OR (B) The person has threatened or attempted homicide or other violent behavior, OR (C) The person has placed others in reasonable fear of violent behavior and serious physical harm to them, OR (D) The person is unable to avoid severe impairment or injury from specific risks, AND (2) There is a substantial likelihood that such harm will occur, THEN (3) The person poses a “substantial likelihood of serious harm” for purposes of subsection (b). This is called Normalized English Note that this format also allows computers to process the text and answer a variety of legal questions. — Tenn. Code Ann. sect (a) (1991) 5/4/2019 CS Lecture 3 CS 100
16
Basic Loop (Trailing Decision)
Lecture 3 5/4/2019 Basic Loop (Trailing Decision) do loop body if not, repeat test if done This is a sort of combination of the previous two: we do something AND then we decide to do it again OR not Note that this is not the only kind of iterative operation This form does the operation at least once if done, continue 5/4/2019 CS Lecture 3 CS 100
17
Iteration (Trailing Decision)
Lecture 3 5/4/2019 Iteration (Trailing Decision) Repeat steps i to j until “a true/false condition” becomes true step i: operation step i + 1: operation … step j: operation (rest of algorithm) termination condition loop body Note indenting If something in the loop body does not cause the termination condition to become true eventually, then the looping will go on forever This is a infinite loop Violates the condition that an algorithm must return a result in finite time 5/4/2019 CS Lecture 3 CS 100
18
Example Loop Set the value of count to 1
Repeat steps 3 to 5 until (count > 100) Set square to (count count) Print the values of count and square Add 1 to count 5/4/2019 CS Lecture 3
19
Alternate Notation Repeat until “a true/false condition” becomes true
“a set of algorithmic operations” End of loop 5/4/2019 CS Lecture 3
20
continue with rest of algorithm
Lecture 3 5/4/2019 Leading Decision Loop test if more and repeat While “a true/false condition” remains true do “a set of algorithmic operations” End of loop continue with rest of algorithm if not do loop body if more In this case test is called a “continuation condition” 5/4/2019 CS Lecture 3 CS 100
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.