chapter 8 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 8 PowerPoint Presentation
Download Presentation
Chapter 8

Loading in 2 Seconds...

play fullscreen
1 / 45

Chapter 8 - PowerPoint PPT Presentation

  • Uploaded on

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Chapter 8' - vanna

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 8

Chapter 8

Introduction to High-Level Language Programming


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
Objectives Postponed(Covered in CS23021)
  • Functions
  • Managing complexity
  • Object-oriented programming
  • Graphical programming
  • The big picture: Software engineering
where do we stand
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
high level languages
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
high level languages continued
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
Figure 8.1

Transitions of a High-level Language Program

introduction to c
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”
Figure 8.3

The Overall Form of a Typical C++ Program

data types
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
data types continued
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
Figure 8.5

Some of the C++ Standard Data Types

data types continued1
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

statement types
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
statement types continued
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
input output statements
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
input output statements continued
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
input output cont
Input/Output (cont.)
  • Consider real numbers such as 84.8232
    • The fixed format output of this number would be


    • In scientific notation, this output would be


    • The computer is free to choose the output style.
    • To specify fixed format output, include the following formatting statement in the program


    • To require the output be in scientific notation, use


    • To require two decimal places, use


output using literal strings
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.
the assignment statement
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;

assignment statements cont
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.
control statements
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
control statements continued
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)
control statements continued1
Control Statements (continued)
  • Conditional flow of control (continued)
    • if-else statement

if (Boolean condition)




    • if variation of the if-else statement

if (Boolean condition)


Figure 8.10

Conditional Flow of Control (flowchart)


Figure 8.11

If-Else with Empty Else

control statements continued2
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)


Figure 8.12

While Loop

putting the pieces together
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
meeting expectations
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
meeting expectations1
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
pseudocode language instructions in c sections 8 1 8 6
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


Second set of algorithmic operations

pseudocode language instructions cont
Pseudocode Language Instructions(cont.)
  • Iteration (looping):
    • While (a boolean condition is true) do





End of Loop

computation expressions in c
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

computation expressions in c1
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: +=, -=, *= , /=, %= ???

computation cont
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

Input and Output in C++



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
conditional statements in c
Conditional Statements in C++
  • Conditional flow of control (continued)
    • if-else statement

if (Boolean condition)




    • if variation of the if-else statement

if (Boolean condition)


Figure 8.12

Conditional Flow of Control


Figure 8.13

If-Else with Empty Else

iterative statement in c
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)


Figure 8.14

While Loop

interative example in c
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;

  • 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