34
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Embed Size (px)

Citation preview

Page 1: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Chapter 6Programming Languages (1)

Introduction to CS

1st Semester, 2012 Sanghyun Park

Page 2: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 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)

Page 3: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Programming Language History ____________ languages (e.g., 5123)

____________ languages (e.g., ADDI R1, R2, R3)

____________ languages (e.g., sum = a + b)

Page 4: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

First Generation Language Machine code Specific to the machine __________ _____ to write Even harder to read

Page 5: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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

Page 6: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Assemblers Since assembly language is _____ to machine language,

_________ can be done automatically

ADDI R1,R2,R3 Assembler 5123

Page 7: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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

Page 8: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Compilers and Interpreters High-level code needs to be translated into machine

language Compilers do so _______ of time Interpreters do so ___________

CompilerA = B * C

1101

1202

5312

3300

Page 9: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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

Page 10: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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

Page 11: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Object-Oriented Programming Program is a collection of ________

An object contains __________ describinghow that object should respond to various ________(icon object and list object, for example)

Interaction between objects is via __________ passing

Examples: Smalltalk, C++, Java, C#

Page 12: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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

Page 13: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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)

Page 14: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Evolution of Programming Paradigms

Page 15: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Traditional Programming Concepts We consider some of the concepts found in imperative

and object-oriented programming languages

We will draw examples from the languagesC, C++, C#, Fortran, Java and Pascal

Appendix D contains a brief background of each lang.

Page 16: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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

Page 17: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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

Page 18: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Sample Variable Declarations

Page 19: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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

Page 20: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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

Page 21: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Constants Variables that are not allowed to _____ Once defined, it can’t be changed later Why use constants?

More ________ code _______ to modify code

Pascal const pi=3.14159;

const numDays=10;

Fortran REAL PARAMETER :: pi = 3.14159

INTEGER PARAMETER :: numDays=10

C,C++ const float pi = 3.14159;

const int numDays=10;

Java static final float pi = 3.14159;

static final int numDays=10;

Page 22: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Data Structures A way of thinking _________ data items

as ______ larger data item

Different operations can be performedon different data structures

________ is the most basic example

We will see others later

Page 23: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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”

12

334

43

693

23

43

345

Page 24: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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 “_____________”

Page 25: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Arrays: Heterogeneous A structure that contains multiple values of different kinds

Page 26: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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;

Page 27: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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;

Page 28: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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 doA = (A / B) + (B * A) – A + B;

Use ___________ to override precedenceA = A /( (B + B) * (A – (A + B)));

Page 29: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Control Statements Alter order of execution of statements in a program

______-entry / ______-exit

Most common ones if-then-else while switch for

Page 30: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

If Statement

Page 31: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

While Statement

Page 32: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

Switch Statement

Page 33: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

For Statement

Page 34: Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park

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