Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Introduction to High-Level Language Programming.

Similar presentations


Presentation on theme: "Chapter 8 Introduction to High-Level Language Programming."— Presentation transcript:

1 Chapter 8 Introduction to High-Level Language Programming

2 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

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

4 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

5 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

6 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

7 Figure 8.1 Transitions of a High-level Language Program

8 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”

9 A Simple C++ Program Figure 8.2

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

11 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

12 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

13 Figure 8.5 Some of the C++ Standard Data Types

14 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

15 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

16 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

17 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

18 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

19 Input/Output (cont.)  Consider real numbers such as 84.8232 The fixed format output of this number would be The fixed format output of this number would be84.8232 In scientific notation, this output would be In scientific notation, this output would be8.48232e+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)

20 Output using literal strings  Suppose we want to output message before the value, such as The circumference that corresponds to this radius is 84.8234  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.

21 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;

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

23 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

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

25 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;

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

27 Figure 8.11 If-Else with Empty Else

28 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;

29 Figure 8.12 While Loop

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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

38 /*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 = 3.14159; const double PI = 3.14159; 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++

39 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;

40 Figure 8.12 Conditional Flow of Control (If-Else)

41 Figure 8.13 If-Else with Empty Else

42 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;

43 Figure 8.14 While Loop

44 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;

45 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


Download ppt "Chapter 8 Introduction to High-Level Language Programming."

Similar presentations


Ads by Google