Title: Introduction to HighLevel Language Programming
1Chapter 8
- Introduction to High-Level Language Programming
2Objectives
- 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
3Objectives Postponed(Covered in CS23021)
- Functions
- Managing complexity
- Object-oriented programming
- Graphical programming
- The big picture Software engineering
4Where 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
5High-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 didnt need to manage details of data
storage or movement
6High-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
8Introduction 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
9Figure 8.2
10- Figure 8.3
- The Overall Form of a Typical C Program
11Data 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
12Data 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
14Data Types (continued)
- An array
- Groups together a collection of memory locations,
all storing data of the same type - Example
- Int Hits12
Figure 8.6 A 12-Element Array Hits
15Statement 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 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 (interactively from
keyboard) - 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
18Input/Output Statements (continued)
- Example
- Pseudocode
- Print the value of Circumference (on screen)
- 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
19Input/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.48232e001
- The computer is free to choose the output style.
- To specify fixed format output, include the
following formatting statement in the program - cout.setf(iosfixed)
- To require the output be in scientific notation,
use - cout.setf(iosscientific)
- To require two decimal places, use
- cout.precision(2)
20Output 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 ltlt The concumference that corresponds
- ltlt to this radius is ltlt Circumference
- Many other output features are available
- Some additional information is given in textbook
- We wont cover the output options in detail here.
21The 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
22Assignment Statements (cont.)
- The C symbols for four basic operations
- Addition
- - Subtraction
- Multiplication
- / Division
- Example The product of A B is AB.
- 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.
23Control 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
24Control 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)
25Control 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
28Control 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 30Putting 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
31Meeting 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
32Meeting 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
33Pseudocode 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
34Pseudocode Language Instructions (cont.)
- Iteration (looping)
- While (a boolean condition is true) do
- operation
- operation
- .....
- operation
- End of Loop
35Computation 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
36Computation 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
37Computation (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 bb 4ac
- root1 (-b sqrt(discrim)) / (2a)
- root2 (-b sqrt(discrim)) / (2a)
- This closely resembles the way we look at the
problemmathematically
38Input and Output in C
- /
- 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
39Conditional 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
42Iterative 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 44Interative Example in C
- Sum 0 //initialize Sum
- cout ltlt Please enter the numbers to add
- cout ltlt terminate with a negative number. ltlt
endl - cin gtgt Number // this will get the first
value - while (Number gt 0)
- Sum Sum Number
- cin gtgt Number
- cout ltlt The total is ltlt Sum ltlt endl
45Summary
- 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