Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch. 7 Programming Languages

Similar presentations


Presentation on theme: "Ch. 7 Programming Languages"— Presentation transcript:

1 Ch. 7 Programming Languages
Historical perspective.(历史回顾) Traditional programming concepts. Program units.(程序单元) Language implementation.(语言实现) 面向对象程序设计

2 Programming Language Programming language similar to our pseudocode have been developed that allow algorithms to be expressed in a form that both palatable(可口的,美味的)to humans and easily convertible into machine language instructions.

3 7.1 Historical Perspective
Machine language - binary form directly controls the hardware. Assembly language - mnemonic form of the machine language. High-level programming language - English like language. Evolution?

4 Early Generation Operation: adding the contents of memory cells 6D and 6C and placing the result in 6E. 156C 166D 5056 306E C000 LD R5 Price LD R6 ShippingCharge ADDI R0 R5 R6 ST R0 TotalCost HLT

5 Programming language used mnemonic is called Assembly language .
The program which translates the program written in assembly language into machine language is called Assembler

6 Interpreter Compiler Primitive
GrossWeight  (VehicleWeight + LoadWeight) is a high-level language statement Interpreter Compiler

7 Historical Perspective
1st-generation - machine language. 2nd-generation - assembly language. 3rd-generation - machine independent. 4th-generation - software packages that allow users to customize computer software to their applications without needing technical expertise. 5th-generation - declarative (logic) programming.(说明性程序设计)

8 Figure 7.1: The evolution of programming paradigms

9 7.2 Traditional Programming Concept
Statements in programming languages tend to fall into three categories: declarative statements, imperative (命令的)statements, and comments. Declarative statements - define customized terminology used in the program. Imperative statements - describe steps in the underlying algorithm. Comments.

10 Traditional Programming Concept
Variables, constants, and literals(字义的). Data type - integer, read, Boolean, char….. Data structure - array, queue, list,…….. Assignment statements Control statements. Comments - internal documentation.

11 Variables and Data Type
High level Programming Language allow locations to be referenced by descriptive names rather than by mnemonic addresses ,Such a name is called Variable. Variable should be declared with its data type in declarative statement prior to being used in program.

12 Figure 7.2: The composition of a typical imperative program or program unit

13 Figure 7.3: The same variable declarations in different languages

14 Data Structure In addition to data type, Variables in program are often associated with Data structure---- the conceptual shape or arrangement of data. Homogeneous array Heterogeneous array

15 Figure 7.4: A two-dimensional array with two rows and nine columns

16 Figure 7.5: Declaration of heterogeneous arrays in Pascal and C

17 Figure 7. 5: Declaration of heterogeneous arrays in
Figure 7.5: Declaration of heterogeneous arrays in Pascal and C (continued)

18 Constants and Literals
Sometimes ,a fixed Predetermined value is used in a program. EffectiveAltAltimeter+ 645 Const AirportAlt = 645; EffectiveAltAltimeter+ AirportAlt

19 Assignment Statement Z = 3.5*X + Y/2; Assigning a value to a variable
( Storing the Value in the memory location identified by the variable name) Z = 3.5*X + Y/2; Z= X+ Y; Z:= X+Y X= 5; Z= 2; Z= X;

20 Control Statement goto 40
The control statement is an imperative statement that alters the execution sequence of the program. The simplest control statement is goto ( label) goto 40

21 Unconditional jump statement

22 Figure 7.6: Control structures and their representations in C, C++, C#, and Java (continued)

23 Figure 7. 6: Control structures and their
Figure 7.6: Control structures and their representations in C, C++, C#, and Java

24 Figure 7.7: The for loop structure and its representation in Pascal, C++, C#, and Java (continued)

25 Figure 7.7: The for loop structure and its representation in Pascal, C++, C#, and Java

26 Procedure Units Breaking large programs into manageable units, units = modules, functions, objects. Procedures and functions. Parameter passing - formal parameters and actual parameter, call by address and call by value. I/O statements.

27 Procedure A procedure ,in its general sense , is a set of instructions for performing a task that can be used as an abstract tool by other program units. Control is transferred to the procedure at the time its services are required and then returned to the original program unit after the procedure finished . The program unit that requires the execution of a procedure is called Calling unit

28 Figure 7.8: The flow of control involving a procedure

29 Parameters Procedures are often written using generic term that are made specific when the procedure is applied. EX. Procedure Sort (List) Such generic terns within a procedure are called parameters. The terms are used in writing procedure are called Formal parameters ,and the precise meanings assigned to these formal parameters when the procedure is applied are called actual parameters

30 Figure 7.9: The procedure Project Population written in the programming language C

31 Figure 7.10: Executing the procedure Demo and passing parameters by value (continued)

32 Figure 7.10: Executing the procedure Demo and passing parameters by value (continued)

33 Figure 7.11: Executing the procedure Demo and passing parameters by value

34 Figure 7.12: Executing the procedure Demo and passing parameters by reference (continued)

35 Figure 7.12: Executing the procedure Demo and passing parameters by reference (continued)

36 Figure 7.13: Executing the procedureDemo and passing parameters by reference

37 Figure 7.14: The function CylinderVolume written in the programming language C

38 Input/output statements
Input/output statements are for inputting/outputting values to program from the keyboard,disk files and other devices. EX. Readln ( value) writeln (value) printf( “the ages are %d \n”,age1); cin >> value

39 Figure 7.15: An example of formatted output

40 7.4 Language Implementation
Translation - converting a program from one language to another. Translation involves three activities: Lexical analysis, (词汇分析) Parsing, and (语法分析) Code generation. Lexical analysis - recognizing which strings of symbols from the source program represent a single entity.

41 Figure 7.16: The translation process

42 Language Implementation
Parsing - identifying the grammatical structure of the program and recognizing the role of each component. Fixed-format languages Vs. free-format languages. Key words, reserved words, syntax diagram, parse tree. Coercion and strongly typed.

43 Language Implementation
Code generation - constructing the machine language instructions to simulate the statements recognized by the parser(语法分析器). Code optimization. Linker - links all necessary object programs to produce a complete, executable program. Loader - place the program in memory for execution (what about multitasking?)

44 Figure 7.16: An object-oriented approach to the translation process

45 Linking and Loading The object program produced by the translation process ,although expressed in machine language, is rarely in a form that can be executed directly by the machine. Because most programming environment allows the modules of the program to be translated as individual units at different time. Thus ,the object program contains only several pieces of a complete program. The program that connects them together is called Linker

46 Figure 7.17: The complete program preparation process

47 7.6 Object-Oriented Programming
Classes And Objects The task of developing a computer game in which the player must protect the Earth from falling meteors by shooting them with high-power Lasers, Each laser contains a finite internal power source that partially consumed each time the laser is fired. Once the power is depleted the laser becomes useless.

48 In object-oriented programming paradigm we view the laser as a whole unit, containing its remaining power as well as the procedure that modifies its aim and firing its beam. This unit is called an object. Since the each laser has the same properties, the object can be built from the same template which is called Class

49 Figure 7.18: The structure of a class describing a laser weapon in a computer game

50 The difference between Class and Object
A class is a template from which the object are constructed. One class can be used to create numerous objects. The object is often called an instance of the class from which it was constructed. In our game, Laser1,…laser n are objects or instances of the class laserClass

51 Graphic representation of objects

52 The Class and Objects

53 HomeWork P , 25


Download ppt "Ch. 7 Programming Languages"

Similar presentations


Ads by Google