Chapter 9 HighLevel Programming Languages Compilers Highlevel language









































- Slides: 41
Chapter 9 High-Level Programming Languages
Compilers High-level language A language that provides a richer (more English like) set of instructions Compiler A program that translates a high-level language program into machine code 2
Programming Language Paradigms Imperative paradigm – Program describes the processing • Specifies “HOW” to perform a given task – Also, called procedural paradigm • A program is a hierarchy of subprograms, each of which performs a specific task – FORTRAN, COBOL, BASIC, C, Pascal, C++ Object-oriented paradigm – Program consists of a set of objects and their interactions – Java is an object-oriented language with some imperative features – C++ is as an imperative language with some object-oriented features 3
Programming Language Paradigms Functional paradigm – Program is written terms of mathematical functions • Specifies “WHAT” to do – LISP, Scheme (a derivative of LISP), ML Logic paradigm – Program describes facts about objects and rules about the relationships among the objects – Then, it ask questions about the objects and their relationships, which can be deduced – PROLOG – Both functional and logic paradigms are sometimes called declarative paradigm 4
Programming Language Variable A location in memory that contains a data value and it is referenced by an identifier (name) – Boolean variable • A Boolean variable is a location in memory that can contain either true or false – Integer variable – Real variable – Character variable 5
Strong Typing Data type A description of the set of values that a variable can hold It also describes the basic set of operations that can be applied to values of the type – Integer – Real – Character – Boolean – String 6
Imperative Languages Boolean expression A sequence of identifiers, separated by compatible operators, that evaluates to true or false A Boolean expression can be – A Boolean variable – An arithmetic expression followed by a relational operator followed by an arithmetic expression – A Boolean expression followed by a Boolean operator followed by a Boolean expression 7
Relational Operators 8
Integers What determines the range of an integer value? - The range depends on how many bytes are assigned Is the range of an integer value the same in all languages? - May provide several integer types of different sizes - integer, short What operations can be applied to integers? - Standard arithmetic and relational operators 9
Reals How about real numbers? - The range depends on how many bytes are assigned - The same standard arithmetic and relational operators can be applied - May provide several real types of different sizes - float, double 10
Boolean and Strings What values can a Boolean variable be? What is a string? What operations can be applied to strings? - Concatenation - Comparison in terms of lexicographic order 11
Atomic vs. Composite Data Types Integers, reals, characters, and Booleans are called simple or atomic data types Composite data types are data types made up of a collection of values - String data type has some of the properties of a composite data type but is often considered as a simple data type 12
Declarations Declaration A statement that associates an identifier with a variable that can be given a name The programmer can refer to that item by name Reserved word (keyword) A word in a language that has special meaning Case-sensitive Uppercase and lowercase letters are not considered the same 13 - C++ and Java are case-sensitive while Ada is not
Declaration Example
Assignment statement An action statement (not a declaration) that says to evaluate the expression on the right-hand side of the symbol and store that value into the place named on the left -hand side A = B * C; Named constant A location in memory, referenced by an identifier, that contains a constant value 15 #define PI 3. 141592
If Statement Figure 8. 3 Flow of control of if statement 16
If Statement The if statement allows the program to test the state of the program variables using a Boolean expression 17
Blocks Note the symbols used to indicate blocks in each language 18
If – Else If (cascading If) Statement If (temperature > 90) Write "Texas weather: wear shorts" Else If (temperature > 50) Write "A little chilly: wear a light jacket" Else If (temperature > 32) Write "Philadelphia weather: wear a heavy coat" Else Write "Stay inside" Why does this work correctly? 19
While Loop Figure 8. 4 Flow of control of while statement 20
Counted Loop (For Loop) A counted loop Set sum to 0 Set count to 1 While (count <= limit) Read number Set sum to sum + number Increment count Write "Sum is " + sum 21
Looping Statements 22
While Loop An event-controlled loop Set sum to 0 Set all. Positive to true While (all. Positive) Read number If (number > 0) Set sum to sum + number Else Set all. Positive to false Write "Sum is " + sum 23
Subprogram (Procedure/Function) We can give a section of code a name and call that name as a statement in another part of the program result = factorial(3); …. int factorial (int N) { If (N <=1) factorial = 1; Factorial = N * factorial (N – 1); } 24
Parameter Passing What if the subprogram needs data from the calling unit? Parameters Identifiers listed in parentheses beside the subprogram declaration; sometimes called formal parameters Arguments Identifiers listed in parentheses on the subprogram call; sometimes called actual parameters 25
Procedure 26 Figure 8. 5 Subprogram flow of control
Function 27 Figure 8. 5 Subprogram flow of control
Parameter Passing Call by value A formal parameter takes a copy of its actual argument value as input Call by reference A formal parameter that takes a pointer (address) to its actual argument as input 28
Subprogram Statements 29
Recursion The ability of a subprogram to call itself Base case The case to which we have an answer General case The case that expresses the solution in terms of a call to itself with a smaller version of the problem 30
Recursion For example, the factorial of a number is defined as the number times the product of all the numbers between itself and 0: N! = N * (N 1)! Base case Factorial(0) = 1 (0! is 1) General Case Factorial(N) = N * Factorial(N-1) 31
Composite Data Types Arrays A named homogeneous collection of items in which an individual item is accessed by its position (index) within the collection Records A named heterogeneous collection of items in which individual items are accessed by name 32
Composite Data Types An Integer Array 33 Figure 8. 8 Array variable ten. Things accessed from 0. . 9
Composite Data Types Declare Array 34
Composite Data Types 35
Composite Data Types 36
Interpreters Interpreter A translating program that translates and executes the statements in sequence – Assembler or compiler produce machine code as output, which is then executed in a separate step – An interpreter translates a statement and then immediately executes the statement – Interpreters can be viewed as simulators 37
Java • Introduced in 1996 and became instantly popular • Portability was of primary importance • Java is compiled into a standard machine language called Bytecode • A software interpreter called the JVM (Java Virtual Machine) takes the Bytecode program and executes it 38
Portability Figure 8. 2 Portability provided by standardized languages versus interpretation by 39 Bytecode
Portability Figure 8. 2 Portability provided by standardized languages versus interpretation by 40 Bytecode
Homework 5 • Read Chapters 8, 7 • Exercise – Chapter 9 • 11~24, 25 -46, 49, 50, 55, 56, 69, 72 41