Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process...
-
Upload
giles-jenkins -
Category
Documents
-
view
216 -
download
0
Transcript of Chapter 9 High-Level Programming Languages: C++. 2 Chapter Goals Describe the translation process...
Chapter 9
High-Level Programming Languages: C++
2
Chapter Goals
• Describe the translation process and distinguish between assembly, compilation, interpretation, and execution
• Name four distinct programming paradigms and name a language characteristic of each
• Define the concepts of a data type and strong typing
• Describe the structure of a C++ program
Chapter Goals
• Construct valid numeric and string expressions
• Construct input and output expressions
• Implement selection statements in C++
• Implement loop structures in C++
• Implement void and value-return functions
• Define subprogram statements and their use
• Use of nested logic
3
4
Compilers
• High-level languages provide a richer set of instructions that makes the programmer’s life even easier– C++, Java, Lisp, Prolog are high-level
languages
• Compiler A program that translates a high-level language program into machine code– C++ is a compiled language
5
Compilers
Figure 8.1 Compilation process
Interpreters
• Interpreter An interpreter translates a statement and then immediately executes the statement
– Lisp and Prolog are interpreted languages
• Java is first compiled in an intermediate language (called Bytecode) and the Bytecode is interpreted.
6
7
Java
• Introduced in 1996 and swept the computing community by storm
• 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
• Portability was of primary importance
8-8
C++ Portability
Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode
8-9
Java Portability
Figure 8.2 Portability provided by standardized languages versus interpretation by Bytecode
10
Programming Language Paradigms
• What is a paradigm?
• A set of assumptions, concepts, values, and practices that constitute a way of viewing reality
11
Programming Language Paradigms
• Imperative or procedural model– FORTRAN, COBOL, BASIC, C, Pascal, Ada
• Functional model– LISP, Scheme (a derivative of LISP), and ML
8-12
Programming Language Paradigms
• Logic programming– PROLOG
• 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
A simple C++ program
13
A C++ program template
14
Introduction to C++
• Some components of a C++ template program
– Comments
• Give information to human readers of code
– Include directive
• The linker includes object code from a library
– Using directive
• Tells compiler to look in a namespace for definitions not mentioned in the program
15
Virtual Data Storage
• Identifiers: names in a programming language
• Keywords: have special meanings in C++
• C++: case-sensitive, free-format language
• Data items can be constants or variables
16
Some C++ Standard Data Types
17
• A declaration of a data item tells– Whether the item is a constant or a variable– The identifier used to name the item– The data type of the item
Example of a variable integer declarationint numb;
18
Other Data Types
• An array– Groups together a collection of memory locations, all storing data of the same type
Example:int Hits[12];
Other data types
• Strings– A string is a sequence of characters
Example:string s=“Intro to CS”;
creates a string s that contains “Intro to CS”
19
20
Statement Types
• Input/output statement
– Input statement
• Collects a specific value from the user for a variable within the program
– Output statement
• Writes a message or the value of a program variable to the user’s screen or to a file
21
Control Statements
• Types of control mechanisms
– Sequential
• Instructions are executed in order
– Selection or Conditional
• Choice of which instructions to execute next depends on some condition
– Looping
• Group of instructions may be executed many times
22
Input Statements
• Example– Pseudocode
Get value for Radius
– 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
23
Output Statements
• Example– Pseudocode
Print the value of Circumference
– 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 and Output
• Cin reads from the keyboard and cout writes on the screen– Cin skips the whitespace
• Function get reads one character from an input stream– Stores the character read in a variable of type
char, the single argument the function takes
cin.get(character);
24
The Assignment Statement
• General form– Pseudocode
Set the value of “variable” to “arithmetic expression”
– C++variable = expression;
1. Expression on the right is evaluated
2. The result is written into the memory location named on the left
25
Selection Statements
• Default mode of execution: sequential
• Selection statement
• Evaluation of a Boolean condition (also called a Boolean expression)
• Which programming statement to execute next is decided based on the value of the Boolean condition (true or false)
26
Selection Statements
• Selection statements– if-else statement
if (Boolean condition)
S1;
else
S2;
– if variation of the if-else statement
if (Boolean condition)
S1;
27
Loops
• Looping (iteration)
– The loop body may be executed repeatedly based on the value of the Boolean condition
– while statement
while (Boolean condition)
S1;
28
Putting the Pieces Together
• At this point, we can:
– Perform input and output
– Assign values to variables
– Direct the flow of control using conditional statements or looping
• For a complete program, we need to:
– Assemble the statements in the correct order
– Fill in the missing pieces
29
Simple example in C++Problem: Write a program that evaluates the arithmetic
expression A = B + C – 7. Assign an arbitrary value to B and C inside the program. Print A.
• This corresponds to the pseudo code instruction:• Set A to B plus C minus 7• Print A
• The C++ program:
30
//simple example#include <iostream>using namespace std;
int main(){
int B=10;int C=5;int A=B+C-7;cout << “the value of A is “ <<
A << endl;return 0;}
Modification 1 of the simple example in C++
• MODIFICATION 1: Now I want to ask the user to provide for me the values of B, and C. Then I want to output the value of A.
– This corresponds to the pseudo code:– Get the value of B and C – Print the value of A
• cin and cout are the commands for getting data in input from the keyboard and printing a data in output on the screen
• Note that in computer numbers are converted in characters in order to be printed since the screen displays ASCII codes!
31
Simple example in C++: Modification 1
• The C++ program:
32
//simple example: Modification 1#include <iostream>using namespace std;
int main(){
int B, C;cout << “Insert the value of B: “;cin >> B;cout << “Insert the value of C: “;cin >> C;int A=B+C-7;cout << “the value of A is “ << A
<< endl;return 0;}
Modification 2 of the simple example in C++
• MODIFICATION 2: The second change consists in checking if B and C are positive (negative values are not accepted). If B and C are not positive, force them to be positive (i.e. If B receive -5 turn B into +5). Then set A=B+C-7
–This corresponds to the pseudo code:
If (B < 0) then Set B to B*(-1)
If (C < 0) then Set C to C*(-1)
Set A to B+C-7
33
Simple example in C++: Modification 2
• The new C++ program:
34
//simple example: Modification 2#include <iostream>using namespace std;
int main(){
int B, C;cout << “Insert the value of B: “;cin >> B;if (B < 0) B=B*(-1);cout << “Insert the value of C: “;cin >> C; if (C < 0) C=C*(-1);int A=B+C-7;cout << “the value of A is “ << A
<< endl;return 0;}
Use of IF
Modification 3 of the simple example in C++
• Nested Logic
• MODIFICATION 3: Repeat the previous program 10 times and each time get new values in input for B and C.
–This corresponds to the pseudo code:
Set count to 0while (count < 10)
execute the programIncrement count
endwhile
35
IF within a WHILE
Simple example in C++: Modification
3
• The new C++ program:
36
//simple example: Modification 3#include <iostream>using namespace std;
int main(){ int B, C; int count=0; while (count <10) { cout << “Insert the value of B: “;
cin >> B;if (B < 0) B=B*(-1);cout << “Insert the value of C: “;cin >> C; if (C < 0) C=C*(-1);int A=B+C-7;cout << “the value of A is “ << A
<< endl;count ++;
}return 0;}
IF within a WHILE
Modification 4 of the simple example in C++
• MODIFICATION 4: Instead of repeating 10 times I want the user to decide if the execution of the program must be repeated or not. I will ask the user to insert a 0 to stop the program. Any other value will force the program to be re-executed.
–This corresponds to the pseudo code:
Set the answer to “yes”;While (answer is “yes”) repeat the program Ask the user if he/she wants to repeat
Get the answerendwhile
37
Simple example in C++: Modification
4
• The new C++ program:
38
//simple example: Modification 4#include <iostream>using namespace std;
int main(){ int B, C; char answer=‘y’; while ((answer == ‘y’) || (answer ==‘Y’)) { cout << “Insert the value of B: “;
cin >> B;if (B < 0) B=B*(-1);cout << “Insert the value of C: “;cin >> C; if (C < 0) C=C*(-1);int A=B+C-7;cout << “the value of A is “ << A
<< endl;cout << “Do you want to repeat this
program? (y/n) “ << endl;cin >> answer;
}return 0;}
A new problem in C++
• Problem
– Read in a sequence of non-negative numbers, one number at a time, and compute a running sum.
– When you encounter the first negative number print out the sum of all the non-negative values received and stop.
39
This Exercise is for you to complete!
The pseudocode
40
41
Subprogram Statements
We can give a section of code a name and use that name as a statement in another part of the program
When the name is encountered, the processing in the other part of the program halts while the named code is executed
It is implemented by using functions.
Remember?
Simple example in C++: Modification
5
• Break the problem in smaller parts – use subprograms
• The new C++ program:
42
//simple example: Modification 5#include <iostream>using namespace std;
int check(int x); // function for subprogram{if (x < 0) x=x*(-1);}
int main(){ int B, C; char answer=‘y’; while ((answer == ‘y’) || (answer ==‘Y’)) { cout << “Insert the value of B: “;
cin >> B;B=check(B);cout << “Insert the value of C: “;cin >> C;C=check(C); int A=B+C-7;cout << “the value of A is “ << A
<< endl;cout << “Do you want to repeat this
program? (y/n) “ << endl;cin >> answer;
}return 0;}
Functions