Programs and Programming Languages
Programs and Programming Languages
3
Programs and Programming
Languages
1
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Programs and Programming
Languages
• A program is a set of instructions that the
computer follows to perform a task
• A programming language is a special
language used to write computer
programs.
• We start with an algorithm, which is a set
of well-defined steps.
2
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Example Algorithm for Calculating
Gross Pay
3
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Machine Language
• Although the previous algorithm defines
the steps for calculating the gross pay, it is
not ready to be executed on the computer.
• The computer only executes machine
language instructions
• Machine language instructions are binary
numbers, such as :
1011010000000101
4
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Machine Language
• Each different type of CPU has its own
machine language.
• Rather than writing programs in machine
language, programmers use programming
languages. Then use special software to
convert their programs to machine
language.
5
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Programs and Programming
Languages
• Types of languages:
6
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Some Well-Known Programming
Languages (Table 1-1 on Page 10)
C++
BASIC Ruby
FORTRAN
Java
Visual Basic
COBOL
C#
JavaScript
C Python
7
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
C++ is one of the most popular
programming Languages
• C++ is popular not only because of its
mixture of low- and high-level features, but
also because of its portability.
• This programming Language usually
requires the program to be recompiled on
each type of system, but the program itself
may need little or no change.
8
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
From a High-Level Program to an
Executable File
1) Create file containing the program with a text
editor.
2) Run preprocessor to convert source file
directives to source code program statements.
3) Run compiler to convert source program into
machine instructions.
4) Run linker to connect hardware-specific code to
machine instructions, producing an executable
file.
• Steps 2– 4 are often performed by a single
command or button click.
• Errors detected at any step will prevent
execution of following steps.
9
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
From a High-Level Program to an
Executable File
10
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Integrated Development
Environments (IDEs)
• An integrated development environment,
or IDE, combine all the tools needed to
write, compile, and debug a program into a
single software application.
• Examples are Microsoft Visual C++, Turbo
C++ Explorer, CodeWarrior, DevC++,
Block Code etc.
11
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Integrated Development
Environments (IDEs)
12
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
1.4
What is a Program Made of?
13
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
What is a Program Made of?
• Common elements in programming
languages:
– Key Words
– Programmer-Defined Identifiers
– Operators
– Punctuation
– Syntax
14
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Program 1-1
15
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Key Words
• Also known as reserved words
• Have a special meaning in C++
• Can not be used for any other purpose
16
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Key Words
17
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Programmer-Defined Identifiers
• Names made up by the programmer
• Not part of the C++ language
• Used to represent various things: variables
(memory locations), functions, etc.
• In Program 1-1: hours, rate, and pay.
18
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Operators
• Used to perform operations on data
• Many types of operators:
– Arithmetic - ex: +,-,*,/
– Assignment – ex: =
19
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Operators
20
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Punctuation
• Characters that mark the end of a
statement, or that separate items in a list
• In Program 1-1: , and ;
21
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Punctuation
22
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Syntax
• The rules of grammar that must be
followed when writing a program
• Controls the use of key words, operators,
programmer-defined symbols, and
punctuation
23
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Variables
• A variable is a named storage location in
the computer’s memory for holding a piece
of data.
• In Program 1-1 we used three variables:
– The hours variable was used to hold the
hours worked
– The rate variable was used to hold the pay
rate
– The pay variable was used to hold the gross
pay
24
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Variable Definitions
• There are two general types of data: numbers
and characters.
• Numeric data can be categorized as integers,
real, or floating-point numbers . etc
• When creating a variable in a C++ program, you
must know what type of data the program will be
storing in it.
• To create a variable in a program you must write
a variable definition (also called a variable
declaration)
25
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Variable Definitions
• There are many different types of data, a variable
holds a specific type of data.
26
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
1.5
Input, Processing, and Output
27
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Input, Processing, and Output
Three steps that a program typically
performs:
1) Gather input data:
• from keyboard
• from files on disk drives
2) Process the input data
3) Display the results as output:
• send it to the screen
• write to a file
28
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Input, Processing, and Output
In Program1-1 :
1) Gather input data:
cin >> hours;
cin >> rate;
2) Process the input data
pay = hours * rate;
1) Display the results as output:
cout << "How many hours did you work? ";
cout << "How much do you get paid per hour? ";
cout << "You have earned $" << pay << endl;
29
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
1.6
The Programming Process
30
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
The Programming Process
31
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
What Is Software Engineering ?
• Software engineering encompasses the whole
process of crafting computer software. It includes :
– Designing
– Writing
– Testing
– Debugging
– Documenting
– Modifying
– Maintaining complex software development
projects.
32
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
What Is Software Engineering ?
Software engineers use a number of tools in
their craft. Here are a few examples:
• Program specifications
• Charts and diagrams of screen output
• Hierarchy charts and flowcharts
• Pseudocode
• Examples of expected input and desired output
• Special software designed for testing programs
33
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
1.7
Procedural and Object-Oriented
Programming
34
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Procedural and Object-Oriented
Programming
• Procedural programming and object-
oriented programming are two ways of
thinking about software development and
program design.
• C++ is a language that can be used for two
methods of writing computer
programs:procedural programming
andobject-oriented programming.
35
Copyright © 2012 Pearson Education, Inc. Updated and Recorded by Vo Minh Thanh
Procedural Programming