chapter 8: high-level programming languages

11
Chapter 8: High-Level Chapter 8: High-Level Programming Languages Programming Languages Chapter 8 Chapter 8 High-Level High-Level Programming Programming Languages Languages Page Page 1 Third-generation languages (e.g., BASIC, FORTRAN, Third-generation languages (e.g., BASIC, FORTRAN, COBOL, C) were developed as a solution to the COBOL, C) were developed as a solution to the assembly language problems. assembly language problems. Third-generation languages are structured to Third-generation languages are structured to avoid considering machine operations at all, avoid considering machine operations at all, instead concentrating on relatively instead concentrating on relatively straightforward instructions on how the data is straightforward instructions on how the data is being manipulated. being manipulated. Each type of computer that Each type of computer that executes programs in a TGL executes programs in a TGL has a special program has a special program (called a (called a compiler compiler ) that ) that translates the TGL code translates the TGL code into the computer’s into the computer’s machine language. machine language. Consequently, a TGL program written on one machine Consequently, a TGL program written on one machine can be run on can be run on any any other computer, as long as the other computer, as long as the computer has a compiler for that TGL! computer has a compiler for that TGL! Example: Example: int negative (int x) int negative (int x) { if (x < 0) if (x < 0) return 1; return 1; else else return 0; return 0; }

Upload: wallace-hawkins

Post on 31-Dec-2015

50 views

Category:

Documents


1 download

DESCRIPTION

Chapter 8: High-Level Programming Languages. Third-generation languages (e.g., BASIC, FORTRAN, COBOL, C) were developed as a solution to the assembly language problems. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 8: High-Level Programming Languages

Chapter 8: High-Level Chapter 8: High-Level Programming Programming LanguagesLanguages

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 11

Third-generation languages (e.g., BASIC, FORTRAN, Third-generation languages (e.g., BASIC, FORTRAN, COBOL, C) were developed as a solution to the COBOL, C) were developed as a solution to the assembly language problems.assembly language problems.Third-generation languages are structured to avoid Third-generation languages are structured to avoid considering machine operations at all, instead considering machine operations at all, instead concentrating on relatively straightforward concentrating on relatively straightforward instructions on how the data is being manipulated.instructions on how the data is being manipulated.Each type of computer that Each type of computer that executes programs in a TGL executes programs in a TGL has a special program has a special program (called a (called a compilercompiler) that ) that translates the TGL code translates the TGL code into the computer’s into the computer’s machine language.machine language.Consequently, a TGL program written on one machine Consequently, a TGL program written on one machine can be run on can be run on anyany other computer, as long as the other computer, as long as the computer has a compiler for that TGL!computer has a compiler for that TGL!

ExampleExample::

int negative (int x)int negative (int x){{ if (x < 0)if (x < 0) return 1;return 1; elseelse return 0;return 0;}}

Page 2: Chapter 8: High-Level Programming Languages

CompilationCompilation

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 22

Lexical AnalysisLexical AnalysisThe compiler takes the TGL program (called the The compiler takes the TGL program (called the source programsource program) ) and determines which strings of characters form separate items and determines which strings of characters form separate items

(e.g., “if (count > 100)” is split into “if”, “(”, “count”, “>”, (e.g., “if (count > 100)” is split into “if”, “(”, “count”, “>”, “100”, and “)”), and all comments and white space (blanks, line “100”, and “)”), and all comments and white space (blanks, line

feeds, etc.) are deleted.feeds, etc.) are deleted.

SourceProgram(in TGL)

LexicalLexicalAnalysisAnalysis ParsingParsing CodeCode

GenerationGeneration

ParsingParsingThe compiler then analyzes the grammatical syntax of the The compiler then analyzes the grammatical syntax of the

program (e.g., “if”, “(”, “count”, “>”, “100”, “)” is determined program (e.g., “if”, “(”, “count”, “>”, “100”, “)” is determined to make sense, but a syntax error would be noticed in “if”, to make sense, but a syntax error would be noticed in “if”,

“count”, “>”, “100”, “)”.)“count”, “>”, “100”, “)”.)Code GenerationCode Generation

Once the program has been satisfactorily parsed, the compiler Once the program has been satisfactorily parsed, the compiler generates an equivalent program in machine language (called generates an equivalent program in machine language (called

the the object programobject program).).

Page 3: Chapter 8: High-Level Programming Languages

Linking and LoadingLinking and Loading

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 33

Since the individual portions of the TGL program are compiled as separate units Since the individual portions of the TGL program are compiled as separate units (e.g., your program, a math library, a graphics library, etc.), the resulting machine (e.g., your program, a math library, a graphics library, etc.), the resulting machine code cannot be executed until all of the units are connected together as a single code cannot be executed until all of the units are connected together as a single machine language program.machine language program.

SourceProgram

ObjectProgram

LinkingLinkingA TGL programmer usually relies on pre-compiled libraries of code (math functions, A TGL programmer usually relies on pre-compiled libraries of code (math functions,

graphics routines, I/O operations, etc.) that are connected to the programmer’s code graphics routines, I/O operations, etc.) that are connected to the programmer’s code prior to execution by a prior to execution by a linkerlinker program. program.

LoadingLoadingFinally, a special Finally, a special loaderloader program places the resulting machine code in main memory, program places the resulting machine code in main memory,

tying up all loose ends (e.g., setting the instruction addresses for JUMP instructions) so tying up all loose ends (e.g., setting the instruction addresses for JUMP instructions) so the code is ready for execution.the code is ready for execution.

CompiCompilele

CompiCompilele

LinkLinkLinkLink LoadLoad ExecutableProgram

ExecutableProgram

Page 4: Chapter 8: High-Level Programming Languages

Standard Source Program Standard Source Program OrganizationOrganization

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 44

Source programs in most third-generation languages generally follow Source programs in most third-generation languages generally follow a standard pattern.a standard pattern.void main()void main(){{ const int maxCount = 10;const int maxCount = 10; int count;int count; int value;int value; float sum = 0.0;float sum = 0.0;

cout << “Input values” << endl;cout << “Input values” << endl; count = 0;count = 0; while (count < maxCount)while (count < maxCount) {{ cin >> value;cin >> value; sum += value;sum += value; count++;count++; }} cout << “Mean value: ”cout << “Mean value: ” << sum/count << endl;<< sum/count << endl;}}

void main()void main(){{ const int maxCount = 10;const int maxCount = 10; int count;int count; int value;int value; float sum = 0.0;float sum = 0.0;

cout << “Input values” << endl;cout << “Input values” << endl; count = 0;count = 0; while (count < maxCount)while (count < maxCount) {{ cin >> value;cin >> value; sum += value;sum += value; count++;count++; }} cout << “Mean value: ”cout << “Mean value: ” << sum/count << endl;<< sum/count << endl;}}

void main()void main(){{ const int maxCount = 10;const int maxCount = 10; int count;int count; int value;int value; float sum = 0.0;float sum = 0.0;

cout << “Input values” << endl;cout << “Input values” << endl; count = 0;count = 0; while (count < maxCount)while (count < maxCount) {{ cin >> value;cin >> value; sum += value;sum += value; count++;count++; }} cout << “Mean value: ”cout << “Mean value: ” << sum/count << endl;<< sum/count << endl;}}

Declarative Declarative StatementsStatements

Constant and Constant and variable values variable values

representing terms representing terms that will be that will be

manipulated as the manipulated as the program is program is executed.executed.

Imperative Imperative StatementsStatements

The procedural The procedural specification of specification of the algorithm the algorithm

itself.itself.

Page 5: Chapter 8: High-Level Programming Languages

Data TypesData Types

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 55

Data types Data types are used to specify how the bit patterns used to represent are used to specify how the bit patterns used to represent data should be interpreted by the program.data should be interpreted by the program.

int count;int count;float price;float price;bool flag;bool flag;

ScalarScalar: Single-valued data types (e.g., integer, floating-: Single-valued data types (e.g., integer, floating-point, character, boolean)point, character, boolean)

StructuredStructured: Multiple-valued data : Multiple-valued data typestypesBuilt-InBuilt-In: Arrays, character : Arrays, character

stringsstrings float CaffeineOuncesPerDay[7];float CaffeineOuncesPerDay[7];char chosenCola[] = “Pepsi”;char chosenCola[] = “Pepsi”;

User-DefinedUser-Defined: Specially : Specially constructedconstructedstruct Studentstruct Student

{{ char name[30];char name[30]; int examScore[5];int examScore[5]; int quizScore[25];int quizScore[25]; int paperScore[2];int paperScore[2]; char letterGrade;char letterGrade;};};Student FALL111[25];Student FALL111[25];

Page 6: Chapter 8: High-Level Programming Languages

Imperative Statements - Part Imperative Statements - Part OneOne

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 66

Assignment statementsAssignment statements are used to assign a value to a variable. are used to assign a value to a variable.

x = 127;x = 127;

Input/output statementsInput/output statements are used to retrieve external values (input) are used to retrieve external values (input) and to file away or print information (output).and to file away or print information (output).

cout << “Enter user’s name: ”;cout << “Enter user’s name: ”;

cc countcount EE mm xx

186000186000 7878 33098753309875 55 290290

cc countcount EE mm xx

186000186000 7878 33098753309875 55 127127

cc countcount EE mm xx

186000186000 7979 33098753309875 55 127127

cc countcount EE mm xx

186000186000 7979 930000930000 55 127127

cc countcount EE mm xx

186000186000 7979 930000930000 55 127127count = count + 1;count = count + 1;E = m * c * c;E = m * c * c;

25 63 1748 50 7713 91 2389 34 56

nextDataValuenextDataValue

9494

25 63 1748 50 7713 91 2389 34 56

25nextDataValuenextDataValue

2525

nextDataValuenextDataValue

2525

dataFiledataFile positiveFilepositiveFile

cin >> username;cin >> username;dataFile >> nextDataValue;dataFile >> nextDataValue;if (nextDataValue > 0)if (nextDataValue > 0) positiveFile << nextDataValue;positiveFile << nextDataValue;

Page 7: Chapter 8: High-Level Programming Languages

Imperative Statements - Part Imperative Statements - Part TwoTwo

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 77

Conditional statementsConditional statements are used to enable alternative steps are used to enable alternative steps based on a condition.based on a condition.if (total == 0)if (total == 0) cout << “Possible Drop”;cout << “Possible Drop”;elseelse cout << “Total: ” << total;cout << “Total: ” << total;

Iterative statementsIterative statements are used to loop through a sequence of are used to loop through a sequence of instructions.instructions.

while (flag == false)while (flag == false){{ cin >> newValue;cin >> newValue; if (newValue > 0)if (newValue > 0) flag = true;flag = true;}}

switch (AreaCode)switch (AreaCode){{ case 701: cout << “ND”; break;case 701: cout << “ND”; break; case 218:case 218: case 507:case 507: case 612: cout << “MN”; break;case 612: cout << “MN”; break;}}

total = 0;total = 0;for (i = 0; i <= 24; i++)for (i = 0; i <= 24; i++){{ quizFile >> score[i];quizFile >> score[i]; total += score[i];total += score[i];}}

Page 8: Chapter 8: High-Level Programming Languages

Imperative Statements - Part Imperative Statements - Part ThreeThree

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 88

Procedures Procedures andand functions functions are used to conveniently write are used to conveniently write programs in a modular fashion.programs in a modular fashion.

void main()void main(){{ intList IQlist;intList IQlist; intList SATlist;intList SATlist; int maxIQ;int maxIQ; int bestSAT;int bestSAT;

getList(IQlist);getList(IQlist); maxIQ = maximum(IQlist);maxIQ = maximum(IQlist); getList(SATlist);getList(SATlist); bestSAT = maximum(SATlist);bestSAT = maximum(SATlist); cout << “The highest IQ is ”cout << “The highest IQ is ” << maxIQ << “ and the ”<< maxIQ << “ and the ” << “best SAT score is ”<< “best SAT score is ” << bestSAT;<< bestSAT;}}

typedef int intList[100];typedef int intList[100];

void getList(intList list)void getList(intList list){{ int count;int count; for (count = 0; count < 100; count++)for (count = 0; count < 100; count++) cin >> list[count];cin >> list[count];}}

int maximum(intList list)int maximum(intList list){{ int maxSoFar;int maxSoFar; int count;int count; maxSoFar = list[0];maxSoFar = list[0]; for (count = 1; count < 100; count++)for (count = 1; count < 100; count++) if (list[count] > maxSoFar)if (list[count] > maxSoFar) maxSoFar = list[count];maxSoFar = list[count]; return maxSoFar;return maxSoFar;}}

Page 9: Chapter 8: High-Level Programming Languages

Example: What Does This Example: What Does This Program Do?Program Do?

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 99

void main()void main(){{ intList estimate;intList estimate; int bestGuesser;int bestGuesser; int price;int price;

cin >> price;cin >> price; getList(estimate);getList(estimate); bestGuesser = drew(estimate, price);bestGuesser = drew(estimate, price); if (bestGuesser == -1)if (bestGuesser == -1) cout << “NO WINNER”;cout << “NO WINNER”; elseelse {{ cout << bestGuesser << “ WINS! ”;cout << bestGuesser << “ WINS! ”; if (estimate[bestGuesser] == price)if (estimate[bestGuesser] == price) cout << “WITH A BONUS!!!”;cout << “WITH A BONUS!!!”; }}}}

typedef int intList[4];typedef int intList[4];

void getList(intList list)void getList(intList list){{ int count;int count; for (count = 0; count < 4; count++)for (count = 0; count < 4; count++) cin >> list[count];cin >> list[count];}}

int drew(intList list, int item)int drew(intList list, int item){{ int bestSoFar, bestIndex, index;int bestSoFar, bestIndex, index; bestIndex = -1;bestIndex = -1; bestSoFar = 0;bestSoFar = 0; for (index = 0; index < 4; index++)for (index = 0; index < 4; index++) if ((list[index] > bestSoFar) &&if ((list[index] > bestSoFar) && (list[index] <= item))(list[index] <= item)) {{ bestIndex = index;bestIndex = index; bestSoFar = list[index];bestSoFar = list[index]; }} return bestIndex;return bestIndex;}}

What would be the output of this What would be the output of this program for the following input file?program for the following input file?

600 400 675 525 450600 400 675 525 450

Page 10: Chapter 8: High-Level Programming Languages

Object-Oriented ProgrammingObject-Oriented Programming

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 1010

Early third-generation programming Early third-generation programming languages used a “procedure-oriented” languages used a “procedure-oriented” approach, in which the approach, in which the wayway something was something was done was the center of attention for the done was the center of attention for the programmer.programmer.More recently, with the advent of graphical More recently, with the advent of graphical user interfaces and massive databases, the user interfaces and massive databases, the focus has shifted to an “object-oriented” focus has shifted to an “object-oriented” approach, emphasizing approach, emphasizing whatwhat is being is being manipulated instead of manipulated instead of howhow..

Page 11: Chapter 8: High-Level Programming Languages

The Three Principles of OOPThe Three Principles of OOP

Chapter 8Chapter 8High-Level High-Level

ProgramminProgramming Languagesg Languages

Page Page 1111