High-Level Programming Languages: C - PowerPoint PPT Presentation

About This Presentation
Title:

High-Level Programming Languages: C

Description:

Chapter 9 High-Level Programming Languages: C++ – PowerPoint PPT presentation

Number of Views:134
Avg rating:3.0/5.0
Slides: 43
Provided by: Jone1168
Category:

less

Transcript and Presenter's Notes

Title: High-Level Programming Languages: C


1
Chapter 9
  • High-Level Programming Languages C

2
Chapter Goals
  • Describe the translation process and distinguish
    between assembly, compilation, interpretation,
    and execution
  • Name four distinct programming paradigms and name
    a language characteristic of each
  • Define the concepts of a data type and strong
    typing
  • Describe the structure of a C program

3
Chapter Goals
  • Construct valid numeric and string expressions
  • Construct input and output expressions
  • Implement selection statements in C
  • Implement loop structures in C
  • Implement void and value-return functions
  • Define subprogram statements and their use
  • Use of nested logic

4
Compilers
  • High-level languages provide a richer set of
    instructions that makes the programmers life
    even easier
  • C, Java, Lisp, Prolog are high-level languages
  • Compiler A program that translates a high-level
    language program into machine code
  • C is a compiled language

5
Compilers
Figure 8.1 Compilation process
6
Interpreters
  • Interpreter An interpreter translates a statement
    and then immediately executes the statement
  • Lisp and Prolog are interpreted languages
  • Java is first compiled in an intermediate
    language (called Bytecode) and the Bytecode is
    interpreted.

7
Java
  • Introduced in 1996 and swept the computing
    community by storm
  • Java is compiled into a standard machine language
    called Bytecode
  • A software interpreter called the JVM (Java
    Virtual Machine) takes the Bytecode program and
    executes it
  • Portability was of primary importance

8
C Portability
Figure 8.2 Portability provided by standardized
languages versus interpretation by Bytecode
9
Java Portability
Figure 8.2 Portability provided by standardized
languages versus interpretation by Bytecode
10
Programming Language Paradigms
  • What is a paradigm?
  • A set of assumptions, concepts, values, and
    practices that constitute a way of viewing reality

11
Programming Language Paradigms
  • Imperative or procedural model
  • FORTRAN, COBOL, BASIC, C, Pascal, Ada
  • Functional model
  • LISP, Scheme (a derivative of LISP), and ML

12
Programming Language Paradigms
  • Logic programming
  • PROLOG
  • Object-oriented paradigm
  • SIMULA and Smalltalk
  • C is as an imperative language with some
    object-oriented features
  • Java is an object-oriented language with some
    imperative features

13
A simple C program
14
A C program template
15
Introduction to C
  • Some components of a C template program
  • 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

16
Virtual Data Storage
  • 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

17
Some C Standard Data Types
  • 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
  • Example of a variable integer declaration
  • int numb

18
Other Data Types
  • An array
  • Groups together a collection of memory locations,
    all storing data of the same type
  • Example
  • int Hits12

19
Other data types
  • Strings
  • A string is a sequence of characters
  • Example
  • string sIntro to CS
  • creates a string s that contains Intro to CS

20
Statement 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

21
Control Statements
  • Types of control mechanisms
  • Sequential
  • Instructions are executed in order
  • Selection or Conditional
  • Choice of which instructions to execute next
    depends on some condition
  • Looping
  • Group of instructions may be executed many times

22
Input 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

23
Output Statements
  • 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

24
Input and Output
  • Cin reads from the keyboard and cout writes on
    the screen
  • Cin skips the whitespace
  • Function get reads one character from an input
    stream
  • Stores the character read in a variable of type
    char, the single argument the function takes
  • cin.get(character)

25
The 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

26
Selection Statements
  • Default mode of execution sequential
  • Selection statement
  • 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)

27
Selection Statements
  • Selection statements
  • if-else statement
  • if (Boolean condition)
  • S1
  • else
  • S2
  • if variation of the if-else statement
  • if (Boolean condition)
  • S1

28
Loops
  • Looping (iteration)
  • The loop body may be executed repeatedly based on
    the value of the Boolean condition
  • while statement
  • while (Boolean condition)
  • S1

29
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

30
Simple example in C
  • Problem Write a program that evaluates the
    arithmetic expression A B C 7. Assign an
    arbitrary value to B and C inside the program.
    Print A.
  • This corresponds to the pseudo code instruction
  • Set A to B plus C minus 7
  • Print A
  • The C program

//simple example include ltiostreamgt using
namespace std int main() int B10 int
C5 int ABC-7 cout ltlt the value of A is
ltlt A ltlt endl return 0
31
Modification 1 of the simple example in C
  • MODIFICATION 1 Now I want to ask the user to
    provide for me the values of B, and C. Then I
    want to output the value of A.
  • This corresponds to the pseudo code
  • Get the value of B and C
  • Print the value of A
  • cin and cout are the commands for getting data in
    input from the keyboard and printing a data in
    output on the screen
  • Note that in computer numbers are converted in
    characters in order to be printed since the
    screen displays ASCII codes!

31
32
Simple example in C Modification 1
  • The C program

//simple example Modification 1 include
ltiostreamgt using namespace std int
main() int B, C cout ltlt Insert the value of
B cin gtgt B cout ltlt Insert the value of C
cin gtgt C int ABC-7 cout ltlt the value
of A is ltlt A ltlt endl return 0
33
Modification 2 of the simple example in C
  • MODIFICATION 2 The second change consists in
    checking if B and C are positive (negative values
    are not accepted). If B and C are not positive,
    force them to be positive (i.e. If B receive -5
    turn B into 5). Then set ABC-7
  • This corresponds to the pseudo code
  • If (B lt 0) then
  • Set B to B(-1)
  • If (C lt 0) then
  • Set C to C(-1)
  • Set A to BC-7

33
34
Simple example in C Modification 2
  • The new C program

//simple example Modification 2 include
ltiostreamgt using namespace std int
main() int B, C cout ltlt Insert the value of
B cin gtgt B if (B lt 0)
BB(-1) cout ltlt Insert the value of C
cin gtgt C if (C lt 0) CC(-1) int
ABC-7 cout ltlt the value of A is ltlt A ltlt
endl return 0
Use of IF
35
Modification 3 of the simple example in C
  • Nested Logic
  • MODIFICATION 3 Repeat the previous program 10
    times and each time get new values in input for B
    and C.
  • This corresponds to the pseudo code
  • Set count to 0
  • while (count lt 10)
  • execute the program
  • Increment count
  • endwhile

IF within a WHILE
35
36
Simple example in C Modification 3
//simple example Modification 3 include
ltiostreamgt using namespace std int main()
int B, C int count0 while (count
lt10) cout ltlt Insert the value of B
cin gtgt B if (B lt 0) BB(-1) cout
ltlt Insert the value of C cin gtgt C if (C
lt 0) CC(-1) int ABC-7 cout ltlt the
value of A is ltlt A ltlt endl count
return 0
  • The new C program

IF within a WHILE
37
Modification 4 of the simple example in C
  • MODIFICATION 4 Instead of repeating 10 times I
    want the user to decide if the execution of the
    program must be repeated or not. I will ask the
    user to insert a 0 to stop the program. Any other
    value will force the program to be re-executed.
  • This corresponds to the pseudo code
  • Set the answer to yes
  • While (answer is yes)
  • repeat the program
  • Ask the user if he/she wants to repeat
  • Get the answer
  • endwhile

37
38
Simple example in C Modification 4
//simple example Modification 4 include
ltiostreamgt using namespace std int main()
int B, C char answery while
((answer y) (answer Y))
cout ltlt Insert the value of B cin gtgt
B if (B lt 0) BB(-1) cout ltlt Insert
the value of C cin gtgt C if (C lt 0)
CC(-1) int ABC-7 cout ltlt the value of A
is ltlt A ltlt endl cout ltlt Do you want to
repeat this program? (y/n) ltlt endl cin gtgt
answer return 0
  • The new C program

39
A new problem in C
This Exercise is for you to complete!
  • Problem
  • Read in a sequence of non-negative numbers, one
    number at a time, and compute a running sum.
  • When you encounter the first negative number
    print out the sum of all the non-negative values
    received and stop.

39
40
The pseudocode
41
Subprogram Statements
  • We can give a section of code a name and use that
    name as a statement in another part of the
    program
  • When the name is encountered, the processing in
    the other part of the program halts while the
    named code is executed
  • It is implemented by using functions.

Remember?
42
Simple example in C Modification 5
//simple example Modification 5 include
ltiostreamgt using namespace std int check(int
x) // function for subprogram if (x lt 0)
xx(-1) int main() int B, C
char answery while ((answer y)
(answer Y)) cout ltlt Insert
the value of B cin gtgt B Bcheck(B) cout
ltlt Insert the value of C cin gtgt
C Ccheck(C) int ABC-7 cout ltlt the
value of A is ltlt A ltlt endl cout ltlt Do you
want to repeat this program? (y/n) ltlt
endl cin gtgt answer return 0
  • Break the problem in smaller parts use
    subprograms
  • The new C program

Functions
Write a Comment
User Comments (0)
About PowerShow.com