Outline Department of Mathematics and Computer Science Bronx Community College February 2, 2015
Outline Outline 1 Chapter 1 Cornerstones of Computing
Textbook Object-Oriented Programming in Python Goldwasser and Letscher
Outline 1 Chapter 1 Cornerstones of Computing
Functions Abstract vs. Concrete Operations Processing concrete (low-level) data is closer to the machine, using machine operations: moving bits into memory or registers, adding binary digits. Processing abstract (high-level) data hides the low-level details by combining many low-level steps into a single operation.
Functions Built-in Operations for Built-in Data Types Each built-in type has operations that go with it. Examples from Python: int or float types allow addition (+), subtraction (-), multiplication (), and division (/ or //). bool allows and, or, and not.
Functions Built-in Control Structures All programming languages have these commands to control flow of programs: A loop causes a sequence of statements to be repeated for as long as a particular condition is met. A conditional statement is executed only when a particular condition is met.
Functions User-Defined Functions Functions can be written to help solve specific problems in programs. Procedural Abstraction: Separation of Concerns There are two aspects to any function: What it does and how it gets it done. Using a function should not require knowing how it works. Procedural abstraction is the discipline to keep these two issues completely separate. Writing a function this way provides a new, high-level abstract operation.
An algorithm is a step-by-step procedure. It can use variables, conditional expressions, looping, and sequences of steps. It can be expressed in English, pseudocode, flowcharts, or languages such as Python or C++.
Example: calculating the greatest common divisor of two integers gcd(u, v) Set u and v u = v v = r r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) Set u and v u v r 54 42 u = v v = r r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) Set u and v u v r 54 42 u = v v = r r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) Set u and v u v r 54 42 12 u = v v = r r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) u v r Set u and v 54 42 12 42 12 u = v v = r r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) u v r Set u and v 54 42 12 42 12 u = v v = r r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) u v r Set u and v 54 42 12 42 12 6 u = v v = r r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) u v r Set u and v 54 42 12 42 12 6 u = v v = r 12 6 r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) u v r Set u and v 54 42 12 42 12 6 u = v v = r 12 6 r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) u v r Set u and v 54 42 12 42 12 6 u = v v = r 12 6 0 r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) u v r Set u and v 54 42 12 42 12 6 u = v v = r 12 6 0 6 0 r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) u v r Set u and v 54 42 12 42 12 6 u = v v = r 12 6 0 6 0 r = u % v No is v = 0? Yes The gcd is u
gcd(u, v) u v r Set u and v 54 42 12 42 12 6 u = v v = r 12 6 0 6 0 r = u % v No is v = 0? Yes The gcd is u
Low-level Languages Machine Language The central processing unit, or CPU of any computer electronically recognizes a small list of binary coded instructions (opcodes) for that CPU. This is called machine language. Assembly Language Early programming was done by using a mnemonic code (of text characters) for each opcode in a computer s machine language. A program, written using mnemonic codes, was assembled (translated) into machine language which could then run on the computer.
High-Level Languages Compiled vs. Interpreted Languages If the programs in a language are completely translated into machine language before they is executed, we say it is compiled language. If the programs in a language are translated and executed one statement at a time, the language is interpreted.
High-Level Languages Syntax vs. Semantics The syntax of a language is the set of rules that combines its parts (function and variable names, punctuation, and ẅhite space ) into meaningful statements. The semantics of a language is the meaning of any statement, which is derived from the meaning of its parts.
High-Level Languages Existing Languages C++ is a compiled object-oriented language. Python is an interpreted object-oriented language. Ruby is an interpreted object-oriented language. Perl is an interpreted object-oriented language. Java is a compiled object-oriented language.
High-Level Languages Level of Language is Relative From the viewpoint of machine language, assembly language is high-level, since it uses alphabetic codes ( add, jump, etc.) which are more meaningful than the binary opcodes of m achine language. From the viewpoint of Python, assembly language is low-level, since its commands are no more powerful than machine language.
High-Level Languages User-defined Data Types and Functions Produce Higher-Level Languages Once a new function has been defined, it can be used any time after that in the same way as a function that was built into the language. So the language has been changed into one that can use a new higher-level command. Once an abstract data type has been defined, it can be used any time after that in the same way as a datatype that was built into the language. So the language has been changed into one that can process a new kind of abstract data.