9 Chapter9 Programming Languages (1)

44
©Brooks/Cole, 2003 Chapter 9 Programming Languages

description

9 Chapter9 Programming Languages (1)

Transcript of 9 Chapter9 Programming Languages (1)

Page 1: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Chapter 9

ProgrammingLanguages

Page 2: 9 Chapter9 Programming Languages (1)

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

Page 3: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

EVOLUTIONEVOLUTION

9.19.1

Page 4: 9 Chapter9 Programming Languages (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)

Page 5: 9 Chapter9 Programming Languages (1)

©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

Page 6: 9 Chapter9 Programming Languages (1)

©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)

Page 7: 9 Chapter9 Programming Languages (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

Page 8: 9 Chapter9 Programming Languages (1)

©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

Page 9: 9 Chapter9 Programming Languages (1)

©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

Page 10: 9 Chapter9 Programming Languages (1)

©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

Page 11: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Natural Language

•English, French, Chinese

Page 12: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

BUILDINGBUILDINGAA

PROGRAMPROGRAM

9.29.2

Page 13: 9 Chapter9 Programming Languages (1)

©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)

Page 14: 9 Chapter9 Programming Languages (1)

©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)

Page 15: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Linker Programs

Linker : assembles all of these functions, yours and the system’s into your final executable program

Page 16: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-2

Building a program

Page 17: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

PROGRAMPROGRAMEXECUTIONEXECUTION

9.39.3

Page 18: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-3

Program execution

Loader: locates the executable program and reads it into memory

Page 19: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

CATEGORIESCATEGORIESOFOF

LANGUAGESLANGUAGES

9.49.4

Page 20: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-4

Categories of languages

Page 21: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

PROCEDURAL(IMPERATIVE) LANGUAGE

• FORTRAN- FORmula TRANslation• COBOL –COmmon Business_Oriented

Language• PASCAL • C language• Ada

Page 22: 9 Chapter9 Programming Languages (1)

©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

Page 23: 9 Chapter9 Programming Languages (1)

©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

Page 24: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-6

Extracting the third element of a list

Page 25: 9 Chapter9 Programming Languages (1)

©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

Page 26: 9 Chapter9 Programming Languages (1)

©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 :

Page 27: 9 Chapter9 Programming Languages (1)

©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

Page 28: 9 Chapter9 Programming Languages (1)

©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>

Page 29: 9 Chapter9 Programming Languages (1)

©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

Page 30: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

A PROCEDURALA PROCEDURALLANGUAGE:LANGUAGE:

CC

9.59.5

Page 31: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-7

Variables

Page 32: 9 Chapter9 Programming Languages (1)

©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

Page 33: 9 Chapter9 Programming Languages (1)

©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

Page 34: 9 Chapter9 Programming Languages (1)

©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

Page 35: 9 Chapter9 Programming Languages (1)

©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

Page 36: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-8

Statements

Page 37: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-9

Side effect of a function

Page 38: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-10 Function declaration

Page 39: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-11

if-else statement

Page 40: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-12 switch statement

Page 41: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-13 while loop

Page 42: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-14 for loop

Page 43: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Figure 9-15 do-while loop

Page 44: 9 Chapter9 Programming Languages (1)

©Brooks/Cole, 2003

Mirrored Machine language through symbols/special word.