9 Chapter9 Programming Languages (1)
-
Upload
ultraman-power -
Category
Documents
-
view
12 -
download
3
description
Transcript of 9 Chapter9 Programming Languages (1)
©Brooks/Cole, 2003
Chapter 9
ProgrammingLanguages
©Brooks/Cole, 2003
Have a vision of computer language evolution.Have a vision of computer language evolution.
Distinguish between machine, assembly, and high-level Distinguish between machine, assembly, and high-level languages.languages.
Understand the process of creating and running a program.Understand the process of creating and running a program.
After reading this chapter, the reader should After reading this chapter, the reader should be able to:be able to:
OOBJECTIVESBJECTIVES
Distinguish between the different categories of languages:Distinguish between the different categories of languages:procedural, object-oriented, functional, declarative, and special.procedural, object-oriented, functional, declarative, and special.
Become familiar with elements of the procedural language C. Become familiar with elements of the procedural language C.
©Brooks/Cole, 2003
EVOLUTIONEVOLUTION
9.19.1
©Brooks/Cole, 2003
Figure 9-1
Evolution of computer languages
A computer language Is a set of predefined words that are combined into a program according to predefined rules
(syntax)
©Brooks/Cole, 2003
00000000 00000100 000000000000000001011110 00001100 11000010 0000000000000010
11101111 00010110 000000000000010111101111 10011110 0000000000001011
11111000 10101101 11011111 000000000001001001100010 11011111 0000000000010101
11101111 00000010 11111011 000000000001011111110100 10101101 11011111 000000000001111000000011 10100010 11011111 000000000010000111101111 00000010 11111011 000000000010010001111110 11110100 1010110111111000 10101110 11000101 000000000010101100000110 10100010 11111011 000000000011000111101111 00000010 11111011 0000000000110100
00000100 000000000011110100000100 0000000000111101
Program 9.1Program 9.1 Program in machine languageProgram in machine language1122334455667788991010111112121313141415151616
©Brooks/Cole, 2003
The only language understood The only language understood byby
a computer is machine a computer is machine language.language.
Note:Note:
•Instruction in ML must be in streams of 0s and 1s. •Internal circuit of a computer is made of switches, transistors and other electronic devices.•Two states : off (0) and on (1)
©Brooks/Cole, 2003
Symbolic Language
1950’s –Grace Hopper, a mathematician + U.S Navy•Develop concept of language that mirrored the ML using symbols, mnemonics•To represent various ML instruction•Known as symbolic language
Assembler : used to translate symbolic code into MLBecause SL had to be assembled into MLKnown as : Assembly language
©Brooks/Cole, 2003
Entry main, ^m<r2>subl2 #12,spjsb C$MAIN_ARGSmovab $CHAR_STRING_CON
pushal -8(fp)pushal (r2)calls #2,readpushal -12(fp)pushal 3(r2)calls #2,readmull3 -8(fp),-12(fp),-pushal 6(r2)calls #2,printclrl r0ret
Program 9.2Program 9.2 Program in symbolic languageProgram in symbolic language1122334455667788991010111112121313141415151616
©Brooks/Cole, 2003
High Level Language
•Required programmer to concentrate on the hardware•SL individually coded= tedious•Led to development of high-level languages
•HL- portable to diff computers•Programmer concentrate on the application rather than the intricacies of PC•HL converted to ML – compilation•Eg: BASIC, COBOL, Pascal, Ada, C, C++ and JAva
©Brooks/Cole, 2003
/* 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
Program 9.3Program 9.3 Program in C++ languageProgram in C++ language112233445566778899101011111212131314141515161617171818
©Brooks/Cole, 2003
Natural Language
•English, French, Chinese
©Brooks/Cole, 2003
BUILDINGBUILDINGAA
PROGRAMPROGRAM
9.29.2
©Brooks/Cole, 2003
Computer understand a program only if the program translated to ML
•Process•Writing and editing the program•Compiling the program•Linking the program with the required library modules
Programmer Job : Write a program, turn it into a executable file (ML)
©Brooks/Cole, 2003
Text editor: Software used to write a programSource file: file that input in the compiler
Writing & Editing Program
Compiling Program
Information : is a source file stored on disk- translated to MLCompiler : 2 separate program; preprocessor ; translator
Preprocessor: reads the source code and prepare for translator; scans for special commands known as preprocessor directives ( tell preprocessor to look special code library) -> result: translation unitTranslator : convert translation unit into ML; reads; -> result: object module ( code in ML)
©Brooks/Cole, 2003
Linker Programs
Linker : assembles all of these functions, yours and the system’s into your final executable program
©Brooks/Cole, 2003
Figure 9-2
Building a program
©Brooks/Cole, 2003
PROGRAMPROGRAMEXECUTIONEXECUTION
9.39.3
©Brooks/Cole, 2003
Figure 9-3
Program execution
Loader: locates the executable program and reads it into memory
©Brooks/Cole, 2003
CATEGORIESCATEGORIESOFOF
LANGUAGESLANGUAGES
9.49.4
©Brooks/Cole, 2003
Figure 9-4
Categories of languages
©Brooks/Cole, 2003
PROCEDURAL(IMPERATIVE) LANGUAGE
• FORTRAN- FORmula TRANslation• COBOL –COmmon Business_Oriented
Language• PASCAL • C language• Ada
©Brooks/Cole, 2003
OBJECT-ORIENTED LANGUAGES
• In OOP, the object and the operations tied together• The objects in OOP are active
– C++ language– Java
• Three principles– Encapsulation – hiding the data– Inheritance – inherit from another object– Polymorphism – define several operations with same name
©Brooks/Cole, 2003
Figure 9-5
Function in a functional language
Considered a mathematical function.Function is a black box that maps a list of inputs to a list of outputs
©Brooks/Cole, 2003
Figure 9-6
Extracting the third element of a list
©Brooks/Cole, 2003
LISP : LiSt Programming =a list-processing programming language which everything considered a list
Scheme : Version from LISP; a set of primitive functions that solves problems
©Brooks/Cole, 2003
•Use the principal of logical reasoning to answer queries. •Based on formal logic defined by Greek mathematicians; later develop into what is called firs-order predicate calculus.
Prolog : famous; By A.ColerauerMade of facts and rulesEg: human (John)
mortal (human)Q? ?-mortal (Jon)
Answer = Yes
Declarative (LOGIC) language :
©Brooks/Cole, 2003
•Mixture of two or more models and others belong to specific task
Special Language
•HTML : Hypertext Markup Language; is a pseudo language that includes marks
•PERL : Practical Extraction and Report Language ; a high level with a syntax similar to C
•SQL : Structured Query Language ; language used to answer queries about databases
©Brooks/Cole, 2003
Table 9.1 Common tagsTable 9.1 Common tags
MeaningMeaning----------------------------documentdocument headdocument bodydocument titledifferent header levelsboldfaceItalicunderlinedsubscriptsuperscriptcenteredline breakordered listunordered listan item in the listan imagean address (hyperlink)
Beginning TagBeginning Tag----------------
<HTML><HEAD><BODY><TITLE>
<Hi><B><I><U>
<SUB><SUP>
<CENTER><BR><OL><UL><LI>
<IMG><A>
Ending TagEnding Tag----------------</HTML></HEAD></BODY></TITLE>
</Hi></B></I></U>
</SUB></SUP>
</CENTER>
</OL></UL></LI>
</A>
©Brooks/Cole, 2003
<HTML><HEAD>
<TITLE> Sample Document </TITLE></HEAD><BODY>
This is the picture of a book: <IMG SRC="Pictures/book1.gif" ALIGN=MIDDLE>
</BODY></HTML>
Program 9.4Program 9.4 HTML ProgramHTML Program
©Brooks/Cole, 2003
A PROCEDURALA PROCEDURALLANGUAGE:LANGUAGE:
CC
9.59.5
©Brooks/Cole, 2003
Figure 9-7
Variables
©Brooks/Cole, 2003
Table 9.2 Arithmetic operatorsTable 9.2 Arithmetic operators
ExampleExample----------------------
3 + 5 4
Num * 5Sum / CountCount % 4
-----------------------Count ++Count
OperatorOperator----------------
DefinitionDefinition----------------
AdditionSubtraction
MultiplicationDivision (quotient)
Division (remainder)-----------------------
IncrementDecrement
©Brooks/Cole, 2003
Table 9.3 Relational operatorsTable 9.3 Relational operators
ExampleExample----------------------Num1 < 5Num1 <= 5Num2 > 3 Num2 >= 3
Num1 == Num2Num1 != Num2
OperatorOperator----------------
<
DefinitionDefinition----------------
Less thanLess than or equal to
Greater thanGreater than or equal to
Equal toNot equal to
©Brooks/Cole, 2003
Table 9.4 Logical operatorsTable 9.4 Logical operators
ExampleExample----------------------
! ( Num1 < Num2 )(Num1 < 5 ) && (Num2 > 10 )(Num1 < 5 ) || (Num2 > 10 )
OperatorOperator----------------
!&&
DefinitionDefinition----------------
NOTANDOR
©Brooks/Cole, 2003
Table 9.5 Assignment operatorsTable 9.5 Assignment operators
Meaning----------------------
Store 5 in NumNum = Num + 5Num = Num 5 Num = Num * 5Num = Num / 5Num = Num % 5
OperatorOperator----------------
==+==*=/=
%=
Example Example ---------------- Num = 5 Num += 5 Num = 5 Num *= 5 Num /= 5 Num %= 5
©Brooks/Cole, 2003
Figure 9-8
Statements
©Brooks/Cole, 2003
Figure 9-9
Side effect of a function
©Brooks/Cole, 2003
Figure 9-10 Function declaration
©Brooks/Cole, 2003
Figure 9-11
if-else statement
©Brooks/Cole, 2003
Figure 9-12 switch statement
©Brooks/Cole, 2003
Figure 9-13 while loop
©Brooks/Cole, 2003
Figure 9-14 for loop
©Brooks/Cole, 2003
Figure 9-15 do-while loop
©Brooks/Cole, 2003
Mirrored Machine language through symbols/special word.