1 / 52

Chapter 9

Chapter 9. Programming Languages. O BJECTIVES. Distinguish between machine, assembly, and high-level languages. Understand the process of creating and running a program.

Download Presentation

Chapter 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9 ProgrammingLanguages

  2. OBJECTIVES Distinguish between machine, assembly, and high-level 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. Have a vision of computer language evolution. After reading this chapter, the reader should be able to:

  3. 9.1 EVOLUTION

  4. Computer language –a set of predefined words that are combined into a program according to predefined rules (syntax). Evolution of computer languages

  5. Each computer has its ownmachine language, which is made of streams of 0s and 1s. Note: The only language understood by a computer is machine language.

  6. Program 9.1 Program in machine language 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 00000000 00000100 0000000000000000 01011110 00001100 11000010 0000000000000010 11101111 00010110 0000000000000101 11101111 10011110 0000000000001011 11111000 10101101 11011111 0000000000010010 01100010 11011111 0000000000010101 11101111 00000010 11111011 0000000000010111 11110100 10101101 11011111 0000000000011110 00000011 10100010 11011111 0000000000100001 11101111 00000010 11111011 0000000000100100 01111110 11110100 10101101 11111000 10101110 11000101 0000000000101011 00000110 10100010 11111011 0000000000110001 11101111 00000010 11111011 0000000000110100 00000100 0000000000111101 00000100 0000000000111101

  7. Symbolic language –simply mirrored the machine languages using symbols to represent the various machine language instructions. • Assembler –a special program to translate symbolic code into machine language. • Assembly language

  8. Program 9.2 Program in symbolic language 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Entry main, ^m<r2> subl2 #12,sp jsb C$MAIN_ARGS movab $CHAR_STRING_CON pushal -8(fp) pushal (r2) calls #2,read pushal -12(fp) pushal 3(r2) calls #2,read mull3 -8(fp),-12(fp),- pushal 6(r2) calls #2,print clrl r0 ret

  9. High-Level language – • Portable to many different computers • Allow programmer to concentrate on application • Must be converted to machine languages(Compilation) • Natural language –English, French, or Chinese

  10. Program 9.3 Program in C++ language 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /* 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

  11. 9.2 BUILDING A PROGRAM

  12. Building a program • Writing and Editing • Compiling • Linking • Running • Load • Execute

  13. Building a program Source file Object file Assemble all subprograms into final executable program Executable file

  14. 9.3 PROGRAM EXECUTION

  15. Program execution • Loader – An OS program to load the program into memory for executing

  16. 9.4 CATEGORIES OF LANGUAGES

  17. Categories of languages

  18. Procedural language –a set of instructions that are executed one by one from beginning to end unless an instruction forces the control elsewhere. • When programmers need to solve a problem, they should know the procedure to follow. • For each problem, the programmer should carefully design an algorithm, and the algorithm should be carefully translated to instructions. • Imperative language –each instruction is a command to the computer to do some specific task.

  19. Fortran – • For scientific and engineering application • The first high-level language • COBOL – • a business programming language • Pascal – • Structured programming for teaching

  20. C – • High-level instructions (Structured programming for writing UNIX) • Low-level instructions to access the hardware directly and quickly.Closer to assembly language than any other language.A good language for system programmers. • Efficient language; its instructions are short. • Standardized by ANSI and ISO

  21. Procedural language – • Objects – independent from Operations • Objects – passive • Object-Oriented language – • An approach to problem solving that is totally different from procedural language. • Objects – tied together with Operations • Objects – active

  22. C++ - • Developed by Bell Lab. • adds object-oriented features to its predecessor, C. • Java – • Developed by SUN, Based on C and C++ • Java source code files are compiled into a format called bytecode , which can then be executed by a Java interpreter. • Compiled Java code can run on most computers because Java interpreters and runtime environments, known as Java Virtual Machines (VMs), exist for most operating systems. • Programs • Application – a complete stand-alone program that can be run independently. • Applet – embedded in HTML language, stored on a server, and run by a client browser.

  23. In Functional language, a program is considered a mathematical function. • A function is a black boxthat maps a list of inputs to a list of outputs. • LISP – designed by MIT Function in a functional language

  24. Extracting the third element of a list • Rest – extracts all the elements except the first. • First - extracts all the first elements.

  25. HTML (Hypertext Markup Language) • A seudolanguage that includes marks that serve as formatting hints and links to other files. • An HTML file is made of text and tags. • An HTML file (page) is stored on the server and can be download by a browser. • Browser – removes the tags and interprets them as either formatting hints or links to other files.

  26. 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 levelsboldface Italic underlined subscript superscript centered line break ordered list unordered list an item in the list an image an address (hyperlink)

  27. 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>

  28. Perl (Practical Extraction and Report Language) • a programming language especially designed for processing text. • one of the most popular languages for writing CGI scripts. • CGI (Common Gateway Interface) programs are the most common way for Web servers to interact dynamically with users. Many HTML pages that contain forms, for example, use a CGI program to process the form's data once it's submitted. • SQL (Structured Query Language) – a language used to answer queries about database.

  29. 9.5 A PROCEDURAL LANGUAGE: C

  30. Variables • Variables – names for memory locations. • Each memory location has an address. • Used by the computer internally • Inconvenient for the programmer

  31. Constants • constants – data values that cannot be changed during the execution of a program. • Literal constant – a = 2 * p * r ; • Named constant – constant pi = 3.14; • Symbolic constant - #define taxRate 0.0825

  32. Table 9.2 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 --

  33. Table 9.3 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

  34. Table 9.4 Logical operators Operator ---------------- ! && || Definition ---------------- NOT AND OR Example---------------------- ! ( Num1 < Num2 ) (Num1 < 5 ) && (Num2 > 10 ) (Num1 < 5 ) || (Num2 > 10 )

  35. Table 9.5 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

  36. Statement • cause an action to be performed by the program. • translates directly into one or more executable instructions. label: goto label;

  37. Statement • Expression statement – a statement by placing a semicolon(;) after it. a++; b = 4;c = b + c * 4; • Compound statement – a unit of code consisting of zero or more statements.{ x = 1; y = 20;}

  38. Functions • In C, a subroutine is called a function. • A C program is made of one or morefunctions, one and only one of which must be called main. • The execution of the program always starts and ends with main, but this function can call other functions. • The function main is called by the operating system; main in turn calls other functions.When main is complete, control returns to the operating system.

  39. Side effect of a function • Side effect • Is an action that results in a change in the state of the program. • Occurs while the function is executing and before the function returns. • In general, the purpose of a function isAt the same time, a function can have a side effect.

  40. Function declaration • Parameter Passing • Pass by value • Pass by reference Actual parameters Return type Formal parameters Function header Function body

  41. Parameter Passing • Pass by value – a copy of data is created and placed in a local variable in the called function. • Pass by reference – sends the address of a variable to the called function rather than sending its value.

  42. Call by Value main() { int a=1,b=2; swap(a, b); } void swap(int x, int y) { int temp; temp=x; x=y; y=temp; } a b 1 2 1 2 y x temp

  43. Call by Address (Reference) main() { int a=1,b=2; swap(&a, &b); } void swap(int *x, int *y) { int temp; temp=*x; *x=*y; *y=temp; } a b 1000 2000 1 2 *y *x 1000 2000 y x temp

  44. if-else statement

  45. switch statement

  46. while loop

  47. for loop False

  48. do-while loop

  49. Recursion • C language supports recursion. • A function in C can call itself. • int fac(int n){ if (n == 0) return(1); else return( n * fact(n-1) );}

  50. Recursion • C language supports recursion. • A function in C can call itself. • int fac(int n){ if (n == 0) return(1); else return( n * fac(n-1) );}

More Related