Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,

Slides:



Advertisements
Similar presentations
An Introduction to Programming General Concepts. What is a program? A program is an algorithm expressed in a programming language. programming language.
Advertisements

Programming Languages and Paradigms
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
High-Level Programming Languages
Chapter 8 High-Level Programming Languages Nell Dale John Lewis.
11/1/06 1 Hofstra University, CSC005 Chapter 8 (Part 3) High Level Programming Languages.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Chapter 1 Program Design
String Escape Sequences
Object-Oriented Design and High-Level Programming Languages
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Invitation to Computer Science 5th Edition
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
High-Level Programming Languages: C++
Chapter 8 High-Level Programming Languages. 2 Compilers High-level language A language that provides a richer (more English like) set of instructions.
Chapter 9 Object-Oriented Design and High-Level Programming Languages.
CIS Computer Programming Logic
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Chapters 7, 8, & 9 Quiz 3 Review 1. 2 Algorithms Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of.
Introduction to Computer Systems and the Java Programming Language.
Chapter 9 Programming Languages
CPS120: Introduction to Computer Science
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Chapter 6 Programming Languages © 2007 Pearson Addison-Wesley. All rights reserved.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
Chapter 6 Programming Languages (1) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Introduction to Java Java Translation Program Structure
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Chapter 8 High-Level Programming Languages. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
CPS120: Introduction to Computer Science Variables and Constants.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
CPS120 Introduction to Computer Science High Level Language: Paradigms.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Information and Computer Sciences University of Hawaii, Manoa
Definition of the Programming Language CPRL
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Multiple variables can be created in one declaration
An Introduction to Programming
The Selection Structure
Computer Programming.
High Level Programming Languages
Low Level Programming Languages
Statement-Level Control Structures
Flow of Control.
The Data Element.
An Introduction to Programming
The Data Element.
Presentation transcript:

Chapter 8 High-Level Programming Languages

8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation, and execution Describe the following constructs: stream input and output, selection, looping, and subprograms Construct Boolean expressions and describe how they are used to alter the flow of control of an algorithm Explain the concept of a parameter and distinguish between value and reference parameters

8-3 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions that makes the programmer’s life even easier

8-4 Compilers Figure 8.1 Compilation process Each high-level language must have many compilers for that language (each machine needs a dedicated compiler)

8-5 Interpreters Interpreter A translating program that translates and executes the statements in sequence –An interpreter translates a statement and then immediately executes the statement. Unlike an assembler or compiler which produce machine code as output, which is then executed in a separate step –Interpreters can be viewed as simulators Compiler: Fortran, Cobol, C Interpreter (more complex) : Basic, Lisp, APL Java slower

8-6 Java Introduced in 1996 and swept the computing community by storm 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

8-7 Portability Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode

8-8 Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode Portability

8-9 Programming Language Paradigms What is a paradigm? One that serves as a pattern or model A set of assumptions, concepts, values, and practices that constitute a way of viewing reality

8-10 Programming Language Paradigms 1. Imperative or procedural model –FORTRAN, COBOL, BASIC, C, Pascal, Ada, and C++ –Most popular model 2. Functional model –LISP, Scheme (a derivative of LISP), and ML –Use recursive function calls instead of looping structure

8-11 Programming Language Paradigms 3. Logic programming –LISP and PROLOG (AI applications) –Objects and relationships 4. Object-oriented paradigm –SIMULA and Smalltalk –C++ is as an imperative language with some object-oriented features –Java is an object-oriented language with some imperative features

8-12 Functionality of Imperative Languages Sequence Executing statements in sequence until an instruction is encountered that changes this sequencing From the first to last statements (from top to down) Selection Deciding which action to take Iteration (looping) Repeating an action Both selection and iteration require the use of a Boolean expression a=4; b=a+3; c=b-2; …..

8-13 Boolean Expressions Boolean expression A sequence of identifiers, separated by compatible operators, that evaluates to true or false 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

8-14 Boolean Expressions Variable A location in memory that is referenced by an identifier that contains a data value Thus, a Boolean variable is a location in memory that can contain either true or false

8-15 Boolean Expressions A relational operator between two arithmetic expressions is asking if the relationship exists between the two expressions For example, xValue < yValue Page 233 if (x>y) while (z<=6) …

8-16 Strong Typing Strong typing The requirement that only a value of the proper type can be stored into a variable (store number to character) Data type A description of the set of values and the basic set of operations that can be applied to values of the type

8-17 Data Types Integer numbers Real numbers Characters Boolean values Strings (“hello”)

8-18 Integers The range varies depending upon how many bytes are assigned to represent an integer value Some high-level languages provide several integer types of different sizes Operations that can be applied to integers are the standard arithmetic and relational operations

8-19 Reals Like the integer data type, the range varies depending on the number of bytes assigned to represent a real number Many high-level languages have two sizes of real numbers (float and double) The operations that can be applied to real numbers are the same as those that can be applied to integer numbers

8-20 Characters It takes one byte to represent characters in the ASCII character set Two bytes to represent characters in the Unicode character set Our English alphabet is represented in ASCII, which is a subset of Unicode

8-21 Characters Applying arithmetic operations to characters doesn’t make much sense Comparing characters does make sense, so the relational operators can be applied to characters The meaning of “less than” and “greater than” when applied to characters is “comes before” and “comes after” in the character set

8-22 Boolean The Boolean data type consists of two values: true and false Not all high-level languages support the Boolean data type If a language does not, then you can simulate Boolean values by saying that the Boolean value true is represented by 1 and false is represented by 0

8-23 Strings A string is a sequence of characters considered as one data value For example: “This is a string.” –Containing 17 characters: one uppercase letter, 12 lowercase letters, three blanks, and a period The operations defined on strings vary from language to language –They include concatenation of strings and comparison of strings in terms of lexicographic order

8-24 Declarations Declaration A statement that associates an identifier with a variable, an action, or some other entity within the language that can be given a name so that the programmer can refer to that item by name

Declarations 8-26 obtain memory space

8-26 Declarations Reserved word A word in a language that has special meaning Case-sensitive Uppercase and lowercase letters are not considered the same (such as apple and APPLE)

8-27 Assignment Statement 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 y=a+4*8-b*6+4; Named constant A location in memory, referenced by an identifier, that contains a data value that cannot be changed

Constant Declaration Page

Input/Output Structures In our pseudocode algorithms we have used the expressions Read and Write High-level languages view input data as a stream of characters divided into lines This is an apple. I like apples. You don’t like apples.

8-30 Input/Output Structures The key to the processing is in the data type that determines how characters are to be converted to a bit pattern (input) and how a bit pattern is to be converted to characters (output) We do not give examples of input/output statements because the syntax is often quite complex and differs so widely among high-level languages

8-31 Control Structures Control structure An instruction that determines the order in which other instructions in a program are executed Structured programming A programming methodology in which each logical unit of a program should have just one entry and one exit Sequence, selection statements, looping statements, and subprogram statements are control structures

8-32 Selection Statements The if statement allows the program to test the state of the program variables using a Boolean expression Page 243

8-33 Selection Statements Figure 8.3 Flow of control of if statement

8-34 Selection Statements compound statement (or block) C and Java use { }

8-35 Selection Statements Page 245

8-36 case Statement For convenience, many high-level languages include a case (or switch) statement Allows us to make multiple-choice decisions easier, provided the choices are discrete Page 246

8-37 Looping Statements The while statement is used to repeat a course of action Let’s look at two distinct types of repetitions

8-38 Looping Statements Count-controlled loops –Repeat a specified number of times –Use of a special variable called a loop control variable Figure 8.4 Flow of control of while statement

8-39 Looping Statements Count-controlled loops  count++

8-40 Looping Statements Event-controlled loops –The number of repetitions is controlled by an event that occurs within the body of the loop itself Page 249 Stop looping when we get a negative number

8-41 Looping Statements –Event-controlled loops Page 249

8-42 Subprogram Statements We can give a section of code a name (sub-program) and use that name as a statement in another part of the program (calling program, might be main program) When the name is encountered, the processing in the other part of the program halts while the named code is executed

8-43 Subprogram Statements There are times when the calling unit needs to give information to the subprogram to use in its processing A parameter list is a list of the identifiers with which the subprogram is to work, along with the types of each identifier placed in parentheses beside the subprogram name

8-44 Subprogram Statements Figure 8.5 Subprogram flow of control

8-45 Subprogram Statements Figure 8.5 Subprogram flow of control

8-46 Subprogram Statements Parameters Identifiers listed in parentheses beside the subprogram declaration; sometimes they are called formal parameters Arguments Identifiers listed in parentheses on the subprogram call; sometimes they are called actual parameters

8-47 Subprogram Statements Value parameter A parameter that expects a copy of its argument to be passed by the calling unit (put on the message board)—call by value Reference parameter A parameter that expects the address of its argument to be passed by the calling unit (put on the message board) )—call by reference

8-48 Subprogram Statements Page 253

8-49 Subprogram Example One call by value One call by reference void main(){ int i; int i; int a=1; int a=1; int b=5; int b=5; i=sum(a,b); i=sum(a,b); print(a,b,i); print(a,b,i); } int sum(int x,int y){ void sum(int x, int y){ x++; y--; x++; y--; return x+y;} return x+y;} Result: a=1, b=5, i=6 Result: a=2, b=4, i=6

8-50 Recursion Recursion The ability of a subprogram to call itself Each recursive solution has at least two cases –Base case The case to which we have an answer (end of subprogram) –General case The case that expresses the solution in terms of a call to itself with a smaller version of the problem (reduction of subprogram) 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)!

8-51 Recursion Factorial 10!=10*9*8*….*2*1  If you know multiplication of two operands only How to calculate 10!? If you know 9!, you can calculate 10! easily. (10!=9!*10) How to calculate 9!? If you know 8!, you can calculate 9! easily. (9!=8!*9) How to calculate 8!? If you know 7!, you can calculate 8! easily. (8!=7!*8) ……

8-52 Recursion void main(){ int number,result; printf(“How many factorial?"); scanf("%d",&number); result=factorial (number); printf(“result is %d”,result); } int factorial (int num) { int temp; if(num==1) temp=1; else temp=factorial(num-1)*num; return temp; } 10!=9!*10 9!=8!*9 8!=7!*8 7!=6!*7 …. This program must be modified to calculate higher factorial. subprogram mainprogram

8-53 Recursion-Towers of Hanoi We are given n disks and three towers. 1. The disks are initially stacked on tower 1 (source) in decreasing order of size from bottom to top. 2. Move the disks to tower 2(destination), one disk at a time, such as no disk is ever on top of a smaller one. Tower 1 Tower 2Tower 3 SourceDestination Auxiliary Tower

8-54 Recursion-Towers of Hanoi Tower 1 Tower 2Tower 3 sou. des. Auxiliary Tower 1.Move (n-1) disks from sou. to aux. 2.Move disk n form sou. to des. 3.Move (n-1) disks from aux. to des.

8-55 Recursion-Towers of Hanoi Main program void main(){ char source,auxiliary,destination; int disk; printf(“How many disk?"); scanf("%d",&disk); scanf("%c"); //erase enter printf(“Source Tower:"); scanf("%c",&source);scanf("%c"); printf(“Auxiliary Tower:"); scanf("%c",&auxiliary); scanf("%c”); printf(“Destination Tower"); scanf("%c",&destination); scanf("%c"); move(disk,source,auxiliary,destination); }

8-56 Recursion-Towers of Hanoi Sub program void move(int n, char sou, char aux, char des){ if(n==1) printf("move disk from %c to %c\n",sou,des); else{ move(n-1,sou,des,aux); //move from sou to aux printf("move disk from %c to %c\n",sou,des); move(n-1,aux,sou,des); //move from aux to des }

8-57 Recursion-Towers of Hanoi move(4, A,,C) move(3, A,, B) move(3, B,, C) sou.des. move(2, A,, C) move(2, C,, B) move(2, B,, A)move(2, A,, C) move from A to C move from A to B move from B to C

8-58 Asynchronous Processing Asynchronous processing The concept that input and output can be accomplished through windows on the screen –Clicking has become a major form of input to the computer –Mouse clicking is not within the sequence of the program –A user can click a mouse at any time during the execution of a program –This type of processing is called asynchronous

8-59 Arrays An array is a named collection of homogeneous items in which individual items are accessed by their place within the collection –The place within the collection is called an index

8-60 Arrays Figure 8.8 Array variable tenThings accessed from 0..9

8-61 Composite Data Types Records –A record is a named heterogeneous collection of items in which individual items are accessed by name –The elements in the collection can be of various types

8-62 Composite Data Types Page 258

8-63 Composite Data Types Page 259

8-64 Object-Oriented Design A problem-solving methodology that produces a solution to a problem in terms of self-contained entities called objects Object A thing or entity that makes sense within the context of the problem For example, a student John

8-65 Object-Oriented Design A group of similar objects is described by an object class, or class (ex: student) A class contains fields that represent the properties and behaviors of the class –A field can contain data value(s) (attributes) and/or methods (subprograms) –A method is a named algorithm that manipulates the data values in the object

8-66 Relationships Between Classes Containment –“part-of” –An address class may be part of the definition of a student class Inheritance –Classes can inherit data and behavior from other classes –“is-a”

8-67 Object-Oriented Design Animal Mammal Dog Cat... John Betty Containment Inheritance Horse