Вы находитесь на странице: 1из 96

Cs126:

PROGRAMMING 1
INTRODUCTION : Overview
of Computers and Logic

Mapua - SOIT Gloren Sison-Fuentes, CoE-MIT


Understanding Computer
Components and Operations

Electronic Data Processing Concepts


 
IPO Diagram:

 
Input Processing Output
 

2
Understanding Computer
Components and Operations
• Hardware: equipment, or devices,
associated with a computer
• For a computer to be useful, it needs more
than equipment; a computer needs to be
given instructions
• The instructions that tell the computer what
to do are called software, or programs, and
are written by programmers
• Hardware devices that perform input
include keyboards and mice

3
Understanding Computer
Components and Operations
(continued)
• Through input devices,
– data, or facts, enter the computer system
• Processing data items may involve
– organizing them,
– checking them for accuracy, or
– performing mathematical operations on them

4
Understanding Computer
Components and Operations
(continued)
• The hardware that performs these sorts
of tasks is the central processing unit,
or CPU
• After data items have been processed,
the resulting information is sent to a
printer, monitor, or some other output
device so people can view, interpret,
and use the results

5
Understanding Computer
Components and Operations
(continued)
A digital computer is a machine (also called hardware) that is
capable of storing data and executing algorithms. It is made up of
the following physical components
 
1. Central Processing Unit (CPU)
2. Memory
2.1 Primary Memory
Read Only Memory (ROM)
Random Access Memory (RAM)
2.2 Secondary Memory
Includes magnetic, hard disk, floppy diskette, ZIP disk, CD-ROM
3. Input/Output Devices (I/O)
3.1 Input devices include keyboard, mouse, touchpad etc.
3.2 Output devices include display screen, printer, plotter

6
Understanding Computer
Components and Operations
(continued)
Software
 
In loose terms, software refer to computer programs
which serve as interface between the user and the
hardware. Software can be categorized into two,
namely:
 
1. System Software
- including operating system, compilers, linkers etc.
2. Application Software
- including word processor, spreadsheet, calculator etc.

7
Understanding Computer
Components and Operations
(continued)
What is a computer program?
  A computer program is very much like an
algorithm. However, it may not be finite,
i.e., a program need not terminate after a
finite number of steps. An example of such
a program is an operating system.
 Basically, a computer program is made up of
two components:
1. Data
2. Instructions for manipulating data
8
Understanding Computer
Components and Operations
(continued)
A computer program is written using a
certain computer programming language.
 
A programming language describes the:
1. vocabulary (set of symbols and words)
2. syntax (rules)
3. semantics (meaning)
that are used to write a valid computer
program.

9
Understanding Computer
Components and Operations
(continued)
• You write computer instructions in a
computer programming language, such as
Visual Basic, Pascal, COBOL, RPG, C#, C++,
Java, or Fortran
• Every language has rules governing its word
usage and punctuation
• Programming rules are called the language’s
syntax
• Each programming language uses a piece of
software to translate the specific
programming language into the computer’s
on-off circuitry, or machine language 10
Understanding Computer
Components and Operations
(continued)
Types of Computer Programming Languages
1. Low-Level Languages
Low-level languages include:
machine language (lowest level, defined in terms of zero's and
one's)
- assembly language (uses mnemonics)
 
>They are called low-level because they require the
programmers to have a good understanding of the underlying
hardware for which the programs are intended to run
>Programs written using low-level languages are not portable,
i.e. they cannot be executed in two different and incompatible
processors

11
Understanding Computer
Components and Operations
(continued)
Types of Computer Programming
Languages
1. Low-Level Languages
• To calculate wages = rates * hours in
machine language:
100100 010001 //Load
100110 010010 //Multiply
100010 010011 //Store
12
Understanding Computer
Components and Operations
(continued)
Types of Computer Programming Languages
1. Low-Level Languages
• Assembly language instructions are mnemonic
• Assembler: translates a program written in
assembly language into machine language

13
Understanding Computer
Components and Operations
(continued)
• Using assembly language instructions,
wages = rates • hours can be written
as:

LOAD rate
MULT hour
STOR wages

14
Understanding Computer
Components and Operations
(continued)
Types of Computer Programming Languages
2. High-Level Languages
High-level languages include:
- FORTRAN - ALGOL
- COBOL - PL/1
- BASIC - Pascal
- C++ - Java
 
>They are called high-level because (1) the language is quasi-
English and (2) it does not require the programmer to know
the actual computer hardware
>The programs written in high-level languages are able to exhibit
portability
15
Understanding Computer
Components and Operations
(continued)
• The language translation software, known as a
compiler or interpreter, tells you if you have
used a programming language incorrectly
• For a program to work properly, you must
give the computer exact instructions in a
specific sequence
• By doing this, you are developing the logic of
the computer program
• Once instructions have been inputted to the
computer and translated into machine
language, a program can be run, or executed
16
Understanding Computer
Components and Operations
(continued)
• Besides input, processing, and output,
all computer systems need and have:
– Internal storage, commonly called memory,
main memory, or primary memory. Though
needed to run programs, internal memory is
volatile—that is, its contents are lost every
time the computer loses power
– External storage, or permanent storage
outside the main memory of the machine, is
held on a device such as a floppy disk, hard
disk, or magnetic tape
17
Understanding the Programming
Process
• The programmer’s job can be broken
down into six programming steps:
1. Understand the problem
2. Plan the logic
3. Code the program
4. Translate the program into machine
language
5. Test the program
6. Put the program into production

18
Understand The Problem
• Really understanding the problem may
be one of the most difficult aspects of
programming
– The description of what the user needs may
be vague
– The user may not even really know what he
or she wants
– Users who think they know what they want
frequently change their minds after seeing
sample output
• A good programmer is often part
counselor, part detective
19
Plan the Logic
• Programmer plans the steps to the
program, deciding what steps to include
and how to order them
• The two most common tools are
flowcharts and pseudocode
• Both tools involve writing the steps of
the program in English

20
Code the Problem
• Some very experienced programmers can
successfully combine the logic planning
and the actual instruction writing, or
coding of the program, in one step

• A good term paper needs planning before


writing, and so do most programs

21
Translate the Program into
Machine Language
• Languages such as Java or Visual Basic translate
the programmer’s English-like high-level
programming language into the low-level
machine language that the computer understands

• If you write a programming language statement


incorrectly (for example, by misspelling a word,
using a word that doesn’t exist in the language,
or using “illegal” grammar), the translator
program doesn’t know what to do and issues an
error message identifying a syntax error

22
Translate the Program into
Machine Language (continued)
• All syntax errors are caught by the
compiler or interpreter
• When writing a program, a programmer
might need to recompile the code several
times
• An executable program is created only
when the code is free of syntax errors
• When you run an executable program, it
might also typically require input data

23
Creating an Executable Program

24
Test the Program
• A program that is free of syntax errors
is not necessarily free of logical errors

• Once a program is free from syntax


errors, the programmer can test it—that
is, execute it with some sample data to
see whether the results are logically
correct
25
Put the Program into Production
• Putting a program into production might mean
simply running the program once, if it was
written to satisfy a user’s request for a special
list
• The process might take months if the program
will be run on a regular basis, or it is one of a
large system of programs being developed
• Conversion, the entire set of actions an
organization must take to switch over to using
a new program or set of programs, can
sometimes take months or years to accomplish

26
Understanding the Data Hierarchy
• When data is stored for use on computer
systems, it is often stored in a data hierarchy,
where the smallest usable unit of data is the
character

• Characters are letters, numbers, and special


symbols, such as “A”, “7”, and “$”

• A field is a single data item, such as


lastName, streetAddress, or
annualSalary
27
Understanding the
Data Hierarchy (continued)
• Records are groups of fields that go
together for some logical reason
• Files are groups of records that go together
for some logical reason
• A database holds a group of files, often
called tables, that together serve the
information needs of an organization
• Database software establishes and
maintains relationships between fields in
these tables, so that users can write
questions called queries
28
The Language of a Computer
• Digital signals: sequences of 0s and 1s
• Machine language: language of a
computer
• Binary digit (bit):
– The digit 0 or 1
• Binary code:
– A sequence of 0s and 1s
• Byte:
– A sequence of eight bits

29
The Language of a Computer
(cont’d.)

30
The Language of a Computer
(cont'd.)
• ASCII (American Standard Code for
Information Interchange)
– 128 characters
– A is encoded as 1000001 (66th character)
– 3 is encoded as 0110011

31
The Language of a Computer
(cont'd.)
• EBCDIC
– Extended Binary Coded Decimal Interchange
Code
– Used by IBM (mainframe, midrange comp)
– 256 characters
• Unicode
– 65536 characters
– Two bytes are needed to store a character

32
Programming with the Problem
Analysis–Coding–Execution Cycle
• Programming is a process of problem
solving
• One problem-solving technique:
– Analyze the problem
– Outline the problem requirements
– Design steps (algorithm) to solve the problem
• Algorithm:
– Step-by-step problem-solving process
– Solution achieved in finite amount of time

33
Using Flowchart Symbols and
Pseudocode Statements
• Flowcharts (pictorial representations) and
pseudocode (English-like representations)
are used by programmers to plan the
logical steps for solving a programming
problem
• Some professional programmers prefer
writing pseudocode to drawing flowcharts,
because using pseudocode is more similar
to writing final statements in programming
language

Programming Logic and


Design, Third Edition 34
Comprehensive
Using Flowchart Symbols and
Pseudocode Statements
(continued)
• Almost every program involves the
steps of input, processing, and output,
necessitating some graphical way to
separate them
• Arithmetic operation statements are
examples of processing in a flowchart,
where you use a rectangle as the
processing symbol containing a
processing statement
Programming Logic and
Design, Third Edition 35
Comprehensive
Using Flowchart Symbols and Pseudocode
Statements (continued)

Programming Logic and Design, Third Edition


36
Comprehensive
Using Flowchart Symbols and
Pseudocode Statements (continued)

• To represent an output statement, you


use the parallelogram, which is also the
same symbol used for input statements

Programming Logic and Design, Third Edition


37
Comprehensive
Using Flowchart Symbols and
Pseudocode Statements (continued)
• In flowcharts:
– Arrows, or flowlines, connect and show the
appropriate sequence of steps
– A terminal symbol, or start/stop symbol, should
be included at each end
– Often, “start” or “begin” is used as the first
terminal symbol and “end” or “stop” is used in
the other
– The standard terminal symbol is shaped like a
racetrack; often called a lozenge, because it
resembles the shape of a medicated candy
lozenge you might use to soothe a sore throat

Programming Logic and


Design, Third Edition 38
Comprehensive
Using Flowchart Symbols and
Pseudocode Statements (continued)
• Figure 1-7 shows a complete flowchart for the
program that doubles a number, and the
pseudocode for the same problem

Programming Logic and Design, Third Edition


39
Comprehensive
Using and Naming Variables
• Variables are memory locations, whose
contents can vary or differ over time
• Sometimes, inputNumber can hold a 2
and calculatedAnswer will hold a 4; at
other times, inputNumber can hold a 6
and calculatedAnswer will hold a 12
• A variable name is also called an
identifier

Programming Logic and


Design, Third Edition 40
Comprehensive
Using and Naming Variables
(continued)
• Variable names used here follow only two
rules:
1. Must be one word

2. Have some appropriate meaning

• Table 1-1 on page 18 of the text lists some


possible variable names that might be used to
hold an employee’s last name and provides a
rationale for the appropriateness of each one

Programming Logic and


Design, Third Edition 41
Comprehensive
Ending a Program By Using Sentinel
Values
• An infinite loop is a repeating flow of logic with
no end
• To end the program, set a predetermined
value for inputNumber that means “Stop the
program!”
• The program can then test any incoming value
for inputNumber and, if it is a 0, stop the
program
• Testing a value is also called making a
decision
– Represented in flowchart by diamond shape called a
decision symbol
Programming Logic and
Design, Third Edition 42
Comprehensive
Ending a Program By Using
Sentinel Values (continued)
• A pre-selected value that stops the
execution of a program is often called a
dummy value since it does not represent
real data, but just a signal to stop

• Sometimes, such a value is called a


sentinel value because it represents an
entry or exit point, like a sentinel who
guards a fortress

Programming Logic and


Design, Third Edition 43
Comprehensive
Using the Connector
• By using just the input, processing, output,
decision, and terminal symbols, you can
represent the flowcharting logic for many
diverse applications
• When drawing a flowchart segment, you
might use only one other symbol, the
connector
• You can use a connector when limited page
size forces you to continue a flowchart in
an unconnected location or on another
page
Programming Logic and
Design, Third Edition 44
Comprehensive
Using a Connector
(continued)
• By convention, programmers use a
circle as an on-page connector
symbol, and a symbol that looks
like a square with a pointed bottom
as an off-page connector symbol

Programming Logic and


Design, Third Edition 45
Comprehensive
Using a Connector
(continued)
• If a flowchart has six processing steps and a page
provides room for only three, you might represent the
logic as shown below:

Programming Logic and Design, Third Edition


46
Comprehensive
Assigning Values to Variables
• When you create a flowchart or
pseudocode for a program that doubles
numbers, you can include the statement
compute caclulatedAnswer as
inputNumber times 2
• This statement incorporates two actions:
– First, the computer computes the arithmetic value
of inputNumber times 2
– Second, the computed value is stored in the
calculatedAnswer memory location

Programming Logic and


Design, Third Edition 47
Comprehensive
Assigning Values to Variables
(continued)
• Most programming languages allow a
shorthand expression for assignment
statements such as compute
caculatedAnswer as inputNumber times
2
• The shorthand takes the form
calculatedAnswer = inputNumber * 2
• The equal sign is the assignment operator,
which always requires the name of a
memory location on its left side—the
location where the result will be stored
Programming Logic and
Design, Third Edition 48
Comprehensive
Understanding Data Types
• Computers deal with two basic types of data
—character and numeric
• When you use a specific numeric value, such
as 43, within a program, you write it using
the digits and no quotation marks
• A specific numeric value is often called a
numeric constant because it does not change
—a 43 always has the value 43
• When you use a specific character value, or
string of characters, such as “Chris” you
enclose the string, or character constant,
within quotation marks
Programming Logic and
Design, Third Edition 49
Comprehensive
Understanding Data
Types (continued)
• Most computer languages allow at least
two distinct types of variables:
– One holds a number, often called a numeric
variable
– Others hold letters of the alphabet and various
special characters such as punctuation marks,
and are called character, text, or string
variables, depending on the language being
used

Programming Logic and


Design, Third Edition 50
Comprehensive
Understanding Data Types
(continued)
• Some languages allow for several types of
numeric data
• Languages such as Pascal, C++, C#, and
Java distinguish between integer (whole
number) numeric variables and floating-
point (fractional) numeric variables
containing a decimal point

Programming Logic and


Design, Third Edition 51
Comprehensive
Understanding the Evolution of
Programming Techniques

• Old programming languages required


programmers to work with memory
addresses and to memorize awkward
codes associated with machine
languages
• Newer programming languages look
much more like natural language and
are easier to use

Programming Logic and


Design, Third Edition 52
Comprehensive
Understanding the Evolution of
Programming Techniques (continued)

• Currently, there are two major


techniques used to develop programs
and their procedures
– Procedural programming focuses on the
procedures that programmers create
– Object-oriented programming, focuses on
objects, or “things”, and describes their
features, or attributes, and their behaviors

Programming Logic and


Design, Third Edition 53
Comprehensive
Unstructured

Spaghetti
The popular
Code
name for snarled
program
statements is
spaghetti code
• The reason for
the name should
be obvious—the
code is as
confusing to read
as following one
noodle through a
plate of
spaghetti

Programming Logic and Design, Third Edition Comprehensive 54


Three Basic
Structures
• A structure is a basic
unit of programming
logic; each structure is
a sequence, selection,
or loop
• The first of these
structures is a
sequence, as shown in
Figure 2-3
• With a sequence
structure, you perform
an action or event, and
then you perform the
next action, in order

Programming Logic and Design, Third Edition Comprehensive 55


Three Basic
Structures (continued)
• The second structure is called a
selection structure or decision
structure
– You ask a question
– depending on the answer,
• you take one of two courses of action
• no matter which path you follow, you
continue with the next event

Programming Logic and Design, Third Edition Comprehensive 56


Three Basic
Structures (continued)

Programming Logic and Design, Third Edition Comprehensive 57


Three Basic
Structures (continued)
• The selection structure is sometimes
called an if-then-else because it fits the
following statement:
– if someCondition is true then
do oneProcess
else
do theOtherProcess

Programming Logic and


Design, Third Edition 58
Comprehensive
Three Basic Structures
(continued)

Programming Logic and


Design, Third Edition 59
Comprehensive
Three Basic
Structures (continued)
• In a loop structure, you ask a question; if
the answer requires an action, you
perform the action and ask the original
question again
• If the answer requires that the action be
taken again, you take the action and then
ask the original question again
• Continues until the answer to the question
is such that the action is no longer
required; then you exit the structure
Programming Logic and
Design, Third Edition 60
Comprehensive
Three Basic
Structures (continued)
• You may hear programmers refer to
looping as repetition or iteration

Programming Logic and Design, Third Edition


61
Comprehensive
Three Basic Structures
(continued)

• All logic problems can be solved using


only these three structures—sequence,
selection, and looping
• The three structures can be combined in
an infinite number of ways
• Attaching structures end-to-end is
called stacking structures

Programming Logic and


Design, Third Edition 62
Comprehensive
Three Basic Structures
(continued)

Programming Logic and


Design, Third Edition 63
Comprehensive
Three Basic
Structures (continued)
• Placing a structure within another
structure is called nesting the structures

Programming Logic and Design, Third Edition


64
Comprehensive
Using the Priming Read
• A priming read or priming input is the first
read or data input statement in a program
• If a program will read 100 data records, you
read the first data record in a statement that
is separate from the other 99
• You must do this to keep the program
structured
• With a selection structure, the logic goes in
one of two directions after the question, and
then the flow comes back together; the
question is not asked a second time
Programming Logic and
Design, Third Edition 65
Comprehensive
Using the Priming
Read (continued)
• In a loop, if an answer results in the loop
being entered and loop statements
executing, then
– the logic returns to the question that started the
loop
• when the body of a loop executes, the
question that controls the loop is always
asked again

Programming Logic and


Design, Third Edition 66
Comprehensive
Using the Priming Read
(continued)

Programming Logic and


Design, Third Edition 67
Comprehensive
Using the Priming Read
(continued)

Programming Logic and


Design, Third Edition 68
Comprehensive
Using the Priming Read
(continued)

Programming Logic and


Design, Third Edition 69
Comprehensive
Using the Priming Read
(continued)

Programming Logic and


Design, Third Edition 70
Comprehensive
Using the Priming Read
(continued)

Programming Logic and


Design, Third Edition 71
Comprehensive
Using the Priming Read
(continued)

Programming Logic and


Design, Third Edition 72
Comprehensive
Using the Priming Read
(continued)

Programming Logic and


Design, Third Edition 73
Comprehensive
Understanding the
Reasons for Structure
• Until you have some programming
experience, it might be difficult to
appreciate the reasons for using only the
three structures—sequence, selection, and
loop
• However, staying with these three
structures is better for the following
reasons:
– Clarity– Professionalism
– Efficiency – Maintenance
– Modularity
Programming Logic and
Design, Third Edition 74
Comprehensive
Two Special Structures
—Case and Do Until
• Many programming languages allow two
more structures: the case structure and
the do until loop
• Never needed to solve any problem
though sometimes are convenient
• Programmers consider them both to be
acceptable, legal structures

Programming Logic and


Design, Third Edition 75
Comprehensive
The Case Structure
• Use the
case
structure
when there
are several
distinct
possible
values for
a single
variable
being
tested and
each value
requires
different
actions

Programming Logic and Design, Third Edition Comprehensive 76


The Case Structure
(continued)

Programming Logic and


Design, Third Edition 77
Comprehensive
The Do Until Loop
• In a do while loop, you ask a question
and, depending on the answer, you might
or might not enter the loop to execute the
loop’s procedure
• Conversely, in a do until loop, you ensure
that the procedure executes at least once;
then, depending on the answer to the
controlling question, the loop may or may
not execute additional times

Programming Logic and


Design, Third Edition 78
Comprehensive
The Do Until Loop (continued)
• Because programmers understand that a
do until can be expressed with a sequence
followed by a do while, most languages
allow the do until
• Again, you are never required to use a do
until; you can always accomplish the same
events with a sequence followed by a do
while

Programming Logic and


Design, Third Edition 79
Comprehensive
The Do Until Loop (continued)

Programming Logic and


Design, Third Edition 80
Comprehensive
The Do Until Loop (continued)

Programming Logic and


Design, Third Edition 81
Comprehensive
The Do Until Loop (continued)

Programming Logic and


Design, Third Edition 82
Comprehensive
The Do Until Loop (continued)
• An unstructured loop is neither a do while loop
(beginning with a decision and, after an action,
returning to the decision), nor a do until loop
(beginning with an action and ending with a
decision that might repeat the action)

Programming Logic and Design, Third Edition


83
Comprehensive
Sequence and Structured Loop that
Accomplish the Same Tasks as Figure 2-37

Programming Logic and


Design, Third Edition 84
Comprehensive
Sequential structure
#1] Input a temperature in Celsius and
output the corresponding temperature in
Fahrenheit. The formula is a follows:
Fahrenheit = 9 (Celsius) + 32
5
Flowchart
Start
Pseudocode
Variables Used:
F=0
F, C are numeric
Begin
Input C F=0
Display ” Input temperature in Celsius: ”
F = (9.0/5.0) * C + 32 Accept C
F = (9.0 / 5.0) * C + 32

Output F Display “Equivalent temperature in Fahrenheit:”, F


End

End
Selection/Conditional Structure
#1] Create a flowchart and a pseudocode
that will prompt the user to input two
number and output the highest number
entered.
Flowchart
Start

Input N1, N2

If Y
N1 > N2
Output N1 A

Output N2
A
End
Pseudocode
Variables Used:
N1, N2 are numeric
Begin
Display ” Input two numbers: ”
Accept N1, N2
if (N1 > N2) then
Display N1
else
Display N2
End
Iterative Program Structure
#1] Create a flowchart/pseudo code
that will generate the following number
series:

10, 5, 9, 10, 8, 15, 7, 20, 6, and 25


Flowchart
Start

X = 10
Y=5

Output X, Y

X=X–1
Y=Y+5

Y
If
X >= 6
N

End
Pseudo code
Variables Used:
X, Y are numeric
Begin
X = 10
Y=5
while (X >= 6) do
begin
Display X, Y
X=X–1
Y=Y+5
end
End
Iterative Program Structure
#5] Create a flowchart/pseudo code
that will input five numbers and output
how many of the numbers entered
are positive or negative.
Flowchart

A
Start

ctr = 1 ctr = ctr + 1


ctrp = 0
ctrn = 0
B Y
If
ctr <= 5
B
Accept num
N
Y Output ctrp, ctrn
If
num > 0 ctrp = ctrp + 1 A
N End

ctrn = ctrn + 1

A
Pseudo code
Variables Used:
num, ctr, ctrp, ctrn are numeric
Begin
ctr = 1
ctrp = 0
ctrn = 0
while (ctr <= 5) do
begin
Accept num
if (num > 0) then
ctrp = ctrp + 1
else
ctrn = ctrn + 1
ctr = ctr + 1
end
Display ctrp, ctrn
End
QUESTIONS???

Вам также может понравиться