Chapter08 - Chapter 8: High-Level Programming Languages...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 8: High-Level Programming Languages Languages Third-generation languages (e.g., BASIC, FORTRAN, COBOL, Third-generation C) were developed as a solution to the assembly language problems. problems. Third-generation languages are structured to avoid Third-generation considering machine operations at all, instead concentrating on relatively straightforward instructions on how the data is being manipulated. being Each type of computer that Each Example: int negative (int { executes programs in a TGL has if (x < 0) if a special program (called a return 1; return compiler) that translates the TGL else compiler that else return 0; return code into the computer’s } machine language. machine Consequently, a TGL program written on one machine can be run Consequently, on any other computer, as long as the computer has a compiler for any that TGL! that x) Chapter 8 High-Level High-Level Programming Languages Languages Page 1 Page Compilation Compilation Source Program (in TGL) Lexical Analysis Parsing Code Generation Lexical Analysis The compiler takes the TGL program (called the source program) and The source and determines which strings of characters form separate items (e.g., “if (count > 100)” is split into “if”, “(”, “count”, “>”, “100”, and “)”), and all comments and white space (blanks, line feeds, etc.) are deleted. white Parsing The compiler then analyzes the grammatical syntax of the program (e.g., “if”, The “(”, “count”, “>”, “100”, “)” is determined to make sense, but a syntax error would be noticed in “if”, “count”, “>”, “100”, “)”.) would Code Generation Once the program has been satisfactorily parsed, the compiler generates an Once equivalent program in machine language (called the object program). object Chapter 8 High-Level High-Level Programming Languages Languages Page 2 Page Linking and Loading Linking Since the individual portions of the TGL program are compiled as Since separate units (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 machine language program. connected Source Program Compil e Object Program Liink Ln Load Executable Program Linking A TGL programmer usually relies on pre-compiled libraries of code (math TGL functions, graphics routines, I/O operations, etc.) that are connected to the programmer’s code prior to execution by a linker program. linker Loading Finally, a special loader program places the resulting machine code in main Finally, loader memory, tying up all loose ends (e.g., setting the instruction addresses for JUMP instructions) so the code is ready for execution. JUMP Chapter 8 High-Level High-Level Programming Languages Languages Page 3 Page Standard Source Program Source programs in most third-generation languages Source Organization Ostandard pattern. rganization generally follow a generally void main() { const int maxCount = 10; const int count; int int value; int float sum = 0.0; float cout << “Input values” << endl; cout count = 0; count while (count < maxCount) while { cin >> value; cin sum += value; sum count++; count++; } cout << “Mean value: ” cout << sum/count << endl; << } Declarative Statements Constant and variable Constant values representing terms that will be manipulated as the program is executed. program Imperative Imperative Statements Statements The procedural The specification of the algorithm itself. algorithm Chapter 8 High-Level High-Level Programming Languages Languages Page 4 Page Data Types Data Data types are used to specify how the bit patterns used to Data represent data should be interpreted by the program. represent Scalar: Single-valued data types (e.g., integer, floating-point, character, Single-valued int count; boolean) boolean) float price; bool flag; Structured: Multiple-valued data types Built-In: Arrays, character strings float CaffeineOuncesPerDay[7]; char chosenCola = “Pepsi”; User-Defined: Specially constructed struct Student { char name[30]; char int examScore[5]; int int quizScore[25]; int int paperScore[2]; int char letterGrade; char }; Student FALL111[25]; Chapter 8 High-Level High-Level Programming Languages Languages Page 5 Page Imperative Statements - Part One Imperative Assignment statements are used to assign a value to a variable. 127; variable. x= c count E m count = count + 1; E = m * c * c; 186000 79 78 3309875 930000 5 x 290 127 Input/output statements are used to retrieve external values (input) and to file away or print information (output). (input) cout << “Enter user’s name: ”; cin >> username; dataFile >> nextDataValue; if (nextDataValue > 0) positiveFile << nextDataValue; positiveFile dataFile 25 48 13 89 63 63 50 91 34 17 77 23 56 positiveFile nextDataValue 25 94 25 Chapter 8 High-Level High-Level Programming Languages Languages Page 6 Page Imperative Statements - Part Two Imperative Conditional statements are used to enable alternative steps based on a condition. steps if (total == 0) cout << “Possible Drop”; cout else cout << “Total: ” << total; cout switch (AreaCode) { case 701: cout << “ND”; break; case case 218: case case 507: case case 612: cout << “MN”; break; case } Iterative statements are used to loop through a sequence of instructions. instructions. while (flag == false) { cin >> newValue; cin if (newValue > 0) if flag = true; flag } total = 0; for (i = 0; i <= 24; i++) { quizFile >> score[i]; quizFile total += score[i]; total } Chapter 8 High-Level High-Level Programming Languages Languages Page 7 Page Imperative Statements - Part Three Imperative Procedures and functions are used to conveniently Procedures and write programs in a modular fashion. write typedef int intList[100]; void getList(intList list) { int count; int for (count = 0; count < 100; count++) for cin >> list[count]; cin } int maximum(intList list) { int maxSoFar; int int count; int maxSoFar = list[0]; maxSoFar for (count = 1; count < 100; count++) for if (list[count] > maxSoFar) if maxSoFar = list[count]; maxSoFar return maxSoFar; return } void main() { intList IQlist; intList intList SATlist; intList int maxIQ; int int bestSAT; int getList(IQlist); getList(IQlist); maxIQ = maximum(IQlist); maxIQ getList(SATlist); getList(SATlist); bestSAT = maximum(SATlist); bestSAT cout << “The highest IQ is ” cout << maxIQ << “ and the ” << << “best SAT score is ” << << bestSAT; << } Chapter 8 High-Level High-Level Programming Languages Languages Page 8 Page Example: What Does This Program Do? Do? typedef int intList[4]; void getList(intList list) { int count; int for (count = 0; count < 4; count++) for cin >> list[count]; cin } int drew(intList list, int item) { int bestSoFar, bestIndex, index; int bestIndex = -1; bestIndex bestSoFar = 0; bestSoFar for (index = 0; index < 4; index++) for if ((list[index] > bestSoFar) && if (list[index] <= item)) (list[index] { bestIndex = index; bestIndex bestSoFar = list[index]; bestSoFar } return bestIndex; return } void main() { intList estimate; intList int bestGuesser; int int price; int cin >> price; cin getList(estimate); getList(estimate); bestGuesser = drew(estimate, price); bestGuesser if (bestGuesser == -1) if cout << “NO WINNER”; cout else else { cout << bestGuesser << “ WINS! ”; cout if (estimate[bestGuesser] == price) price) cout << “WITH A BONUS!!!”; cout } } What would be the output of this What program for the following input file? program 600 400 675 525 450 Chapter 8 High-Level High-Level Programming Languages Languages Page 9 Page Object-Oriented Programming Object-Oriented Early third-generation programming languages used a Early “procedure-oriented” approach, in which the way way something was done was the center of attention for the programmer. programmer. More recently, with the advent of graphical user More interfaces and massive databases, the focus has shifted to an “object-oriented” approach, emphasizing what is being manipulated instead of how. what how Chapter 8 High-Level High-Level Programming Languages Languages Page 10 Page 10 The Three Principles of OOP The Chapter 8 High-Level High-Level Programming Languages Languages Page 11 Page 11 ...
View Full Document

Ask a homework question - tutors are online