Chapter 8 Introduction to HighLevel Language Programming Objectives
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 the C++ Language l Will focus on the C language subset of C++ Ø Virtual data storage Ø Statement types Ø Putting the pieces together
Objectives Postponed (Covered in CS 23021) Only the introduction to the C portion of C++ in sections 8. 1 - 8. 6 (inclusive) will be covered. The topics postponed are: Ø Functions Ø Managing complexity Ø Object-oriented programming Ø Graphical programming Ø The big picture: Software engineering
Where Do We Stand? Ø Early days of computing l Programmers used assembly language • Programs written by technically oriented people l Assembler programs • Were machine specific • Required programmers take a microscopic view of a task Ø Later decades l 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
High-level Languages Ø High-level programming languages l Called third-generation languages • Machine language was “first generation” • Assembly language was “second generation” l l Overcame deficiencies of assembly language Programmer didn’t need to manage details of data storage or movement
High-level Languages (continued) Ø Expectations of a high-level language program (continued) l l l 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
Figure 8. 1 Transitions of a High-level Language Program
Introduction to C++ Ø Some components of program on next slide l Comments • Anything following “//” on a line is a comment • Give information to human readers of code l The “include” directive • • l 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” l 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 l l 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++ l l Also called reserved words. Can not be used as an identifier Ø C++ is a case-sensitive, free-format language l Doesn’t matter where things are put on a line. Ø Data items can be constants or variables
Data Types (continued) Ø A declaration of a data item tells l Whether the item is a constant or a variable l The identifier used to name the item l The data type of the item
Figure 8. 5 Some of the C++ Standard Data Types
Data Types (continued) Ø An array l l 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
Statement Types Ø Input/output statements l Input statement • Collects a specific value from the user for a variable within the program l Output statement • Writes a message or the value of a program variable to the user’s screen or to a file
Statement Types (continued) Ø Assignment statement l Assigns a value to a program variable Ø Control statement l Directs the flow of control • Can cause it to deviate from usual sequential flow
Input/Output Statements Ø Example l Pseudocode Get value for Radius (interactively from keyboard) l C++ cin >> Radius; Ø cin: Input stream - - from user’s keyboard Ø Code for extraction operator (>>) and the definition of the cin stream come from the iostream library and std namespace
Input/Output Statements (continued) Ø Example l Pseudocode Print the value of Circumference (on screen) l C++ cout << Circumference; Ø cout: Output stream -- set to user’s screen Ø 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 84. 8232 l l l The fixed format output of this number would be 84. 8232 In scientific notation, this output would be 8. 48232 e+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)
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 circumference that corresponds “ << “to this radius is “ << Circumference Ø Many other output features are available l l Some additional information is given in textbook We won’t cover the output options in detail here.
The Assignment Statement General form Ø l Pseudocode Set the value of “variable” to “arithmetic expression” l C++ assignment statement evaluation q q variable = expression; Expression on the right is evaluated The result is written into the memory location specified on the left Example: Consider assembly code for Ø A= B + C - 3; l Knowledge of assembly language helps you understand how calculation is done in C++.
Assignment Statements (cont. ) Ø The C++ symbols for four basic operations l Ø Data Type Considerations: l l Ø + Addition - Subtraction * Multiplication / Division Example: The product of A and B is A*B. 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.
Control Statements Ø Types of control mechanisms l Sequential • Instructions are executed in order l Conditional • Choice of which instructions to execute next depends on some condition l Looping • Group of instructions may be executed many times
Control Statements (continued) Ø Sequential is the default mode of execution Ø Conditional flow of control l l 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)
Control Statements (continued) Ø Conditional flow of control (continued) l if-else statement if (Boolean condition) S 1; else S 2; l if variation of the if-else statement if (Boolean condition) S 1;
Figure 8. 10 Conditional Flow of Control (flowchart) (If-Else)
Figure 8. 11 If-Else with Empty Else
Control Statements (continued) Ø Looping (iteration) l l The loop body may be executed repeatedly based on the value of the Boolean condition while statement while (Boolean condition) S 1;
Figure 8. 12 While Loop
Putting the Pieces Together Ø At this point, we can l Perform input and output l Assign values to variables l Direct the flow of control using conditional statements or looping Ø For a complete program, we need to l Assemble the statements in the correct order l Fill in the missing pieces
Meeting Expectations Ø C++ meets the four expectations for a high- level programming language Ø Expectations l 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) l l l 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
Pseudocode Language Instructions in C++ (Sections 8. 1 – 8. 6) Recall pseudocode language instructions (pg 58) Ø Computation: l Ø Input/Output: l l l Ø Set value of “variable” to “arithemetic expression” Get a value for “variable” Print the message “message” Conditional: l If “a boolean expression” is true then First set of algorithmic operations Else Second set of algorithmic operations
Pseudocode Language Instructions (cont. ) Ø Iteration (looping): l While (a boolean condition is true) do operation. . . operation End of Loop
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 Operator Precedence Associativity () 1 na +, - , ++, -- , ! ( unary ) 2 L->R (+a, -a, ++a, --a, !a) R->L (a++, a--) *, /, % 3 L->R +, - 4 L->R <, >, <=, >= 5 L->R ==, != 6 L->R && 7 L->R || 8 L->R = 9 R->L
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 – 4 ac) ) / 2 a Ø Using a high level language we could write: Ø Ø l discrim = b*b – 4*a*c; l root 1 = (-b + sqrt(discrim)) / (2*a); l root 2 = (-b – sqrt(discrim)) / (2*a); This closely resembles the way we look at the problem mathematically
Input and Output in C++ /* circle 03. cpp Gets the radius of a circle, calculates it’s circumference and prints out the result */ i/o example #3 ·cout – prompt user for data ·<< (insertion operator) ·cin – store data in a variable #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; ·>> (extraction operator) circumference = 2 * PI * radius; ·cout – output data entered cout << “The circumference is: “ ; cout << circumference << “. n”; return 0; }
Conditional Statements in C++ Ø Conditional flow of control (continued) l if-else statement if (Boolean condition) S 1; else S 2; l if variation of the if-else statement if (Boolean condition) S 1;
Figure 8. 12 Conditional Flow of Control (If-Else)
Figure 8. 13 If-Else with Empty Else
Iterative Statement in C++ Ø Looping (iteration) l l The loop body may be executed repeatedly based on the value of the Boolean condition while statement while (Boolean condition) S 1;
Figure 8. 14 While Loop
Pseudocode Language Instructions in C++ Ø All of the pseudocode constructs (see pg 58) can be implemented in C++ Ø As a result, we can code the pseudocode version of any of our algorithms in C++ and execute them.
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;
Summary Ø In a high-level language, the programmer: l Need not manage storage l Can think about the problem at a higher level l l Can use more powerful and more naturallanguage-like program instructions Can write a much more portable program
Pop Test 5 What are the restrictions when declaring a variable 2. List two different data types 3. There are four expectations for a higher level programming language. Give two of these. 1.
- Slides: 47