1 / 45

Chapter 8

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

vanna
Download Presentation

Chapter 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • Programs written by technically oriented people • Assembler programs • Were machine specific • Required programmers take a microscopic view of a task • Later decades • Programmers demanded a more comfortable programming environment • Programs could be written by “nontechie” people • Programs could be portable rather than machine specific • Programmers could avoid data storage and movement issues

  5. High-level Languages • High-level programming languages • Called third-generation languages • Machine language was “first generation” • Assembly language was “second generation” • Overcame deficiencies of assembly language • 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 • Program will be portable • 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 • Anything following “//” on a line is a comment • Give information to human readers of code • The “include” directive • The linker includes object code from a library • C++ does not provide input or output of data (I/O) • The “using” directive • Tells compiler to look in a namespace for definitions not mentioned in the program • Used here for I/O commands “cin” & “cout” • 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. • Should select names suggestive of meaning • Keywords: Have special meanings in C++ • Also called reserved words. • 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 • The identifier used to name 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 • Example Int Hits[12] Figure 8.6 A 12-Element Array Hits

  15. Statement Types • Input/output statements • 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

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

  17. Input/Output Statements • Example • Pseudocode Get value for Radius (interactively from keyboard) • 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 Print the value of Circumference (on screen) • 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 84.8232 • In scientific notation, this output would be 8.48232e+001 • The computer is free to choose the output style. • To specify fixed format output, include the following formatting statement in the program cout.setf(ios::fixed); • To require the output be in scientific notation, use cout.setf(ios::scientific); • To require two decimal places, use cout.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 • Many other output features are available • Some additional information is given in textbook • We won’t cover the output options in detail here.

  21. The Assignment Statement • General form • Pseudocode Set the value of “variable” to “arithmetic expression” • C++ assignment statement evaluation variable = expression; • Expression on the right is evaluated • 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. • Data Type Considerations: • 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 • Other data-type considerations are covered in textbook, and will be used, as needed.

  23. Control Statements • Types of control mechanisms • Sequential • Instructions are executed in order • Conditional • Choice of which instructions to execute next depends on some condition • Looping • Group 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) • 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 (Boolean condition) S1; else S2; • 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 • 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 • 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

  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

  32. Meeting Expectations • Expectations (continued) • 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 • Programming statements in a high-level language • Will be closer to standard English • Will 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” • Input/Output: • Get a value for “variable” • Print the value for “variable” • Print the message “message” • Conditional: • If “a boolean expression” is true then First set of algorithmic operations Else Second set of algorithmic operations

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

  35. Computation: Expressions in C++ 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++ Assume b = 5, c = 3, d = 4, e = 2 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: +=, -=, *= , /=, %= ???

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

  38. Input and Output in C++ /* circle03.cpp Gets the radius of a circle, calculatesit’s circumference and prints out the result */ #include <iostream> using std::cin; using std::cout; int main() { const double PI = 3.14159; double radius = 0.0, circumference = 0.0; cout << "Please enter the radius " << '\n'; cin >> radius; circumference = 2 * PI * radius; cout << “The circumference is: “ ; cout << circumference << “.\n”; 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

  39. Conditional Statements in C++ • Conditional flow of control (continued) • if-else statement if (Boolean condition) S1; else S2; • 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 • 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; } cout << “The total is “ << Sum << endl;

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

More Related