Title: IT
1IT som værktøj
- Bent Thomsen
- Institut for Datalogi
- Aalborg Universitet
2Introduction to Programming
3Why learn about programming?
- programming teaches you how to solve problems
- programming helps you be more precise
(doesnt win you many friends though!) - why did the computer scientist stay in the shower
forever? - the instructions on the shampoo said lather,
rinse, repeat! - programming gets you more out of your computer
- you may not be programming, but
- knowing a little bit about Computer Science
- and knowing a little bit about Programming
- will help you work with people who do
4Programs
- A program is a set of step-by-step instructions
that directs the computer to do the tasks you
want it to do and produce the results you want.
5You have already programmed!
- You wrote complex formulas in Excel
- D5EKSP(-LN(2)E4/C5)
- You used SQL to talk to databases
- SELECT FROM contacts WHERE age BETWEEN 18
AND 35 - You programmed in MATLAB
- function r fz(x)
- global M p w1
- X cos(x), sin(x) -sin(x), cos(x)
- r1 M' - p' - Xw1'
- r r1'r1
6Programming
- Programming consists of two steps
- algorithmic design (the architects)
- coding (the construction workers)
- Programming requires
- a programming language to express your ideas
- a set of tools to design, edit, and debug your
code - either
- a compiler to translate your programs to machine
code - a machine to run the executable code
- or
- an interpreter to translate and execute your
program
7Programming Languages
- A programming language is a set of rules that
provides a way of telling a computer what
operations to perform.
8Levels of Programming Languages
- Machine language
- Assembly Language
- High Level Languages
- Fourth Generation Languages (4GL)
- Fifth Generation Languages (5GL)
9Machine Languages
- different for each computer processor
- 0100
- 001101 100000 001101 110001
- 00101 10001 10000
- 01110
- 111001
- . . .
10Assembly Languages
- different for each computer processor
- main proc pay
- mov ax, dseg
- mov ax, 0b00h
- add ax, dx
- mov a1, b1
- mul b1, ax
- mov b1, 04h
11High-Level Languages
- Higher Level Languages
- Use traditional programming logic where the
programming instructions tell the computer what
to do and how to perform the required operations. - 4GLs
- Use high-level English-like instructions to
specify what to do, not how to do it .
12Types of high level Programming Languages
- Procedure-oriented languages
- Object-oriented languages
- Event-driven languages
- Declarative languages
13Procedure-Oriented Languages
- FORTRAN
- COBOL
- Pascal
- C
- Ada
14OOED Languages
- Object-oriented languages
- Smalltalk
- C
- Ada 95
- Java
- C
- Event-driven languages
- Visual Basic
- most Visual languages
15Declarative languages (5GL)
- Functional(?) Lisp, Scheme, SML
- Also called applicative
- Everything is a function
- Logic Prolog
- Based on mathematical logic
- Rule- or Constraint-based
16LanguageFamily Tree
17Lots more Languages
- There are many programming languages out there
- specification languages, e.g. Z, UML
- document languages, e.g. LaTeX, Postscript
- command languages, e.g. csh, MATLAB
- query languages, e.g. SQL
- Scripting languages, e.g. Perl, Python,
JavaScript, VBScript, ASP, PHP,
18What determines a good language
- Formerly Run-time performance
- (Computers were more expensive than programmers)
- Now Life cycle (human) cost is more important
- Ease of designing, coding
- Debugging
- Maintenance
- Reusability
- FADS
19Why so many?
- Why does some people speak French?
- Most important the choice of paradigm, and
therefore language, depends on how humans best
think about the problem - Other considerations
- efficiency
- compatibility with existing code
- availability of tools
20What can a program do?
- A program can only instruct a computer to
- Sequence
- Calculate
- Store data
- Compare and branch
- Iterate or Loop
- Write Output
- Read Input
21Sequence Control Structures
- Sequence control structures direct the order of
program instructions. - The fact that one instruction follows anotherin
sequenceestablishes the control and order of
operations.
22Calculate
- A program can instruct a computer to perform
mathematical operations.
Add 1 to Counter
23Store
- A program will often instruct a computer to store
intermediate results.
Place 1 in Counter
24Compare and Branch
- A program can instruct a computer to compare two
items and do something based on a match or
mismatch which, in turn, redirect the sequence of
programming instructions. - There are two forms
- IF-THEN
- IF-THEN-ELSE
25IF-THEN
Test condition p
26IF-THEN-ELSE
27Iterate
- A program loop is a form of iteration. A computer
can be instructed to repeat instructions under
certain conditions.
No
28Iteration Control Structures
- Iteration control structures are looping
mechanisms. - Loops repeat an activity until stopped. The
location of the stopping mechanism determines how
the loop will work - Leading decisions
- Trailing decisions
29Leading Decisions
- If the stop is at the beginning of the iteration,
then the control is called a leading decision. - The command DO WHILE performs the iteration and
places the stop at the beginning.
30DO WHILE Loop
31Trailing Decisions
- If the stop is at the end of the iteration, the
control mechanism is called a trailing decision. - The command DO UNTIL performs the iteration and
puts the stop at the end of the loop.
32DO UNTIL Loop
Loop statement a
33Programs are Solutionsto Problems
- Programmers arrive at these solutions by using
one or more of these devices - Logic flowcharts
- Pseudocode
- Structured Programming
- UML
- Object Oriented Programming
34Logic Flowcharts
- These represent the flow of logic in a program
and help programmers see program design.
35Common Flowchart Symbols
36Flowchart for aCash Register Program
37Psuedocode
- This device is not visual but is considered a
first draft of the actual program. - Pseudocode is written in the programmers native
language and concentrates on the logic in a
programnot the syntax of a programming language.
38Pseudocode for aCash Register Program
sum0 While More items do Input price
sumsumprice End While vatsum x
0.25 totalsumvat Output sum, vat, total
39Structured Programming
- Structured program languages lend themselves to
flowcharts and pseudocode. - Structured programming languages work best where
the instructions have been broken up into small,
manageable parts.
40Object Oriented Programming
- Everything is an object
- A program is a bunch of objects telling each
other what to do by sending messages - Each object has its own memory made up of other
objects - Every object has a type
- All objects of a particular type can receive the
same messages - (Alan Kay)
41The object concept
- An object is an encapsulation of data and
behaviour, modeled after real-world objects - An object is an instance of an abstract data type
- An abstract data type is implemented via a class
- An object has
- identity (a unique reference)
- state (also called characteristics)
- behaviour
- Behaviour is implemented via methods
- Methods are often implemented using structured
programming - An objects methods and state are access via dot
notation - I.e document.write(Hello World)
42The Program Development Cycle
Analyze the problem
Design the solution algorithm
Design the user interface
Write the code
Test and debug the program
Complete the documentation
43Programming and Debugging
- Write code
- Syntax
- Rules of the language
- Logic
- Order of execution of various parts of the program
44Programming and Debugging
- Programming Errors
- Syntax error
- Misuse of syntax
- e.g., typing fer instead of for
- Logic errors
- Unintended operation of program
- e.g., Infinite loop
45Programming and Debugging
- Debugging
- Tracing and resolving errors in a program
- Coined by Admiral Grace Hopper
- Moth short-circuited a relay
- bug in the system
- Removed it ? system debugged
- Not an exact science more a black art
- Human against evil machine!
46So really, why learn about programming?
- Programmers make lots of money.
- Programming really is fun.
- Programming is very intellectually rewarding.
- Programming makes you feel superior to other
people. - Programming gives you complete control over an
innocent, vulnerable machine, which will do your
evil bidding with a loyalty not even your pet dog
can rival.