Download presentation
Presentation is loading. Please wait.
1
High-Level Programming Languages
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
2
High-Level Programming Languages
Compilation Source Program (in TGL) Object Program (in Machine Language) Lexical Analysis Parsing Code Generation 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
3
High-Level Programming Languages
Linking and Loading 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 Object Program Load Module Executable Program Compile Link Load 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
4
Standard Source Program Organization
Source programs in most third-generation languages generally follow a standard pattern. void main() { const int maxCount = 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 int maxCount = 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 int maxCount = 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; 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. Chapter 8 High-Level Programming Languages Page 76
5
High-Level Programming Languages
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]; int examScore[5]; int quizScore[25]; int paperScore[2]; char letterGrade; }; Student FALL111[25]; Chapter 8 High-Level Programming Languages Page 77
6
High-Level Programming Languages
Imperative Statements - Part One Assignment & I/O Assignment statements are used to assign a value to a variable. x = 127; c count E m x 186000 78 5 290 c count E m x 186000 78 5 127 c count E m x 186000 79 930000 5 127 c count E m x 186000 79 5 127 c count E m x 186000 79 930000 5 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 nextDataValue 25 nextDataValue 94 nextDataValue 25 Chapter 8 High-Level Programming Languages Page 78
7
High-Level Programming Languages
Imperative Statements - Part Two Control Statements Conditional statements are used to enable alternative steps based on a condition. if (total == 0) cout << “Possible Drop”; else cout << “Total: ” << total; switch (AreaCode) { case 701: cout << “ND”; break; case 218: case 507: case 612: cout << “MN”; break; } Iterative statements are used to loop through a sequence of instructions. while (flag == false) { cin >> newValue; if (newValue > 0) flag = true; } total = 0; for (i = 0; i <= 24; i++) { quizFile >> score[i]; total += score[i]; } Chapter 8 High-Level Programming Languages Page 79
8
High-Level Programming Languages
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; 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
9
Example: What Does This Program Do?
typedef int intList[4]; void getList(intList list) { int count; for (count = 0; count < 4; count++) cin >> list[count]; } int drew(intList list, int item) int bestSoFar, 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; int bestGuesser; 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? Chapter 8 High-Level Programming Languages Page 81
10
High-Level Programming Languages
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
11
High-Level Programming Languages
The Three Principles of OOP “Hide” information from objects that don’t need it. Is the search being performed sequential or binary? Is the data in an array or separate variables? Is the input coming from the user or from a file? The code will be more robust if it’s not unnecessarily dependent on information that it can perform without! Encapsulation Don’t “reinvent the wheel” when creating new data types. A GUI Window is rectangular with a title bar. A Document Window also has a menu bar, and max & min buttons. Why not let the Document Window “inherit” as much behavior as possible from the GUI Window (e.g., how to draw it, how to place text in its title bar)? Inheritance Some objects are “similar”, without being the same. A Triangle object needs its own method for “Drawing”. A Circle object needs its own method for “Drawing”. With polymorphism, you can write code to invoke “Drawing” without having to spell out what type of “Drawing” is intended. Polymorphism Chapter 8 High-Level Programming Languages Page 83
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.