IT som vrktj Bent Thomsen Institut for Datalogi
IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet November 2003 Bent Thomsen - FIT 5 -1 1
Introduction to Programming Bent Thomsen November 2003 Bent Thomsen - FIT 5 -1 2
Why learn about programming? • programming gets you more out of your computer • 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!” • 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 November 2003 Bent Thomsen - FIT 5 -1 3
Programs • 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. November 2003 Bent Thomsen - FIT 5 -1 4
You have already programmed! • You wrote complex formulas in Excel =$D 5*EKSP(-LN(2)*E$4/$C 5) • You used SQL to talk to databases SELECT * FROM contacts WHERE age BETWEEN 18 AND 35; • You will programme in MATLAB function r = fz(x) global M p w 1; X = [cos(x), sin(x); -sin(x), cos(x)]; r 1 = M' - p' - X*w 1'; r = r 1'*r 1; November 2003 Bent Thomsen - FIT 5 -1 5
Programming • Programming – the process of creating a program – the development of a solution to an identified program, and setting up of a related series of instructions which, when directed through computer hardware, will produce the desired results November 2003 Bent Thomsen - FIT 5 -1 6
Programming • 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 November 2003 Bent Thomsen - FIT 5 -1 7
What is an algorithm? • An algorithm is like a recipe. It lists the steps involved in accomplishing a task. • It can be defined in programming terms as a set of detailed and ordered instructions developed to describe the processes necessary to produce the desired output from the given input. November 2003 Bent Thomsen - FIT 5 -1 8
Simple rules…. When writing algorithms, there are some simple principles which must be adhered to. An algorithm must: 1. Be precise and unambiguous 2. Give the correct solution in all cases, and 3. Eventually end. November 2003 Bent Thomsen - FIT 5 -1 9
A simple algorithm… Here is a simple algorithm. Turn on cash till Clear cash till Repeat the following instructions Key in pounds amount Key in decimal point (. ) Key in pence amount Press plus (+) key Until all prices have been entered Press equal (=) key Store result Multiply by 0. 25 to calculate VAT Add the two numbers to get total Write down total price Turn off cash till November 2003 Bent Thomsen - FIT 5 -1 10
Programming Languages • A programming language is a set of rules that provides a way of telling a computer what operations to perform. November 2003 Bent Thomsen - FIT 5 -1 11
Levels of Programming Languages • • • Machine language Assembly Language High Level Languages Fourth Generation Languages (4 GL) Fifth Generation Languages (5 GL) November 2003 Bent Thomsen - FIT 5 -1 12
Machine Languages • different for each computer processor 0100 001101 100000 001101 110001 00101 10000 0111001. . . November 2003 Bent Thomsen - FIT 5 -1 13
Assembly Languages • different for each computer processor main November 2003 proc pay mov ax, dseg mov ax, 0 b 00 h add ax, dx mov a 1, b 1 mul b 1, ax mov b 1, 04 h Bent Thomsen - FIT 5 -1 14
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. • 4 GLs – Use high-level English-like instructions to specify what to do, not how to do it. November 2003 Bent Thomsen - FIT 5 -1 15
Types of high level Programming Languages • • Procedure-oriented languages Object-oriented languages Event-driven languages Declarative languages November 2003 Bent Thomsen - FIT 5 -1 16
Language Family Tree November 2003 Bent Thomsen - FIT 5 -1 17
Lots more Languages • There are many programming languages out there – specification languages, e. g. Z, UML – document languages, e. g. La. Te. X, Postscript – command languages, e. g. csh, MATLAB – query languages, e. g. SQL – Scripting languages, e. g. Perl, Python, Java. Script, VBScript, ASP, PHP, … November 2003 Bent Thomsen - FIT 5 -1 18
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 November 2003 Bent Thomsen - FIT 5 -1 19
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 November 2003 Bent Thomsen - FIT 5 -1 20
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 November 2003 Bent Thomsen - FIT 5 -1 21
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. November 2003 Bent Thomsen - FIT 5 -1 22
Calculate • A program can instruct a computer to perform mathematical operations. November 2003 Bent Thomsen - FIT 5 -1 Add 1 to Counter 23
Store • A program will often instruct a computer to store intermediate results. November 2003 Bent Thomsen - FIT 5 -1 Place 1 in Counter 24
Compare 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-ELSE November 2003 Bent Thomsen - FIT 5 -1 25
IF-THEN Entry Test condition p false Exit November 2003 true True statement a Bent Thomsen - FIT 5 -1 26
IF-THEN-ELSE Entry Test condition p “false” statement a November 2003 false true Exit Bent Thomsen - FIT 5 -1 “true” statement a 27
Iterate • A program loop is a form of iteration. A computer can be instructed to repeat instructions under certain conditions. No November 2003 Bent Thomsen - FIT 5 -1 28
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 November 2003 Bent Thomsen - FIT 5 -1 29
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. November 2003 Bent Thomsen - FIT 5 -1 30
DO WHILE Loop Entry Exit No Test condition p Yes Loop statement a November 2003 Bent Thomsen - FIT 5 -1 31
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. November 2003 Bent Thomsen - FIT 5 -1 32
DO UNTIL Loop Entry Loop statement a Test condition p November 2003 Exit No Bent Thomsen - FIT 5 -1 Yes 33
Programs are Solutions to Problems • Programmers arrive at these solutions by using one or more of these devices: • Pseudocode • Logic flowcharts • Structured Programming • UML • Object Oriented Programming November 2003 Bent Thomsen - FIT 5 -1 34
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. November 2003 Bent Thomsen - FIT 5 -1 35
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 November 2003 Bent Thomsen - FIT 5 -1 36
Logic Flowcharts • These represent the flow of logic in a program and help programmers “see” program design. November 2003 Bent Thomsen - FIT 5 -1 37
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. Processing. Indicates an operation performed by the computer, such as a variable assignment or mathematical operation. 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. Predefined Process. One statement denotes a group of previously defined statements. For instance, “Calculate m!” indicates that the program executes the necessary commands to compute m factorial. 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. November 2003 Flowlines connect the flowchart symbols and show the sequence of operations during the program execution. Bent Thomsen - FIT 5 -1 38
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 Output sum, vat, and total November 2003 Stop Bent Thomsen - FIT 5 -1 39
Structured 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. November 2003 Bent Thomsen - FIT 5 -1 40
Object 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) November 2003 Bent Thomsen - FIT 5 -1 41
The 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”) November 2003 Bent Thomsen - FIT 5 -1 42
Steps in program development 1. 2. 3. 4. 5. Define the problem Outline the solution Develop the outline into an algorithm Test the algorithm for correctness Code the algorithm into a specific programming language 6. Run the program on the computer 7. Document and maintain the program November 2003 Bent Thomsen - FIT 5 -1 43
The 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 November 2003 Bent Thomsen - FIT 5 -1 44
So 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. November 2003 Bent Thomsen - FIT 5 -1 45
- Slides: 45