Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park
Outline Historical Perspective Traditional Programming Concepts Procedural Units(next file) Language Implementation(next file) Object-Oriented Programming(skip) Programming Concurrent Activities(skip) Declarative Programming(skip)
Programming Language History ____________ languages(e.g., 5123) ____________ languages(e.g., ADDI R1, R2, R3) ____________ languages(e.g., sum = a + b)
First Generation Language Machine code Specific to the machine __________ _____ to write Even harder to read
Second Generation Language Assembly language Usually a 1-1 _________ to machine code, e.g., ADDI R1, R2, R3 instead of 5123 ______ to read and write Still specific to the machine architecture Better, but not by much
Assemblers Since assembly language is _____ to machine language, _________ can be done automatically ADDI R1,R2,R3Assembler5123
Third Generation Languages High level, machine ___________ Much easier to read and write, e.g., money = bills + coins instead of ADDI R1, R2, R3 Fortran and ______ are early examples
Compilers and Interpreters High-level code needs to be translated into machine language Compilers do so _______ of time Interpreters do so ___________ CompilerA = B * C
Programming Paradigms Languages can be classified by ________ Many different programming languages Only a few programming paradigms Imperative/Procedural programming Object-oriented programming Functional programming Logic/Declarative programming
Imperative Programming Traditionally the most _________ The approach we have seen so far Program is a _______ of steps Receives input Executes a sequence of commands for some computation Generates output Examples: Fortran, C, COBOL, Pascal
Object-Oriented Programming Program is a collection of ________ An object contains __________ describing how that object should respond to various ________ (icon object and list object, for example) Interaction between objects is via __________ passing Examples: Smalltalk, C++, Java, C#
Functional Programming This paradigm views the process of program development as connecting predefined “______ ______”, each of which accepts inputs and produces outputs Lisp Expression: (Divide (Sum Numbers) (Count Numbers)) Example: Lisp, Scheme, ML
Logic/Declarative Programming Describe the _________ not the solution The idea here is to discover and implement a ________ problem-solving algorithm Once this is done, problems can be solved by developing a precise _________ of the problem A major obstacle is the discovery of the underlying problem-solving algorithm For this reason early declarative languages were designed for use in _________ applications More recently, formal logic within ____________ gave a tremendous boost to this paradigm; this resulted in the emergence of logic programming (e.g., Prolog)
Evolution of Programming Paradigms
Traditional Programming Concepts We consider some of the concepts found in imperative and object-oriented programming languages We will draw examples from the languages C, C++, C#, Fortran, Java and Pascal Appendix D contains a brief background of each lang.
Variables Hold a temporary value that can _______ Some languages insist that a variable should have a ______, while others do not Example: Java To store an integer, use an int To store a real number, use a float To store a string, use a String
Data Types Common integer for ______ numbers (16 or 32 bits) float for ______ numbers (32 or 64 bits) character for data consisting of _________ (ASCII (8 bits) or Unicode(16 bits)) Sometimes available Boolean for 0 or 1 (1 bit) String for string of characters (variable length) Many others
Sample Variable Declarations
Literals Value is explicitly stated in a program int X = 10(10 is a literal) String Name = “Bob”(“Bob” is a literal) Wage = hours 9.00(9.00 is a literal) Although sometimes necessary, the use of literals should be _______ wherever possible
Literals: Problem Example AverageSales = Sales / 10 AverageSalary = Salary / 10 Pay = Hours 10 What does 10 mean in each of the previous examples? Literals can ______ the meaning of the statements in which they appear Literals can complicate the task of ________ the program _________ should be used wherever possible
Constants Variables that are not allowed to _____ Once defined, it can’t be changed later Why use constants? More ________ code _______ to modify code Pascalconst pi= ; const numDays=10; FortranREAL PARAMETER :: pi = INTEGER PARAMETER :: numDays=10 C,C++const float pi = ; const int numDays=10; Javastatic final float pi = ; static final int numDays=10;
Data Structures A way of thinking _________ data items as ______ larger data item Different operations can be performed on different data structures ________ is the most basic example We will see others later
Arrays: Homogeneous A structure that contains multiple values of the ______ kind An array of integers A string can be thought of as an array of _________ “This is a string”
Arrays: Homogeneous Example In C, C++, Java char B[20] = “This is a string”; You can access any item in an array Index starts at __, not at __ For instance, if we execute B[2] = ‘u’, the array above will become “_____________”
Arrays: Heterogeneous A structure that contains multiple values of different kinds
Assignment Statements Assign to a variable Value of another variable Result of a computation Result from a function C, C++, Java Total = Price + Tax; Pascal Total := Price + Tax;
Assignment Example What are A and B at the end of the following? int A, B; A = 2; B = 3; A = A+A; A = A+B; B = B B;
Operator Precedence What is A at the end of the following? int A=2, B=3; A = A / B + B * A – A + B; Most programming languages will do A = (A / B) + (B * A) – A + B; Use ___________ to override precedence A = A /( (B + B) * (A – (A + B)));
Control Statements Alter order of execution of statements in a program ______-entry / ______-exit Most common ones if-then-else while switch for
If Statement
While Statement
Switch Statement
For Statement
Comments ____________ statements within the code Should not just repeat the code, but __________ on it ________ by compiler C, C++, Java /* Insert Comments here */ // Or put them here