IT - Som Værktøj: Bent Thomsen Institut For Datalogi Aalborg Universitet
IT - Som Værktøj: Bent Thomsen Institut For Datalogi Aalborg Universitet
IT - Som Værktøj: Bent Thomsen Institut For Datalogi Aalborg Universitet
Bent Thomsen
Institut for Datalogi
Aalborg Universitet
Bent Thomsen
Why learn about programming?
• programming teaches you how to solve problems
• programming helps you be more precise
(doesn’t 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 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
Programming Languages
• A programming language is a set of rules
that provides a way of telling a computer
what operations to perform.
Levels of Programming
Languages
• Machine language
• Assembly Language
• High Level Languages
• Fourth Generation Languages (4GL)
• Fifth Generation Languages (5GL)
Machine Languages
• different for each computer processor
0100
001101 100000 001101 110001
00101 10001 10000
01110
111001
. . .
Assembly 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
High-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 .
Types of high level Programming
Languages
• Procedure-oriented languages
• Object-oriented languages
• Event-driven languages
• Declarative languages
Procedure-Oriented Languages
• FORTRAN
• COBOL
• Pascal
• C
• Ada
OOED Languages
• Object-oriented languages
– Smalltalk
– C++
– Ada 95
– Java
– C#
• Event-driven languages
– Visual Basic
– most Visual languages
Declarative languages (5GL)
• Functional(?): Lisp, Scheme, SML
– Also called applicative
– Everything is a function
• Logic: Prolog
– Based on mathematical logic
– Rule- or Constraint-based
Language
Family
Tree
Lots 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, …
What 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
Why 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
What 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
Sequence Control Structures
• Sequence control structures direct the order
of program instructions.
• The fact that one instruction follows another
—in sequence—establishes the control and
order of operations.
Calculate
• A program can
instruct a computer Add 1 to
to perform Counter
mathematical
operations.
Store
Entry
Exit
True
statement a
IF-THEN-ELSE
Entry
Test
condition p false true
“false” “true”
statement a Exit statement a
Iterate
• A program loop is a
form of iteration. A
computer can be
instructed to repeat
instructions under
certain conditions.
No
Iteration 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
Leading 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.
DO WHILE Loop
Entry
Exit
Test No
condition p
Yes
Loop
statement a
Trailing 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.
DO UNTIL Loop
Entry
Loop
statement a
Exit
Test No Yes
condition p
Programs are Solutions
to Problems
• Programmers arrive at these solutions by
using one or more of these devices:
• Logic flowcharts
• Pseudocode
• Structured Programming
• UML
• Object Oriented Programming
Logic Flowcharts
• These represent the
flow of logic in a
program and help
programmers “see”
program design.
Common Flowchart Symbols
Common Flowchart Symbols
Terminator. Shows the starting and ending points of the program. A terminator
has flowlines in only one direction, either in (a stop node) or out (a start node).
Data Input or Output. Allows the user to inputdata and results to be displayed.
Decision. The diamond indicates a decision structure. A diamond always has two
flowlines out. One flowlineout is labeled the “yes” branch and the other is labeled the
“no” branch.
Connector. Connectors avoid crossing flowlines, making the flowchart easier to read.
Connectors indicate where flowlines are connected. Connectors come in pairs, one with
a flowline in and the other with a flowline out.
Off-page connector. Even fairly small programs can have flowcharts that extend several
pages. The off-page connector indicates the continuation of the flowchart on another
page. Just like connectors, off-page connectors come in pairs.
Flowline. Flowlines connect the flowchart symbols and show the sequence of operations
during the program execution.
Flowchart for a
Cash Register Program
Start
sum=0
Input price
sum=sum+price
Yes More
items?
No
vat=sum x 0.25
total=sum+vat
Stop
Psuedocode
• This device is not visual but is considered a
“first draft” of the actual program.
• Pseudocode is written in the programmer’s
native language and concentrates on the logic
in a program—not the syntax of a
programming language.
Pseudocode for a
Cash Register Program
sum=0
While More items do
Input price
sum=sum+price
End While
vat=sum x 0.25
total=sum+vat
Output sum, vat, total
Structured Programming