Presentation is loading. Please wait.

Presentation is loading. Please wait.

High-Level Programming Languages: C++

Similar presentations


Presentation on theme: "High-Level Programming Languages: C++"— Presentation transcript:

1 High-Level Programming Languages: C++
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

3 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

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

6 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.

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 C++ Portability Figure Portability provided by standardized languages versus interpretation by Bytecode

9 Java Portability Figure 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

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

13 A simple C++ program

14 A C++ program template

15 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

16 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

17 Some C++ Standard Data Types
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 declaration int 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];

19 Other data types Strings Example:
A string is a sequence of characters Example: string s=“Intro to CS”; creates a string s that contains “Intro to CS”

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 C++ cin: input stream
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 C++ cout: output stream
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

24 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);

25 The Assignment Statement
General form Pseudocode Set the value of “variable” to “arithmetic expression” C++ variable = expression; Expression on the right is evaluated The result is written into the memory location named on the left

26 Selection Statements Default mode of execution: sequential
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)

27 Selection Statements Selection statements if-else statement
if (Boolean condition) S1; else S2; if variation of the if-else statement

28 Loops Looping (iteration)
The loop body may be executed repeatedly based on the value of the Boolean condition while statement while (Boolean condition) S1;

29 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

30 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: //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; } 30

31 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

32 Simple example in C++: Modification 1
The C++ program: //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; } 32

33 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

34 Simple example in C++: Modification 2
The new C++ program: //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 34

35 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 0 while (count < 10) execute the program Increment count endwhile IF within a WHILE 35

36 Simple example in C++: Modification 3
//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; The new C++ program: IF within a WHILE 36

37 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 answer endwhile 37

38 Simple example in C++: Modification 4
//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; The new C++ program: 38

39 This Exercise is for you to complete!
A new problem in C++ This Exercise is for you to complete! 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

40 The pseudocode

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?

42 Simple example in C++: Modification 5
//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; Break the problem in smaller parts – use subprograms The new C++ program: Functions 42


Download ppt "High-Level Programming Languages: C++"

Similar presentations


Ads by Google