Chapter 9 Programming Languages ©Brooks/Cole, 2003 OBJECTIVES After reading this chapter, the reader should be able to: Have a vision of computer language evolution. Distinguish between machine, assembly, and highlevel languages. Understand the process of creating and running a program. Distinguish between the different categories of languages: procedural, object-oriented, functional, declarative, and special. Become familiar with elements of the procedural language C. 9.1 EVOLUTION ©Brooks/Cole, 2003 Figure 9-1 Evolution of computer languages ©Brooks/Cole, 2003 Program 9.1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Program in machine language 00000000 00000100 0000000000000000 01011110 00001100110000100000000000000010 11101111 000101100000000000000101 11101111 10011110 0000000000001011 11111000 10101101 11011111 0000000000010010 0110001011011111 0000000000010101 11101111 00000010 11111011 0000000000010111 11110100 1010110111011111 0000000000011110 0000001110100010 11011111 0000000000100001 11101111 00000010 11111011 0000000000100100 01111110 11110100 10101101 11111000 10101110 110001010000000000101011 0000011010100010 11111011 0000000000110001 11101111 00000010 11111011 0000000000110100 00000100 0000000000111101 00000100 0000000000111101 ©Brooks/Cole, 2003 Note: The only language understood by a computer is machine language. ©Brooks/Cole, 2003 Program 9.2 Program in symbolic language 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Entry subl2 jsb movab main, ^m<r2> #12,sp C$MAIN_ARGS $CHAR_STRING_CON pushal pushal calls pushal pushal calls mull3 pushal calls clrl ret -8(fp) (r2) #2,read -12(fp) 3(r2) #2,read -8(fp),-12(fp),6(r2) #2,print r0 ©Brooks/Cole, 2003 Symbolic languages ► Assembly language is a symbolic language. ► An assembler is used to translate symbolic code into machine language. ©Brooks/Cole, 2003 Program 9.3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /* Program in C++ language This program reads two integer numbers from the keyboard and prints their product. */ #include <iostream.h> int main (void) { // Local Declarations int number1; int number2; int result; // Statements cin >> number1; cin >> number2; result = number1 * number2; cout << result; return 0; } // main ©Brooks/Cole, 2003 9.2 BUILDING A PROGRAM ©Brooks/Cole, 2003 Figure 9-2 Building a program Building a program ► Three steps to building a program: Writing an editing the program ►Using text editor to edit the source file Compiling the program ►Two programs: the preprocessor and the translator Linking the program with the required library modules ►The linker assembles all of the subprograms into the final executable program ©Brooks/Cole, 2003 9.3 PROGRAM EXECUTION ©Brooks/Cole, 2003 Figure 9-3 Program execution disk computer monitor 9.4 CATEGORIES OF LANGUAGES ©Brooks/Cole, 2003 Figure 9-4 Categories of languages ©Brooks/Cole, 2003 Procedural language ►A procedural (imperative 命令) language is a set of instructions that are executed one by one from beginning to end unless an instruction forces the control elsewhere. FORTRAN COBOL Pascal C Ada ©Brooks/Cole, 2003 FORTRAN ► FORTRAN: FORmula TRANslation Designed by a group of IBM engineers (1957) The first high-level language Features: ►High-precision arithmetic (高精確度的計算) ►Capability of handling complex numbers (複數) ►Exponentiation computation (ab) (指數) ©Brooks/Cole, 2003 COBOL ► COBOL: COmmon Business-Oriented language Designed by Grace Hopper group A business programming language Features: ►Fast access to files and databases ►Fast updating of files and databases ►Large amounts of generated reports ►User-friendly formatted output ©Brooks/Cole, 2003 Pascal ► Pascal Invented by Niklaus Wirth (1971) Pascal was designed --►To teach programming to novices by emphasizing the structured programming approach Pascal became the most popular language in academic (學術界) ©Brooks/Cole, 2003 C ►C Developed by Dennis Ritchie at Bell Laboratories (1970s) Most of the UNIX operating system is written in C Features: ►C has all the high-level instructions ►C has some low-level instructions ►C is a very efficient language; its instructions are short ►C has been standardized by ANSI (專門認定語言標準 的機構) and ISO ©Brooks/Cole, 2003 Ada ► Ada: Created for the U.S. Department of Defense (DoD 美國國防部) Features: ►Ada has high-level instructions. ►Ada has instructions to allow real-time processing. This make it suitable for process control. ►Ada has parallel-processing capabilities. It can run on mainframe computers with multiple processors. ©Brooks/Cole, 2003 Object-Oriented languages ► In object-oriented programming, the objects and the operations to be applied to them are tied together. ► The objects in object-oriented programming are active. C++ Java ©Brooks/Cole, 2003 C++ ► C++: developed by Bjarne Stroustrup at Bell Lab ► Three principles: Encapsulation (封裝) ►The user knows what to do with the data without knowing how it is done. Inheritance (繼承) ►When a general class is defined, you can define a more specific class that inherits some of the characteristics of the general class but also has some new characteristics. Polymorphism (多型) ►You can define several operations with the same name that can do different things in related classes. Java ► Java: Developed at Sun Microsystems,, Inc. The language is totally class oriented. Every data item belongs to a class. The browser can download the applet (小小的應 用程式) and run it locally. Features: ►Class library ►Multithreading ©Brooks/Cole, 2003 Figure 9-5 Functional languages ►A functional language: Predefines a set of primitive functions Allow the programmer to combine primitive functions to create new function Function in a functional language ©Brooks/Cole, 2003 Figure 9-6 Extracting the third element of a list ©Brooks/Cole, 2003 Functional languages ► Advantages: Encourage modular programming Allow the programmer to make new functions out of existing ones ► Examples: LISP: designed at MIT Scheme: LISP similar ©Brooks/Cole, 2003 Scheme ► For example: car : extract the first element of a list cdr : extracts the rest of the elements in a list except the first one (car 2 3 7 8 11 17 20) 2 (cdr 2 3 7 8 11 17 20) 3 7 8 11 17 20 (car (cdr (cdr 2 3 7 8 11 17 20))) 7 ©Brooks/Cole, 2003 Declarative language ►A declarative language uses the principle of logical reasoning to answer queries. ► Logical reasoning is based on deduction (歸納). ► For example: If (A is B) and (B is C), then (A is C) ► Problem: Collecting all the facts into one program make the program huge. ©Brooks/Cole, 2003 Prolog ► Prolog: Developed by A. Colmerauer in France in 1972. A program in Prolog is made of facts and rules. Example: human ( John ) mortal ( human ) (註:mortal難免一死的) ?-mortal ( John ) (註:user ask) yes (註:program answer) ©Brooks/Cole, 2003 Special languages ► HTML (Hypertext Markup Language) An HTML file is stored on the server and can be downloaded by a browser. The instructions are stored with the text. Any browser can read the instructions and format the text according to the workstation being used. HTML lets you use only ASCII characters for both the main text and formatting instructions. Head, Body, and Tags ©Brooks/Cole, 2003 Table 9.1 Common tags Beginning Tag ---------------<HTML> <HEAD> <BODY> <TITLE> <Hi> <B> <I> <U> <SUB> <SUP> <CENTER> <BR> <OL> <UL> <LI> <IMG> <A> Ending Tag ---------------</HTML> </HEAD> </BODY> </TITLE> </Hi> </B> </I> </U> </SUB> </SUP> </CENTER> </OL> </UL> </LI> </A> Meaning ---------------------------document document head document body document title different header levels boldface Italic underlined subscript superscript centered line break ordered list unordered list an item in the list an image an address (hyperlink) Program 9.4 HTML Program <HTML> <HEAD> <TITLE> Sample Document </TITLE> </HEAD> <BODY> This is the picture of a book: <IMG SRC="Pictures/book1.gif" ALIGN=MIDDLE> </BODY> </HTML> ©Brooks/Cole, 2003 Special languages ► PERL (Practical Extraction and Report Language) A high-level language with a syntax similar to the C but more efficient The power of PERL lies in its well-designed use of regular expressions. ► SQL (Structured query Language) A language used to answer queries about databases. See Chapter 14. ©Brooks/Cole, 2003 9.5 A PROCEDURAL LANGUAGE: C ©Brooks/Cole, 2003 C — Identifiers ► Identifiers (識別字) Identifiers allow you to name data and other objects in the program Different programming languages use different rules for form identifiers The first character of the identifier cannot be a digit. Reserved words (key words): about 50 words. ©Brooks/Cole, 2003 C — Data types ► Data types A data type defines a set of values and a set of operations that can be applied to those values. Standard types: ► integer, char, float Derived (衍生的) types: ►pointer, enumerated type, union, array, and structure. ©Brooks/Cole, 2003 Figure 9-7 C — Variables ► Variables are names for memory location. ©Brooks/Cole, 2003 Variables ► Declaration (宣告) A declaration is used to name a variable. ► Definitions Definitions are used to create the variable and to allocate a memory location for it. Example: float price; ► Initialization The initializer establishes the first value that the variable will contain. Example: float price = 23.45; ©Brooks/Cole, 2003 C — Constants ► Constants are data values that cannot be changed during the execution of a program. ► Three types: Literal constant circumference = 2 * length * width ; Named constant const pi = 3.14; Symbolic constant #define taxRate 0.0825 ©Brooks/Cole, 2003 C — Input and output ► Input Example: scanf (“%d”, &num); ► Output Example: printf (“The value of the number is: %d” , num); ©Brooks/Cole, 2003 C — Expressions ► An expression is a sequence of operands and operators that reduces to a single value. Example: 2 * 5 ► Operators An operator is a language-specific syntactical token that requires an action to be taken. ► Operand A operand receives an operator’s action. The operands of multiply are the multiplier and the multiplicand. ©Brooks/Cole, 2003 Arithmetic operators Operator ---------------+ * / % ---------++ -- Definition ---------------Addition Subtraction Multiplication Division (quotient) Division (remainder) ----------------------Increment Decrement Example ---------------------3 + 5 2 - 4 Num * 5 Sum / Count Count % 4 ----------------------Count ++ Count -- Table 9.2 Arithmetic operators ©Brooks/Cole, 2003 Relational operators Operator ---------------< <= > >= == != Definition ---------------Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to Example ---------------------Num1 < 5 Num1 <= 5 Num2 > 3 Num2 >= 3 Num1 == Num2 Num1 != Num2 Table 9.3 Relational operators ©Brooks/Cole, 2003 Logical operators Operator ---------------! && || Definition ---------------NOT AND OR Example ---------------------! ( Num1 < Num2 ) (Num1 < 5 ) && (Num2 > 10 ) (Num1 < 5 ) || (Num2 > 10 ) Table 9.4 Logical operators ©Brooks/Cole, 2003 Assignment operators Operator ---------------== += -= *= /= %= Example ---------------Num =5 Num += 5 Num -= 5 Num *= 5 Num /= 5 Num %= 5 Meaning ---------------------Store 5 in Num Num = Num + 5 Num = Num - 5 Num = Num * 5 Num = Num / 5 Num = Num % 5 Table 9.5 Assignment operators ©Brooks/Cole, 2003 Figure 9-8 Statements ©Brooks/Cole, 2003 C — Statements ► Expression statements a++; b = 4; c = b + c * 4; ► Compound statements { x = 1; y = 20 ; } ©Brooks/Cole, 2003 C — Functions ► In C, a subroutine is called a function. ► A function’s side effect is an action that results in a change in the state of the program. The side effect can involve - Accepting data from outside the program Sending data out of the program to the monitor or a file Changing the value of a variable in the calling function ©Brooks/Cole, 2003 Figure 9-9 Side effect of a function ©Brooks/Cole, 2003 Figure 9-10 Function declaration Parameter passing ► Pass by value When you pass by value, a copy of the data is created and placed in a local variable in the called function. The original data in the calling function are safe and unchanged. ► Pass by reference Pass by reference send the address of a variable to the called function. When you want to change the contents in a variable in the calling function, you must pass by reference. Figure 9-11 Selection if-else statement ©Brooks/Cole, 2003 Figure 9-12 switch statement ©Brooks/Cole, 2003 Figure 9-13 Repetition while loop: a pretest loop Figure 9-14 for loop: a pretest loop Figure 9-15 do-while loop: a posttest loop Key terms Ada Arithmetic operator ► Assembler ► Assembly language ► Assignment operator ► C ► C++ ► Class ► COBOL ► Compiler ► Compound statement ► Computer language ► Constant ► ► Data type Declaration ► Declarative language ► Definition ► Derived types ► Executable file ► Expression ► Expression statement ► FORTAN ► Function ► Functional language ► High-level language ► HTML ► ► ©Brooks/Cole, 2003 Identifier ► Imperative language ► Inheritance ► Java ► Keyword ► Linker ► LISP ► Loader ► Logical operator ► Machine language ► Natural language ► Object-oriented language ► Operand ► Operation ► Operator ► Pascal ► Pass by reference ► Pass by value ► PERL ► Processor ► Procedural language ► Programming language ► Prolog ► Relational operator ► Reserved word ► Scheme ► Side effect ► Source file ► SQL ► Symbolic language ► Text editor ► variable ► ©Brooks/Cole, 2003