110 likes | 318 Views
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.
E N D
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. Third-generation languages are structured to avoid considering machine operations at all, instead concentrating on relatively straightforward instructions on how the data is being manipulated. Each type of computer that executes programs in a TGL has a special program (called a compiler) that translates the TGL code into the computer’s machine language. Example: int negative (int x) { if (x < 0) return 1; else return 0; } Consequently, a TGL program written on one machine can be run on any other computer, as long as the computer has a compiler for that TGL! Chapter 8 High-Level Programming Languages Page 73
Compilation Lexical Analysis Code Generation Parsing Object Program (in Machine Language) Source Program (in TGL) Lexical Analysis The compiler takes the TGL program (called the source program) 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. Parsing The compiler then analyzes the grammatical syntax of the program (e.g., “if”, “(”, “count”, “>”, “100”, “)” is determined to make sense, but a syntax error would be noticed in “if”, “count”, “>”, “100”, “)”.) Code Generation Once the program has been satisfactorily parsed, the compiler generates an equivalent program in machine language (called the object program). Chapter 8 High-Level Programming Languages Page 74
Linking and Loading Object Program Load Module Executable Program Link Load Compile 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 code cannot be executed until all of the units are connected together as a single machine language program. Source Program Linking 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 prior to execution by a linker program. Loading Finally, a special loader program places the resulting machine code in main memory, tying up all loose ends (e.g., setting the instruction addresses for JUMP instructions) so the code is ready for execution. Chapter 8 High-Level Programming Languages Page 75
Standard Source Program Organization Declarative Statements Constant and variable values representing terms that will be manipulated as the program is executed. Imperative Statements The procedural specification of the algorithm itself. Source programs in most third-generation languages generally follow a standard pattern. void main() { const intmaxCount = 10; int count; int value; float sum = 0.0; cout << “Input values” << endl; count = 0; while (count < maxCount) { cin >> value; sum += value; count++; } cout << “Mean value: ” << sum/count << endl; } void main() { const intmaxCount = 10; int count; int value; float sum = 0.0; cout << “Input values” << endl; count = 0; while (count < maxCount) { cin >> value; sum += value; count++; } cout << “Mean value: ” << sum/count << endl; } void main() { const intmaxCount = 10; int count; int value; float sum = 0.0; cout << “Input values” << endl; count = 0; while (count < maxCount) { cin >> value; sum += value; count++; } cout << “Mean value: ” << sum/count << endl; } Chapter 8 High-Level Programming Languages Page 76
Data Types Data types are used to specify how the bit patterns used to represent data should be interpreted by the program. Scalar: Single-valued data types (e.g., integer, floating-point, character, boolean) int count; 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]; intexamScore[5]; intquizScore[25]; intpaperScore[2]; char letterGrade; }; Student FALL111[25]; Chapter 8 High-Level Programming Languages Page 77
Imperative Statements - Part One Assignment & I/O Assignment statements are used to assign a value to a variable. x = 127; count = count + 1; E = m * c * c; Input/output statements are used to retrieve external values (input) and to file away or print information (output). Enter user’s name: MOE cout << “Enter user’s name: ”; cin >> username; dataFile >> nextDataValue; if (nextDataValue > 0) positiveFile << nextDataValue; dataFile positiveFile 25 63 17 48 50 77 13 91 23 89 34 56 25 63 17 48 50 77 13 91 23 89 34 56 25 Chapter 8 High-Level Programming Languages Page 78
Imperative Statements - Part Two Control Statements Conditional statements are used to enable alternative steps based on a condition. switch (AreaCode) { case 701: cout << “ND”; break; case 218: case 507: case 612: cout << “MN”; break; } if (total == 0) cout << “Possible Drop”; else cout << “Total: ” << total; Iterative statements are used to loop through a sequence of instructions. total = 0; for (i = 0; i <= 24; i++) { quizFile >> score[i]; total += score[i]; } while (flag == false) { cin >> newValue; if (newValue > 0) flag = true; } Chapter 8 High-Level Programming Languages Page 79
Imperative Statements - Part Three Procedures & Functions Procedures and functions are used to conveniently write programs in a modular fashion. typedef int intList[100]; void getList(intList list) { int count; for (count = 0; count < 100; count++) cin >> list[count]; } int maximum(intList list) { int maxSoFar; int count; maxSoFar = list[0]; for (count = 1; count < 100; count++) if (list[count] > maxSoFar) maxSoFar = list[count]; return maxSoFar; } void main() { intList IQlist; intList SATlist; int maxIQ; int bestSAT; getList(IQlist); maxIQ = maximum(IQlist); getList(SATlist); bestSAT = maximum(SATlist); cout << “The highest IQ is ” << maxIQ << “ and the ” << “best SAT score is ” << bestSAT; } Chapter 8 High-Level Programming Languages Page 80
Example: What Does This Program Do? typedefintintList[4]; void getList(intList list) { int count; for (count = 0; count < 4; count++) cin >> list[count]; } int drew(intList list, int item) { intbestSoFar, bestIndex, index; bestIndex = -1; bestSoFar = 0; for (index = 0; index < 4; index++) if ((list[index] > bestSoFar) && (list[index] <= item)) { bestIndex = index; bestSoFar = list[index]; } return bestIndex; } void main() { intList estimate; intbestGuesser; int price; cin >> price; getList(estimate); bestGuesser = drew(estimate, price); if (bestGuesser == -1) cout << “NO WINNER”; else { cout << bestGuesser << “ WINS! ”; if (estimate[bestGuesser] == price) cout << “WITH A BONUS!!!”; } } What would be the output of this program for the following input file? 600 400 675 525 450 Chapter 8 High-Level Programming Languages Page 81
Object-Oriented Programming Early third-generation programming languages used a “procedure-oriented” approach, in which the way something was done was the center of attention for the programmer. More recently, with the advent of graphical user interfaces and massive databases, the focus has shifted to an “object-oriented” approach, emphasizing what is being manipulated instead of how. Chapter 8 High-Level Programming Languages Page 82
The Three Principles of OOP Chapter 8 High-Level Programming Languages Page 83