Chapter 8 Introduction to High-Level Language Programming.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Three types of computer languages
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, Java Version, Third Edition.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Introduction to C Programming
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, C++ Version, Fourth Edition.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Chapter 8 Introduction to High-Level Language Programming.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Chapter 01: Introduction to Computer Programming
COMPUTER SCIENCE I C++ INTRODUCTION
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
High-Level Programming Languages: C++
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Completing the Problem-Solving Process
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
The Selection Structure
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Chapter 8: Introduction to High-Level Language Programming
Chapter 2 Elementary Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Programming Funamental slides
1.13 The Key Software Trend: Object Technology
Programming Funamental slides
Course websites CS201 page link at my website: Lecture slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Topics Designing a Program Input, Processing, and Output
Capitolo 1 – Introduction C++ Programming
Presentation transcript:

Chapter 8 Introduction to High-Level Language Programming

Objectives In this chapter, you will learn about  Where do we stand?  High-level languages  Introduction to C++  Virtual data storage  Statement types  Putting the pieces together

Objectives Postponed (Covered in CS23021)  Functions  Managing complexity  Object-oriented programming  Graphical programming  The big picture: Software engineering

Where Do We Stand?  Early days of computing Programmers used assembly language Programmers used assembly language Programs written by technically oriented peoplePrograms written by technically oriented people Assembler programs Assembler programs Were machine specificWere machine specific Required programmers take a microscopic view of a taskRequired programmers take a microscopic view of a task  Later decades Programmers demanded a more comfortable programming environment Programmers demanded a more comfortable programming environment Programs could be written by “nontechie” peoplePrograms could be written by “nontechie” people Programs could be portable rather than machine specificPrograms could be portable rather than machine specific Programmers could avoid data storage and movement issuesProgrammers could avoid data storage and movement issues

High-level Languages  High-level programming languages Called third-generation languages Called third-generation languages Machine language was “first generation”Machine language was “first generation” Assembly language was “second generation”Assembly language was “second generation” Overcame deficiencies of assembly language Overcame deficiencies of assembly language Programmer didn’t need to manage details of data storage or movement Programmer didn’t need to manage details of data storage or movement

High-level Languages (continued)  Expectations of a high-level language program (continued) Programmer can take a macroscopic view of tasks; “primitive operations” can be larger Programmer can take a macroscopic view of tasks; “primitive operations” can be larger Program will be portable Program will be portable Code will be closer to standard English and use standard mathematical notation Code will be closer to standard English and use standard mathematical notation

Figure 8.1 Transitions of a High-level Language Program

Introduction to C++  Some components of program on next slide Comments Comments Anything following “//” on a line is a commentAnything following “//” on a line is a comment Give information to human readers of codeGive information to human readers of code The “include” directive The “include” directive The linker includes object code from a libraryThe linker includes object code from a library C++ does not provide input or output of data (I/O)C++ does not provide input or output of data (I/O) The “using” directive The “using” directive Tells compiler to look in a namespace for definitions not mentioned in the programTells compiler to look in a namespace for definitions not mentioned in the program Used here for I/O commands “cin” & “cout”Used here for I/O commands “cin” & “cout” Main program code in brackets after “main” Main program code in brackets after “main”

A Simple C++ Program Figure 8.2

Figure 8.3 The Overall Form of a Typical C++ Program

Data Types  Identifiers: Names in a programming language Any combination of letters, digits, and underscore symbol that does not start with a digit. Any combination of letters, digits, and underscore symbol that does not start with a digit. Should select names suggestive of meaning Should select names suggestive of meaning  Keywords: Have special meanings in C++ Also called reserved words. Also called reserved words. Can not be used as an identifier Can not be used as an identifier  C++ is a case-sensitive, free-format language  Data items can be constants or variables

Data Types (continued)  A declaration of a data item tells Whether the item is a constant or a variable Whether the item is a constant or a variable The identifier used to name the item The identifier used to name the item The data type of the item The data type of the item

Figure 8.5 Some of the C++ Standard Data Types

Data Types (continued)  An array Groups together a collection of memory locations, all storing data of the same type Groups together a collection of memory locations, all storing data of the same type Example Example Int Hits[12] Figure 8.6 A 12-Element Array Hits

Statement Types  Input/output statements Input statement Input statement Collects a specific value from the user for a variable within the programCollects a specific value from the user for a variable within the program Output statement Output statement Writes a message or the value of a program variable to the user’s screen or to a fileWrites a message or the value of a program variable to the user’s screen or to a file

Statement Types (continued)  Assignment statement Assigns a value to a program variable Assigns a value to a program variable  Control statement Directs the flow of control Directs the flow of control Can cause it to deviate from usual sequential flowCan cause it to deviate from usual sequential flow

Input/Output Statements  Example Pseudocode Pseudocode Get value for Radius (interactively from keyboard) C++ C++ cin >> Radius;  cin: Input stream  Code for extraction operator (>>) and the definition of the cin stream come from the iostream library and std namespace

Input/Output Statements (continued)  Example Pseudocode Pseudocode Print the value of Circumference (on screen) C++ C++ cout << Circumference;  cout: Output stream  Code for the insertion operator (<<) and the definition of the cout stream come from the iostream library and std namespace

Input/Output (cont.)  Consider real numbers such as The fixed format output of this number would be The fixed format output of this number would be In scientific notation, this output would be In scientific notation, this output would be e+001 The computer is free to choose the output style. The computer is free to choose the output style. To specify fixed format output, include the following formatting statement in the program To specify fixed format output, include the following formatting statement in the programcout.setf(ios::fixed); To require the output be in scientific notation, use To require the output be in scientific notation, usecout.setf(ios::scientific); To require two decimal places, use To require two decimal places, usecout.precision(2)

Output using literal strings  Suppose we want to output message before the value, such as The circumference that corresponds to this radius is  This could be accomplished using cout << “The concumference that corresponds “ << “to this radius is “ << Circumference << “to this radius is “ << Circumference  Many other output features are available Some additional information is given in textbook Some additional information is given in textbook We won’t cover the output options in detail here. We won’t cover the output options in detail here.

The Assignment Statement  General form Pseudocode Pseudocode Set the value of “variable” to “arithmetic expression” C++ assignment statement evaluation C++ assignment statement evaluation variable = expression; 1.Expression on the right is evaluated 2.The result is written into the memory location specified on the left  Example: Consider assemby code for A= B + C - 3;

Assignment Statements (cont.)  The C++ symbols for four basic operations + Addition - Subtraction * Multiplication / Division Example: The product of A & B is A*B. Example: The product of A & B is A*B.  Data Type Considerations: If A and B are integers, then A/B is the integer obtained by truncation: 7/2 is 3 If A and B are integers, then A/B is the integer obtained by truncation: 7/2 is 3 If either A or B is a real number then A/B is a real number: 7/2 is 3.5 If either A or B is a real number then A/B is a real number: 7/2 is 3.5  Other data-type considerations are covered in textbook, and will be used, as needed.

Control Statements  Types of control mechanisms Sequential Sequential Instructions are executed in orderInstructions are executed in order Conditional Conditional Choice of which instructions to execute next depends on some conditionChoice of which instructions to execute next depends on some condition Looping Looping Group of instructions may be executed many timesGroup of instructions may be executed many times

Control Statements (continued)  Sequential is the default mode of execution  Conditional flow of control Evaluation of a Boolean condition (also called a Boolean expression) Evaluation of a Boolean condition (also called a Boolean expression) Which programming statement to execute next is based on the value of the Boolean condition (true or false) Which programming statement to execute next is based on the value of the Boolean condition (true or false)

Control Statements (continued)  Conditional flow of control (continued) if-else statement if-else statement if (Boolean condition) S1;elseS2; if variation of the if-else statement if variation of the if-else statement if (Boolean condition) S1;

Figure 8.10 Conditional Flow of Control (flowchart) (If-Else)

Figure 8.11 If-Else with Empty Else

Control Statements (continued)  Looping (iteration) The loop body may be executed repeatedly based on the value of the Boolean condition The loop body may be executed repeatedly based on the value of the Boolean condition while statement while statement while (Boolean condition) S1;

Figure 8.12 While Loop

Putting the Pieces Together  At this point, we can Perform input and output Perform input and output Assign values to variables Assign values to variables Direct the flow of control using conditional statements or looping Direct the flow of control using conditional statements or looping  For a complete program, we need to Assemble the statements in the correct order Assemble the statements in the correct order Fill in the missing pieces Fill in the missing pieces

Meeting Expectations  C++ meets the four expectations for a high- level programming language  Expectations A programmer need not manage the details of the movement of data items within memory, nor pay any attention to where they are stored A programmer need not manage the details of the movement of data items within memory, nor pay any attention to where they are stored

Meeting Expectations  Expectations (continued) Programmer can take a macroscopic view of tasks, thinking at a higher level of problem solving Programmer can take a macroscopic view of tasks, thinking at a higher level of problem solving Programs written in high-level languages will be portable rather than machine-specific Programs written in high-level languages will be portable rather than machine-specific Programming statements in a high-level language Programming statements in a high-level language Will be closer to standard EnglishWill be closer to standard English Will use standard mathematical notationWill use standard mathematical notation

Pseudocode Language Instructions in C++ (Sections 8.1 – 8.6) Recall pseudocode language instructions (pg 58)  Computation: Set value of “variable” to “arithemetic expression” Set value of “variable” to “arithemetic expression”  Input/Output: Get a value for “variable” Get a value for “variable” Print the value for “variable” Print the value for “variable” Print the message “message” Print the message “message”  Conditional: If “a boolean expression” is true then If “a boolean expression” is true then First set of algorithmic operations Else Else Second set of algorithmic operations Second set of algorithmic operations

Pseudocode Language Instructions (cont.)  Iteration (looping): While (a boolean condition is true) do While (a boolean condition is true) dooperationoperation operation End of Loop End of Loop

Computation: Expressions in C++ OperatorPrecedenceAssociativity ( ) 1na +, -, ++, --, ! ( unary ) 2 L->R (+a, -a, ++a, --a, !a) R->L (a++, a--) *, /, % 3L->R +, - 4L->R, =, =5L->R ==, != 6L->R &&7L->R ||8L->R =9R->L An expression in C++ is a sequence of operators and operands which adhere to the C++ syntax rules. The operators are grouped according to an order of precedence, and associativity

Computation: Expressions in C++ Example 1: a = b + c * d; Example 2: a = b + c * d – e; Example 3: a = (b + c) * (d-e); Example 4: a = b / c; Example 5: a = b % c; Example 6: a = b = c – d; Other: +=, -=, *=, /=, %= ??? Assume b = 5, c = 3, d = 4, e = 2

Computation (cont.)  Consider equation: ax 2 + bx + c = 0  The roots are x = ( –b  sqrt(b 2 – 4ac) ) / 2a  Using a high level language we could write: discrim = b*b – 4*a*c; discrim = b*b – 4*a*c; root1 = (-b + sqrt(discrim)) / (2*a); root1 = (-b + sqrt(discrim)) / (2*a); root2 = (-b – sqrt(discrim)) / (2*a); root2 = (-b – sqrt(discrim)) / (2*a);  This closely resembles the way we look at the problem mathematically

/*circle03.cpp Gets the radius of a circle, calculates it’s circumference and prints out the result Gets the radius of a circle, calculates it’s circumference and prints out the result*/ #include #include using std::cin; using std::cout; int main() { const double PI = ; const double PI = ; double radius = 0.0, circumference = 0.0; double radius = 0.0, circumference = 0.0; cout << "Please enter the radius " << '\n'; cout << "Please enter the radius " << '\n'; cin >> radius; cin >> radius; circumference = 2 * PI * radius; circumference = 2 * PI * radius; cout << “The circumference is: “ ; cout << “The circumference is: “ ; cout << circumference << “.\n”; cout << circumference << “.\n”; return 0; return 0;} i/o example #3  cout – prompt user for data  << (insertion operator)  cin – store data in a variable  >> (extraction operator)  cout – output data entered Input and Output in C++

Conditional Statements in C++  Conditional flow of control (continued) if-else statement if-else statement if (Boolean condition) S1;elseS2; if variation of the if-else statement if variation of the if-else statement if (Boolean condition) S1;

Figure 8.12 Conditional Flow of Control (If-Else)

Figure 8.13 If-Else with Empty Else

Iterative Statement in C++  Looping (iteration) The loop body may be executed repeatedly based on the value of the Boolean condition The loop body may be executed repeatedly based on the value of the Boolean condition while statement while statement while (Boolean condition) S1;

Figure 8.14 While Loop

Interative Example in C++ Sum = 0; //initialize Sum cout << “Please enter the numbers to add; “; cout << “terminate with a negative number.” << endl; cin >> Number; // this will get the first value while (Number >= 0) { Sum = Sum + Number; cin >> Number; } cin >> Number; } cout << “The total is “ << Sum << endl;

Summary  In a high-level language, the programmer: Need not manage storage Need not manage storage Can think about the problem at a higher level Can think about the problem at a higher level Can use more powerful and more natural- language-like program instructions Can use more powerful and more natural- language-like program instructions Can write a much more portable program Can write a much more portable program