02 introduction programming - york university · what is a program? • a program is ... • to...

48
01-1 An Introduction To Programming

Upload: others

Post on 24-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-1

An IntroductionTo

Programming

Page 2: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-2

What is Programming?

Page 3: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-3

What is Programming? – 2•  Specifying what to to do and when to do it

Page 4: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-4

What is Programming? – 3•  Specifying what to to do and when to do it•  The what consists of the following

» At the assembler level the hardwired instructions

> add, load, store, move, etc.

Page 5: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-5

What is Programming? – 4•  Specifying what to to do and when to do it•  The what consists of the following

» At the assembler level the hardwired instructions

> Add, load, store, move, etc.

» At the Eiffel, C, Java level> Assignment, arithmetic, read/write> Routines from a Subprogram library, API

(Application Program Interface)

Page 6: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-6

What is Programming? – 5•  The when consists of specifying in what order

to do the "what" operations

Page 7: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-7

What is Programming? – 6•  The when consists of specifying in what order

to do the "what" operations» Control structures

Page 8: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-8

What is Programming? – 7•  The when consists of specifying in what order

to do the "what" operations» Control structures

> What are the fundamental control structures?

Page 9: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-9

What is Programming? – 8•  The when consists of specifying in what order

to do the "what" operations» Control structures – these are the only

ones> Sequence> Choice> Loop

Page 10: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-10

What is Programming? – 9•  The when consists of specifying in what order

to do the "what" operations» Control structures – these are the only

ones> Sequence> Choice> Loop

• What and when are intertwined» Changing one generally requires changing

the other

Page 11: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-11

What is a program?•  A program is an algorithm expressed in a

programming language

Page 12: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-12

What is a program?•  A program is an algorithm expressed in a

programming language

•  An algorithm is a detailed sequence of actions to perform to accomplish some task.

Page 13: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-13

What is a program?•  A program is an algorithm expressed in a

programming language

•  An algorithm is a detailed sequence of actions to perform to accomplish some task.» Named after an Iranian mathematician

Al-Khwarizmi

Page 14: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-14

What is a program?•  A program is an algorithm expressed in a

programming language

•  An algorithm is a detailed sequence of actions to perform to accomplish some task

» Named after an Iranian mathematicianAl-Khwarizmi

•  Technically, an algorithm must reach a result after a finite number of steps

Page 15: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-15

An Example Algorithm•  To convert Celsius to degrees Fahrenheit

1.  Multiply the Celsius temperature by 9

2.  Divide the result by 5

3.  Add 32 to the result

Page 16: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-16

Algorithm Key elements•  Notice that there are 3 key elements in this

example

Page 17: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-17

Algorithm Key elements•  Notice that there are 3 key elements in this

example

1.  What to do – the operation

Page 18: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-18

Algorithm Key elements•  Notice that there are 3 key elements in this

example

1.  What to do – the operation

2.  What to do it to – the data

Page 19: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-19

Algorithm Key elements•  Notice that there are 3 key elements in this

example

1.  What to do – the operation

2.  What to do it to – the data

3.  When to do it – the sequence

Page 20: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-20

Algorithm Key elements•  The operations that can be performed on

data are defined for each programming language and associated with special symbols and keywords

Page 21: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-21

Algorithm Key elements•  The operations that can be performed on data are

defined for each programming language and associated with special symbols and keywords

•  The data elements that can be manipulated fall into two categories» Literal – data that is a constant value

» Variable – data that can change value

Page 22: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-22

Algorithm Key elements•  The operations that can be performed on data are

defined for each programming language and associated with special symbols and keywords

•  The data elements that can be manipulated fall into two categories

» Literal – data that is a constant value

» Variable – data that can change value

•  The order in which the instructions are executed is determined by their sequence

Page 23: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-23

Key elements of Programs• While the example demonstrates several key

elements of algorithms it omits an essential element of all programs

Page 24: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-24

Key elements of Programs• While the example demonstrates several key

elements of algorithms it omits an essential element of all programs

Input / Output (I/O)

Page 25: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-25

Key elements of Programs• While the example demonstrates several key

elements of algorithms it omits an essential element of all programs

Input / Output (I/O)

•  To be useful a program must accept input data and produce output data

Page 26: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-26

Key elements of Programs•  Input / Output

Page 27: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-27

Key elements of Programs•  Input / Output

» How a program communicates with the user

•  Logic

Page 28: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-28

Key elements of Programs•  Input / Output

» How a program communicates with the user

•  Logic» The order in which activities are carried out

Page 29: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-29

Key elements of Programs•  Input / Output

» How a program communicates with the user

•  Logic» The order in which activities are carried out

•  Data

Page 30: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-30

Key elements of Programs•  Input / Output

» How a program communicates with the user

•  Logic» The order in which activities are carried out

•  Data» Its storage and manipulation

Page 31: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-31

Evolution of I/O•  Early in the history of computing, programs

were submitted on punch cards with all the data they required and executed together with other programs that used the same libraries. Output was to a line printer.

•  Later developments introduced interactive processing which allowed the user to provide data while the program was running. This normally took place in a Question & Answer format.

Page 32: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-32

Evolution of I/O•  The development of the Graphic User

Interface (GUI) provided the opportunity for another development

Page 33: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-33

Evolution of I/O•  The development of the Graphic User

Interface (GUI) provided the opportunity for another development

•  The GUI is not restricted to interacting with the user one line at a time

Page 34: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-34

Evolution of I/O•  The development of the Graphic User

Interface (GUI) provided the opportunity for another development

•  The GUI is not restricted to interacting with the user one line at a time» It can present several options to the user

and respond to whichever is selected

Page 35: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-35

Language Paradigms•  The evolution of I/O reflected changes in the

way programmers looked at programming

Page 36: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-36

Language Paradigms•  The evolution of I/O reflected changes in the

way programmers looked at programming•  Several paradigms have been developed

Page 37: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-37

Language Paradigms•  The evolution of I/O reflected changes in the way

programmers looked at programming

•  Several paradigms have been developed

• What is a paradigm?

Page 38: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-38

Language Paradigms•  The evolution of I/O reflected changes in the way

programmers looked at programming

•  Several paradigms have been developed

• What is a paradigm?» A set of assumptions, concepts values,

and practices that consititute a way of viewing reality.

Page 39: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-39

Imperative or Procedural Model•  Algorithms are expressed as a hierarchy of

tasks.» Fortran, Cobol, Basic, C, Pascal, Ada

Page 40: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-40

Functional Model•  Computation is expressed in terms of the

evaluation of functions•  A solution is expressed in terms of function

calls.» Functions are both input and output

» Lisp, Scheme, and ML

Page 41: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-41

Logic Model•  Based on symbolic logic

» A program consists of> A set of facts about objects> A set of rules about relationships

between the objects> A way to ask questions about the

objects and their relationships

» PRO(gramming) in LOG(ic)> Prolog

Page 42: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-42

Object-Oriented Model•  Views the world as interacting objects• Objects are active and send messages to

each other» SIMULA, Smalltalk, C++, Java, Eiffel

Page 43: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-43

Event Driven Programming•  Clicking has become a major form of input to

a computer

Page 44: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-44

Event Driven Programming•  Clicking has become a major form of input to a computer

• Mouse clicking is not within the sequence of the program

Page 45: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-45

Event Driven Programming•  Clicking has become a major form of input to a computer

•  Mouse clicking is not within the sequence of the program

•  A user can click a mouse at any time during the execution of a program

Page 46: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-46

Event Driven Programming•  Clicking has become a major form of input to a computer

•  Mouse clicking is not within the sequence of the program

•  A user can click a mouse at any time during the execution of a program

•  The system consists of» An interface of objects that the user can

manipulate

Page 47: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-47

Event Driven Programming•  Clicking has become a major form of input to a computer

•  Mouse clicking is not within the sequence of the program

•  A user can click a mouse at any time during the execution of a program

•  The system consists of» An interface of objects that the user can

manipulate

» A collection of routines that will be executed when each event occurs

Page 48: 02 Introduction Programming - York University · What is a program? • A program is ... • To convert Celsius to degrees Fahrenheit 1. Multiply the Celsius temperature by 9 2. Divide

01-48

Event Driven Programming•  You still have to deal with the thee key

elements» I /O» Data

» Logic