Chapter 1
Introduction
to
Computer Programming
Programming Logic for Business
Copyright 2001 Laura Saret
Laura Saret EdD
Chapter Outline
 Introduction: The Computer Revolution
 The Mystery of Computers
 How Does Data Become Information?
 Why Do We Organize Data?
 How Are Data Organized?
 What Is Hardware?
 What Is Software?
 What Are the Various Programming Languages?
 How do Procedure-Oriented, Visual,
Object-Oriented, and Internet Languages Differ?
 What is the Programming Process?
Chapter Objectives
After completing this chapter you should be able to.…
 Explain how the computer revolution has changed
how people live and work.
 Describe what a computer program and
programming logic are.
 Explain how data become information.
 Understand why and how data are organized and
give an example to describe the relationship
among databases, files, records, fields, characters,
and bits.
 Describe the various types of fields and specify
which characters are allowed in numeric and
nonnumeric fields as well as indicate whether a
given field is numeric or nonnumeric.
Chapter Objectives (continued)
 Describe the major hardware components of a
computer system.
 Explain the differences between machine,
assembler, and high-level programming
languages.
 Explain why and how programs written in highlevel languages are translated into machine
language.
 Describe how procedure-oriented, visual,
object-oriented, and Internet languages differ.
 Explain the steps of the programming process.
Introduction: The Computer
Revolution
 Before the industrial revolution
in the 18th century
 Society based on agriculture
 Manufacturing existed mostly as a
cottage industry
 During the industrial revolution
 Machine manufacturing of products in
large factories
 Computer or information
revolution
 Computer is transforming how
people do business, communicate
with each, and live
 Future is unclear
Computer Revolution (continued)
 Computers are everywhere
 Internet
 Dependence of business on computers
 Routine tasks such as billing, payroll, communications
 Decision making
 Computerized manufacturing
 Many workers telecommute
 Work on computers from home making use of modems
and facsimile (fax) machines
 Changing job market
The Mystery of Computers
 Computer program
 Sequence of instructions that tells
the computer to do some
predetermined task(s)
 Can be purchased or written by
computer programmers
 Programming logic
 The design of a solution to a
programming problem
How Does Data Become
Information?
 Processing data without computers
 Manual data processing
 What is data processing?
 Input data
 Raw facts such as test scores, recipes
 Process data
 Calculating grades
 Changing recipe that feeds six into
recipe that feeds 12
 Output information
 Data that have been processed
and made useful
 Grade report showing average and
grade
 The recipe to be used for dinner
Data are processed to become
information
Input
Output
80
average and
grade:
85
85
90
B
test scores:
Why Do We Organize Data?- Businesses and other organizations maintain
data
 The government maintains data
 Individuals maintain data
AFTER WE STORE DATA, WE WANT TO
BE ABLE TO FIND AND USE THE
DATA
Questions and Issues When
Dealing With Data:
 Where should the data be stored?
 After being stored, how are data found
again?
 How should data be updated or
maintained?
 How should data be organized?
 How are privacy and security issues
handled?
 What happens if data are destroyed?
Databases
How are data
organized?
Files
Records
Fields
Characters (bytes)
Bits (binary digits 0 and 1)
Example: fund raising database for a
children’s baseball league
 Database contains 1 file for each team in the league
 Each file contains 1 record for each player on the
team
Player’s name:
Greta Patel
Number of candy bars sold:
20
Money turned in to the league: Yes
Date money turned in:
7-12
 Each record contains 4 fields
 Field name (data name, variable or identifier) identifies
the field and provides a name to access the data
contained in the field
 Field (data) type describes the data in each field
 Each field is composed of characters
 Each of the characters is composed of bits
 Coding schemes, such as ASCII and EBCDIC, specify how
bits are combined to make characters
Field (data) type
 Describes the type of data contained in the field
 2 types we will be concerned about in this course
 Numeric
 Can only contain numbers, a decimal point, and a
positive or negative sign
 Examples include: 587
-326 1.248 +1
 Nonnumeric (alphanumeric)
 Can contain any character
 Examples include: $1,000.00 abcdEFG 56%
 Only numeric fields can be used for arithmetic
Player’s name:
Greta Patel
Number of candy bars sold:
20
Money turned in to the league: Yes
Date money turned in:
7-12
nonnumeric
numeric
nonnumeric
nonnumeric
Other field types
 Logical or boolean
 Money turned into the league
 “yes”/”no” or “true”/”false” value
 Character or string
 Any nonnumeric field
 Date
 Date money turned in
 Consists of a calendar date
 Integer
 15
-3
0
 Numeric field that only contains a whole number that is positive,
negative, or zero
 Real or floating point
 3.25
878.999
 Numeric fields with decimal points
 Can be single precision or double precision depending on the
magnitude and accuracy required
 Variant
 Can contain any type of data
What is hardware?
 Physical devices used to process data
 Computer system
 Interconnected devices capable of processing data, which have
been input, and supplying the resulting information as output
 Processing device is called central processing unit (CPU)
Input Devices
Examples:
Disk drives
CD-ROM drives
Tape drives
Keyboards
Mice
Processing Device:
Central Processing Unit
(CPU)
Control unit
Arithmetic logic unit
(ALU)
Primary Storage
Output Devices
Examples:
Printers
Video displays
Disk drives
Tape drives
Control unit
 “Brain” of the computer
 Determines which program instruction to execute
 Interprets the instruction
 Causes the instruction to be executed
Input Devices
Examples:
Disk drives
CD-ROM drives
Tape drives
Keyboards
Mice
Processing Device:
Central Processing Unit
(CPU)
Control unit
Arithmetic logic unit
(ALU)
Primary Storage
Output Devices
Examples:
Printers
Video displays
Disk drives
Tape drives
Arithmetic/logic unit (ALU)
 Does arithmetic computations
 Add 1 to Counter
 Does data transfers
 Move Name to output
 Counter = 0
 Does logical comparisons
 Is Name = “Smith”?
Input Devices
Examples:
Disk drives
CD-ROM drives
Tape drives
Keyboards
Mice
Processing Device:
Central Processing Unit
(CPU)
Control unit
Arithmetic logic unit
(ALU)
Primary Storage
Output Devices
Examples:
Printers
Video displays
Disk drives
Tape drives
Primary (main) storage
 Stores data and instructions (programs) needed by the computer
 Storage is also called memory
 Types of memory
 RAM (random-access memory)
 Memory size of a computer--storage capacity of its RAM
 ROM (read-only memory)
Input Devices
Examples:
Disk drives
CD-ROM drives
Tape drives
Keyboards
Mice
Processing Device:
Central Processing Unit
(CPU)
Control unit
Arithmetic logic unit
(ALU)
Primary Storage
Output Devices
Examples:
Printers
Video displays
Disk drives
Tape drives
Other memory is used
 Why?
 Main memory is expensive
 Main memory is not permanent
 You never have enough main memory!
 Secondary storage
 Used as additional memory for the computer
 Less expensive than main memory
 Not as fast as main memory
 Other names for secondary storage
 External storage
 Auxiliary storage
 Examples of secondary storage




Magnetic disks
Tapes
CDs
ZIP disks
Input/output (I/O) devices
 Peripherals
 Hardware used to get data and instructions from secondary
to primary storage and vice versa
 Examples







Disk drives
CD drives
Tape drives
Keyboards
Mice
Printers
Video displays
 Programmers write instructions for
 Input
 Output
What is software?
 Computer programs
 To execute the instructions of a computer program, the
instructions must be in primary storage
 System software
 Example: Operating system
 Needed to make the computer “usable”
 Communicates with hardware to accomplish input and output
functions
 Windows 2000
 Application software
 Programs that carry out business functions
 Firmware
 Programs that are neither hardware nor software
Computer programs are written in
programming languages
 Statements or instructions
 Syntax rules
 Languages
 Machine languages (1st generation)
 Machine dependent
 Binary
 Consist of an operation code (instruction) and operands
(the data used by the instruction)
0100010101
operation code
1001010101000010001
operand
Programming languages (continued)
 Assembly language (2nd generation)
 Machine dependent
 Use mnemonics
SUB
W
Symbolic operation code
Symbolic operand
for subtraction
 Must be translated into machine language
 Assembler program
 Each instruction generates one machine-language
instruction
 Calculations done in a register in the ALU
 The following example adds the numbers 17 and 41, stores
the result in SUM, and prints the result
CLR
LD 17
ADD 41
STR SUM
PRT SUM
Programming languages (continued)
 3rd generation languages
 Machine independent
 COBOL, FORTRAN, C, C++, BASIC, Visual BASIC
 More English-like
 ADD 17, 41 GIVING SUM
 SUM = 17 + 41
 SUM = 17 + 41;
 Must be translated into machine language
 Compilers
 Interpreters
 Assemblers translate each instruction into 1
machine-language instruction
 Compilers and interpreters translate each instruction
into one or more machine-language instructions
Compilers vs. interpreters
List of diagnostics--
Source
program-program written in
a programming
language such as
C++
Compiler
Program
translation (syntax or
language) errors
Object program--machine
language program that can be
saved for future executions
Source
program-program written in
a programming
language such as
C++
List of diagnostics-Interpreter
Program
translation (syntax or
language) errors
Programming languages (continued)
 Fourth-generation languages (4GLs)
 High-level languages that require much less effort than
3rd-generation languages to use
 Typically used to retrieve information from files and
databases
 Query languages (e.g., SQL) and report generators
 Sample SQL instruction to select student names
SELECT NAME FROM STUDENTS WHERE CLASS = “CIS”
Another way to look at
languages
 Procedure-oriented
 Event-driven, visual
 Object-oriented
 Internet
Procedure-oriented languages
 Emphasis is on how to accomplish a task
 Programmer writes instructions AND determines
order in which the computer should process the
instructions
 Examples:
 FORTRAN
 COBOL
 BASIC
C
Sample COBOL program
IDENTIFICATION DIVISION.
PROGRAM-ID. SALES AVERAGES.
AUTHOR.
LAURA SARET.
DATE-COMPILED.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. HP-3000.
OBJECT-COMPUTER.
HP-3000.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SALES-FILE ASSIGN TO “SALESIN”.
SELECT PRINT-FILE ASSIGN TO “PRINT,UR,A,LP(CCTL)”.
(CONTINUED ON NEXT SLIDE)
DATA DIVISION.
FILE SECTION.
FD
SALES-FILE
LABEL RECORDS ARE STANDARD
DATA RECORD IS SALES-REC.
01
SALES-RECORD.
02 SALES1
PIC 9(5)V99.
02 SALES2
PIC 9(5)V99.
02 SALES3
PIC 9(5)V99.
02 SALES4
PIC 9(5)V99.
FD
PRINT-FILE
LABEL RECORDS ARE STANDARD
DATA RECORD IS REPORT-LINE.
01
REPORT-LINE
PIC X(80).
(continued)
WORKING-STORAGE SECTION.
01
DONE
PIC X VALUE “N”.
01
TOTAL
PIC 9(6)V99.
01
AVERAGE
PIC 9(5)V99.
HEADING-LINE.
02 FILLER
PIC X(34) VALUE SPACES.
02 FILLER
PIC X(12) VALUE “SALES REPORT”.
02 FILLER
PIC X(34) VALUE SPACES.
DETAIL-LINE
02 FILLER
PIC X(35) VALUE SPACES.
02 DETAIL-LINE-AVERAGE PIC ZZ,ZZ9.99.
02 FILLER
PIC X(36).
Sample
COBOL
program
(CONTINUED ON NEXT SLIDE)
Sample COBOL program (continued)
PROCEDURE-DIVISION
*
MAIN.
OPEN INPUT SALES-FILE, OUTPUT PRINT-FILE.
PERFORM HEADINGS.
PERFORM READ-INPUT.
PERFORM PROCESS UNTIL DONE IS EQUAL TO “Y”.
CLOSE SALES-FILE, PRINT-FILE.
STOP RUN
*
HEADINGS.
WRITE PRINT-LINE FROM HEADING-LINE AFTER ADVANCING TO TOP OF
PAGE.
MOVE SPACES TO PRINT-LINE.
WRITE PRINT-LINE AFTER ADVANCING 1 LINES.
*
READ-INPUT.
READ SALES-FILE INTO SALES-RECORD AT END MOVE “Y” TO DONE.
*
PROCESS.
ADD SALES-1, SALES-2, SALES-3, SALES-4 GIVING TOTAL.
DIVIDE TOTAL BY 4 GIVING AVERAGE ROUNDED.
MOVE AVERAGE TO DETAIL-LINE-AVERAGE
WRITE PRINT-LINE FROM DETAIL-LINE AFTER ADVANCING 1 LINES.
PERFORM READ-INPUT.
Sample Quick BASIC program
INPUT “ENTER SALES AMOUNT FOR FIRST QUARTER”, Sales1
DO WHILE Sales1 <>0
INPUT “ENTER SALES AMOUNT FOR SECOND QUARTER”, Sales2
INPUT “ENTER SALES AMOUNT FOR THIRD QUARTER”, Sales3
INPUT “ENTER SALES AMOUNT FOR FOURTH QUARTER”, Sales4
Average = (Sales1 + Sales2 + Sales3 + Sales4)/4
PRINT
PRINT “AVERAGE SALES = ”;Average
INPUT “ENTER SALES AMOUNT FOR FIRST QUARTER”, Sales1
LOOP
END
Sample C program
#include <stdio.h>
Main ()
{
float sales1, sales2, sales3, sales4, average;
printf(“Enter sales amount for first quarter:”);
scanf(“%f”,&sales1);
while(sales1 != 0);
{
printf(“Enter sales amount for second quarter:”);
scanf(“%f”,&sales2);
printf(“Enter sales amount for third quarter:”);
scanf(“%f”,&sales3);
printf(“Enter sales amount for fourth quarter:”);
scanf(“%f”,&sales4);
average = (sales1 + sales2 + sales3 + sales4)/4;
printf(“\n”);
printf(“Average score %5.1f\n”,average);
printf(“Enter sales amount for first quarter:”);
scanf(“%f”,&sales1);
}
}
Event-driven, visual languages
 Emphasis of a program is on the
 objects (e.g., buttons) included in the user interface
 events (e.g., mouse click) that occur when objects are used
 Gives the user control over the program
 Graphical user interface (GUI) of Windows
 Sample Visual Basic input screen
Sales Program
Calculate
First Quarter Sales:
Average
Second Quarter Sales:
Third Quarter Sales:
Fourth Quarter Sales:
Print
Average
Clear
Sales
Exit
Object-oriented languages
 Programmer creates objects and methods for working with the
objects
 Traditional vs. object-oriented programming
 Traditional--sales amounts would be stored in a sales file;
instructions for calculating and printing the average would be in a
program separate from the data
 Object-oriented--a sales object would be created containing both
the sales amounts and instructions (methods) for calculating and
printing the average
 Examples of languages




Smalltalk
C++
Visual Basic
Java
Internet languages
 Used to create web pages
 HTML (Hypertext Markup Language)
 Java
 Programmer can create applets
// Average sales program
import java.io.*;
public class Average {
public static void main( String args[] ) throws IOException
{
double sales1, sales2, sales3, sales4, average;
System.out.print(“Enter sales amount for first quarter: ”);
sales1 = System.in.read();
while(sales1 != 0) {
System.out.print(“Enter sales amount for second quarter: ”);
sales2 = System.in.read();
System.out.print(“Enter sales amount for third quarter: ”);
sales3 = System.in.read();
System.out.print(“Enter sales amount for fourth quarter: ”);
sales4 = System.in.read();
average = (sales1 + sales2 + sales3 + sales4)/4;
System.out.println (“Average is “ + average );
System.out.print(“Enter sales amount for first quarter: ”);
sales1 = System.in.read();
}
}
What is the programming
process?
 Understand program
specifications
 Plan the program’s logic
 Code the program
 Get the program into machinereadable form
 Translate the coded program into
machine language
 Test the program
 Document the program
Understand the program
specifications
 Provided by systems analyst
 Systems analyst works with the user
 Analyst studies the current system
 Collection of people, machines, and procedures that work
together to accomplish objectives
 Uses interviews, questionnaires, observing, studying
procedure manuals to gather data
 Program specifications include
 A statement of the problem
 Description of all inputs and outputs
 Details for processing
Plan the program’s logic
 How you will solve the problem
 Subroutine, function, or module
 Set of instructions or tasks that accomplish a specific
function of the program
 Printing headings
 Accessing a file
 Doing a complicated calculation
 Advantages
 Cost less and take less time to modify
 A subroutine written for one program can be
used in another program
 Several programmers can work on the same
program simultaneously
Code the program
 Write a source program for the solution in a
programming language
Get the program into machinereadable form
 Key the program into the computer’s memory
or onto a disk
Translate the program
 Use a compiler, interpreter, or assembler
 Find syntax (language) errors
Test the program
 Use sample data
 Find logic errors
 Debugging
Documentation
 Written commentary of the programming process
 Program specifications
 Flowcharts and other planning aids
 List of source program
 Comments in the program
 Sample test data
 Test results
Descargar

Chapter 1