230 likes | 414 Views
AS Computing. Introduction to Programming. What is a Computer Program?. A list of instructions that a computer must work through, in a logical sequence, to carry out a specific task Computer programs are written in a programming language. Programming Languages.
E N D
AS Computing Introduction to Programming
What is a Computer Program? • A list of instructions that a computer must work through, in a logical sequence, to carry out a specific task • Computer programs are written in a programming language
Programming Languages • Many different languages and many different versions of same language • Each language has a vocabulary and rules that define arrangement - SYNTAX Fewer words to learn than a foreign language Rules are more rigid than a foreign language
Generations of Programming Languages • Computers have been around since 1940s • Early computers programmed by changing switch settings to represent binary data Bletchley Park
First Generation – Machine Code 0010100100100101001001110110101011010100111110110100001010101010110101010101011011011010 • Processors can only work with Binary Digits • All instructions given to a computer must be in a pattern of zeros and ones
Second Generation – Assembly Language • Use words rather than 0s and 1s • Simple translation of 1 word to 1 machine code instruction • Uses mnemonics: LDA Load Accumulator STA Store Accumulator ADD Adds to Accumulator SUB Subtracts from Accumulator
Low Level Languages ;// ADDRESS OPCODE ASM ; ---------------------------------- $0000 $2A82 GOTO hifi_main $0004 $ delays_delay_1us: $0004 $0000 nop $0005 $0000 nop $0006 $0008 RETURN $0007 $ delays_delay_5500us: $0007 $300F MOVLW 15 $0008 $1303 BCF STATUS,RP1 $0009 $1283 BCF STATUS,RP0 $000A $00F0 MOVWF STACK_0 $000B $30FF MOVLW 255 $000C $00F1 MOVWF STACK_1 $000D $ delays_L_8: $000D $0BF0 DECFSZ STACK_0, F $000E $2810 GOTO delays_L_9 $000F $2813 GOTO delays_L_10 $0010 $ delays_L_9: $0010 $0BF1 DECFSZ STACK_1,F $0011 $2810 GOTO delays_L_9 $0012 $280D GOTO delays_L_8 $0013 $ delays_L_10: $0013 $303E MOVLW 62 $0014 $00F0 MOVWF STACK_0 $0015 $ delays_L_11: $0015 $0BF0 DECFSZ STACK_0, F • Machine Code & Assembly language are: • Processor Dependant • Collectively known as Low Level Languages
Third Generation - High Level Languages • Problem oriented languages, easy to understand if Age >=17 Then Showmessage(‘Can drive a car’) • Based on what the program must do, rather than what the processor and memory do • PORTABLE – programs can be used on different types of computer BASIC JAVA PASCAL C
Many Different High Level Languages • Written to cope with the demands of specific types of problem: • Business Applications • Databases • Web Pages
High Level Language Classifications • Procedural or Imperative • Use a sequence of instructions, executed in a particular order • Object Oriented • Use objects and inheritance • Declarative • Prolog, SQL • Facts and rules based
Translating High Level Languages • Easy for people to use, but computers can not understand them • The translation from high level code to machine code is a more complex process than for assembly language • There are 2 types of Translator: • INTERPRETER • COMPILER
Programming Constructs • Most high level languages construct their statements in a similar way: • ASSIGNMENT • SELECTION • ITERATION
Assignment Age = 34 • Sets the variable Age to the value of 34 • Assignment is fundamental to programming
Types of Variables • Integer • Long Integer • Real Number • Number with decimal point • String • Alphanumeric • Boolean • Yes/No or True/False
Data Types • Current Rate of VAT • Real • Todays Date • String / Date? • Total takings at a shop • Real • If a school has a sixth form or not • Booleans • Number of Pupils in a School • Integer
Selection - If • Compares values and decides outcome If Age < 17 Then Showmessage(‘Under Age to drive’); Else Showmessage(‘Old enough to drive’); End If
Iteration • Often useful to repeat a section of code • Counting the number of letters in a word • Keeping an object moving, until it reaches a barrier Doing something until a condition is met Doing something a certain number of times Doing something while a condition is true
Iteration Do while x < 10 x = x + 1 MessageBox x Loop For x = 1 to 10 Messagebox x Next x
2 Methods of Iteration • Conditional • Unconditional
Conditional Loop • Conditional • Move an object until it reaches a barrier • x=1 • Repeat • Move forward 1 unit • Until x=2 • Infinite Loop
Unconditional Loop • Unconditional • Move an object x number of times • For Counter = 1 to 40 • Move forward 1 unit • Next Counter • Finite Loop