Title: Chapter%208:%20Introduction%20to%20High-level%20Language%20Programming
1Chapter 8 Introduction to High-level Language
Programming
- Invitation to Computer Science,
- C Version, Third Edition
2Objectives
- In this chapter, you will learn about
- High-level languages
- Introduction to C
- Data types
- Statement types
- Putting the pieces together
3Objectives
- Managing complexity
- Object-oriented programming
- Graphical programming
- The big picture software engineering
4Where Do We Stand?
- Early days of computing
- Programmers were satisfied with assembly language
- Programs written by technically oriented people
- Later decades
- Programmers demanded a more comfortable
programming environment - Programs could be written by nontechie people
5High-level Languages
- High-level programming languages
- Called third-generation languages
- Overcame deficiencies of assembly language
- Programmer didnt need to manage details of data
storage or movement
6- Figure 8.1
- Transitions of a High-level Language Program
7Simple C Program
- / Get a number input from the user and print it
out / - include ltiostreamgt
- using stdcout
- using stdcin
- int main()
-
- // Prompt user, define a variable for num, input
a value. - cout ltlt "Please input an integer "
- int num
- cin gtgt num
- // Output result
- cout ltlt "The number you entered"
- cout ltlt " is " ltlt num ltlt "\n"
- return 0
-
8Overall Form of a Typical C Program
- prologue Comment (optional)
- include directives (optional)
- using directives (optional)
- functions (optional)
- main function declarations (optional) main
function body
9Introduction to C
- Some components of program in Figure 8.2
- 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
10Data Types
- 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
11Data 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
12Data Types (continued)
- Some of C standard data types
- int a positive or negative integer
- double a real number( double precision )
- float a real number ( single precision )
- char a character ( single keyboard character like
t
13Data Types(continued)
- Data Declarations
- double radius
- Allocates space for a type double data item
- Associates the name radius with this item
- Associates a set of valid operations with this
item - double circumference 34.5
- Same as above, plus?
- Gives it an initial value (initializes it)
- See sample programs
14Data Types(continued)
- Data Declarations
- double radius, circumference, area
- Declares three separate data items of type
double - const double PI 3.1416
- Allocates space for type double data item
- Associates the name PI with this item
- Associates a set of valid operations with this
item - Initializes the item with the value 3.1416
- Cannot be changed. It is a constant.
15Statement 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 users screen or to a file
16Statement 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
17Input/Output Statements
- Example
- Pseudocode
- Get value for Radius
- C
- cin gtgt Radius
- cin input stream
- Code for extraction operator (gtgt) and the
definition of the cin stream come from the
iostream library and std namespace
18- /
- circle01.cpp
- Gets the radius of a circle
- /
- include ltiostreamgt
- using stdcin
- using stdcout
- int main()
- double radius 0.0
- cout ltlt "Please enter the radius " ltlt '\n'
- cin gtgt radius
- return 0
-
- i/o example 1
- cout prompt user for data
- ltlt (insertion operator)
- cin store data in a variable
- gtgt (extraction operator)
19Input/Output Statements (continued)
- Example
- Pseudocode
- Print the value of Circumference
- C
- cout ltlt Circumference
- cout output stream
- Code for the insertion operator (ltlt) and the
definition of the cout stream come from the
iostream library and std namespace
20- /
- circle02.cpp
- Gets the radius of a circle
- /
- include ltiostreamgt
- using stdcin
- using stdcout
- int main()
- double radius 0.0
- cout ltlt "Please enter the radius " ltlt '\n'
- cin gtgt radius
- cout ltlt The radius you entered is ltlt radius
- i/o example 2
- cout prompt user for data
- ltlt (insertion operator)
- cin store data in a variable
- gtgt (extraction operator)
- cout output data entered
21The 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
22Expressions 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-gtR (a, -a, a, --a, !a)R-gtL (a, a--)
, /, 3 L-gtR
, - 4 L-gtR
lt, gt, lt, gt 5 L-gtR
, ! 6 L-gtR
7 L-gtR
8 L-gtR
9 R-gtL
23Expressions 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
24- /
- circle03.cpp
- Gets the radius of a circle,
calculatesits circumference and prints out the
result - /
- include ltiostreamgt
- using stdcin
- using stdcout
- int main()
- const double PI 3.14159
- double radius 0.0, circumference 0.0
- cout ltlt "Please enter the radius " ltlt '\n'
- cin gtgt radius
-
- circumference 2 PI radius
- cout ltlt The circumference is
- i/o example 3
- cout prompt user for data
- ltlt (insertion operator)
- cin store data in a variable
- gtgt (extraction operator)
- cout output data entered
25Control 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
26Control Statements (continued)
- Default mode of execution sequential
- Conditional flow of control
- 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)
27Control 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
28- Figure 8.12
- Conditional Flow of Control
- (If-Else)
29- Figure 8.13
- If-Else with Empty Else
30(No Transcript)
31Control 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
32 33Data Types Revisited
- How to declare a data item which is a group of
integers, doubles, chars - int Hits12 // web page hits
- Allocates space for group of 12 integers
- Associates the name Hits with this group of
integers - Associates a set of valid operations with Hits
- Each integer in Hits has its own name
- The first integer is Hits0 the last is Hits11
34Data Types Revisited
- An array
- Groups together a collection of memory locations,
all storing data of the same type
Figure 8.6 A 12-Element Array Hits
35Data Types Revisited(continued)
- Declaring a group of arrays ( i.e. a table )
- double WaterReadings 2 3
- Allocates space for two arrays of three real
numbers - 6 total numbers
- Associates the name WaterReadings with this
table - Associates set of valid operations with this
table - Each element in WaterReadings has its own name
- The number at row 1, column 2 is
WaterReadings12 - The number at row 0, column 0 is
WaterReadings00
36Data Types Revisited(continued)
- Initializing an array
- Int Hits12 3,6,7,22,25,12,34, 9, 3,17,15,2
- Int Hits 3,6,7,22,25,12,34, 9, 3,17,15,2
- Same as above
- Int Hits12 3,6,7,22
- Initializes first 4 as specified
- Last 12 get 0
- double WaterReadings 23 14.3, 15.2,
16.4, 13.9, 14.2, 12.7 - int myValue23 0, 0 // ?
37Intro to C
- Lets look at some programs using arrays
38Putting 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
39High-level Languages Revisited
- Expectations of a high-level language program
- Programmer can take a macroscopic view of tasks
primitive operations can be larger - Programs will be portable
- Code will be closer to standard English and use
standard mathematical notation
40High-level Languages Revisited
- Consider equation ax2 bx c 0
- The roots are x ( b ? sqrt(b2 4ac) ) / 2a
- Using a high level language we could write
- discrim bb 4ac
- root1 (-b sqrt(discrim)) / (2a)
- root2 (-b sqrt(discrim)) / (2a)
- This closely resembles the way we look at the
problemmathematically
41Virtual Data Storage
- Programs dealing with circles might deal with
- Circle as geometric shape
- Circular garden, pool, tile
- Properties of circle
- Circumference, radius, diameter, area
- Relationships among properties
- Circumference ? ? d ? ? 2 ? r
- Area ? ? r2
42Virtual Data Storage (continued)
- Possible Data Items For Circle Programs
- Radius, Circumference, Diameter, Area and PI
- To deal with actual problems what kind of data
items should these be? - Numbers with fractional parts ? double (in C)
- PI never changes ? const (in C)
43Virtual Data Storage (continued)
- What if we want to deal with a group of things
that are all the same kind? - For Example a list numbers
- Monthly sales figures ( Jan thru Dec )
- Midterm exam grades ( 35 of them )
- Electoral college votes by state ( 50 of them )
- Website hits per month ( Jan thru Dec )
- We can declare a group of like data items.
44Virtual Data Storage (continued)
- How to represent a table of the same kind of data
items?
Site 1 Site 2 Site 3
FinalReading 14.3 15.2 16.4
InitialReading 13.9 14.2 12.7
45Virtual Data Storage (continued)
Assume these array declarations in C
int Hits1 12 int Hits2 12 0 int
Hits3 12 0,1,2,3 int Hits4 12
0,1,2,3,4,5,6,7,8,9,10,11 double
WaterReadings1 23 double WaterReadings2
23 0 double WaterReadings3 23
14.3,15.2,16.4 double WaterReadings4
23 14.3,15.2,16.4,13.9,14.2,12.7
46Virtual Data Storage (continued)
- Hits1 - elements 0 - 3
- -1219546216 134521764 -1073757288
134514065 - Hits2 - elements 0 - 11
- 0 0 0 0 0 0 0 0
0 0 0 0 - Hits3 - elements 0 - 11
- 0 1 2 3 0 0 0 0
0 0 0 0 - Hits4 - elements 0 - 11
- 0 1 2 3 4 5 6 7
8 9 10 11
47Virtual Data Storage (continued)
- WaterReadings1
- 3.76654e-313 -5.74686e-42 -5.74962e-42
- -5.64181e-42 -3.24569e-42 4.94066e-324
- WaterReadings2
- 0 0 0
- 0 0 0
- WaterReadings3
- 14.3 15.2 16.4
- 0 0 0
- WaterReadings4
- 14.3 15.2 16.4
- 13.9 14.2 12.7
48Meeting Expectations
- C meets the four expectations for a high-level
programming language - Expectations
- Programmer need not manage details of the
movement of data items within memory, nor pay any
attention to where they are stored
49Meeting Expectations (continued)
- 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
50Managing Complexity Divide and Conquer
- Divide and conquer
- To solve a problem, divide it into smaller pieces
- In a computer program
- Divide the code into modules (subprograms), each
doing a part of the overall task - Empower these modules to work together to solve
the original problem
51Figure 8.20 A More Detailed Structure Chart
- Figure 8.19
- A Structure Chart
52Using Functions
- Function
- A module of code in C
- Named using ordinary C identifiers
- Subtask functions optional
- The main function mandatory
53Using Functions (continued)
- To invoke a subtask function, the main function
gives - Name of the function
- Argument list for the function
- Argument list list of identifiers for variables
that concern that function - Any function can have its own constant and
variable declarations
54Writing Functions
- A function header consists of
- Return indicator classifies a function as a void
or a nonvoid function - Function identifier
- Parameter list
- By default, arguments in C are passed by value
55- Figure 8.24
- The Outline for a C Function
56- Figure 8.29
- Some C Terminology
57Object-Oriented Programming
- Object-oriented programming (OOP)
- A program is a simulation of some part of the
world that is the domain of interest - Each object is an example drawn from a class of
similar objects - Key elements of OOP
- Encapsulation
- A class consists of its subtask modules and its
properties - Both are encapsulated in the class
58Object-Oriented Programming (continued)
- Key elements of OOP (continued)
- Inheritance
- Once a class A of objects is defined, a class B
of objects can be defined as a subclass of A - Polymorphism
- One name, the name of the service to be
performed, has several meanings, depending on the
class of the object providing the service
59What Have We Gained?
- Two major advantages of OOP
- Software reuse
- A more natural world view
60Graphical Programming Graphics Primitives
- Bitmapped display
- The screen is made up of thousands of pixels,
laid out in a two-dimensional grid - Frame buffer
- Memory that stores the actual screen image
- The terminal hardware displays on the screen the
frame buffer value of every individual pixel
61- Figure 8.34
- Pixel Numbering System in a Bitmapped Display
62Graphics Primitives (continued)
- Graphics library
- Software containing a collection of functions
that control the setting and clearing of pixels - Virtually all modern programming languages come
with a graphics library
63The Big Picture Software Engineering
- Software life cycle
- Overall sequence of steps needed to complete a
large-scale software project - Implementation represents a relatively small part
of the cycle
64- Figure 8.37
- Steps in the Software Development Life Cycle
65Scaling Up
- Programs written by students
- No longer than a few hundred lines
- Real-world programs
- 2, 3, or 4 orders of magnitude larger
- Large-scale software development
- Extensive planning and design needed
- A team of programmers needed
- Software engineering
66The Software Life Cycle
- Each step in the software development life cycle
- Has a specific purpose and activities
- Should result in a written document
- The feasibility study
- Problem specification
- Program design
67The Software Life Cycle (continued)
- Algorithm selection or development, and analysis
- Coding
- Debugging
- Testing, verification, and benchmarking
- Documentation
- Maintenance
68Modern Environments
- Integrated Development Environment (IDE) speeds
development by providing - A text editor
- A file manager
- A compiler
- A linker and loader
- Tools for debugging
69Summary
- 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-li
ke program instructions - Can write a much more portable program
70Summary
- C is an object-oriented, high-level programming
language - if-else statement creates a conditional flow of
control - while loop can be used for iteration
- Software life cycle overall sequence of steps
needed to complete a large-scale software project