Lecture 1 2
Lecture 1 2
PRINCIPLES OF PROGRAMMING
“The Box”
CRT Display CD-ROM Drive
Floppy
Disk
Drive
Keyboard
Mouse
SOFTWARE TYPES
• System Software
– Operating systems
– Communications
– Device drivers and utilities
– Performance management
• Business Software
– General business
– Applications by business/interest area
– Desktop publishing
– Project management
– Image processing
• Development Software
– Programming languages
– Computer-assisted software engineering
– Web site development software
PROGRAMMING MODELS
• programmers to develop programs and
their procedures:
– Procedural programming focuses on the
procedures that programmers create.
• procedural programmers focus on the actions that
are carried.
– Object-oriented programming focuses on
objects, or “things,” and describes their
features (also called attributes) and
behaviors.
PROGRAMMING LANGUAGE
• You write computer instructions in a computer
programming language such as Visual Basic, C++, C#,
or Java.
• Programmers write programs in different languages.
– Some programmers work exclusively in one language,
• others know several and use the one that is best suited to the
task at hand.
• The instructions you write using a programming language
are called program code.
• Every programming language has rules governing its
word usage and punctuation.
– These rules are called the language’s syntax.
– Mistakes in a language’s usage are syntax errors.
COMPUTER LANGUAGES
• Computer languages may be divided into
three general types:
B i n a r y
– Machine languages
– Assembly languages
– High-level languages
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.
7. Maintain the program.
1. Understanding the Problem
• Programmer
– Write programs to satisfy others needs
– Provide services to users
• First understand what users want
• Well designed program
– Steps might involve a short meeting
• Poorly designed program
– Steps might take many meeting hours
• Thoroughly understanding problem
– A most difficult aspect of programming
Envisioning the Objects
• Programmer
– Envisions objects needed
– Writes programming language instructions to
create objects
• Distinct steps
– Data modeling
• Identify all manipulated objects and relations
– Create class
• General object category
– Establish communication means
• Between objects, user
2. Planning the Logic
• Experienced programmers
– Combine planning and program coding in
one step
4. Translating the Program Code
• Maintenance
– Process of making required changes
• Reasons
– New legislated
– Input file format alterations
– End user requires additional information
• Not included in original output specifications
The program development cycle
Algorithm
• An algorithm is a set of rules or instructions for
doing a task or solving a problem.
1- Terminal symbol
indicates the beginning and the end of processing.
Some symbols used in flowcharts
2- I/O symbol
indicates that the computer is to obtain new
data or to record the results of computations.
parallelogram
Some symbols used in flowcharts
3- Decision symbol
represents a branch in the flow of a program
diamond
Some symbols used in flowcharts
4- Process symbol
contains instructions that are not represented
by other symbols. i.e. it contains calculations,
storage of data.
rectangle
Some symbols used in flowcharts
6- comment or annotation
symbol is used to annotate other entries.
dashed line indicates the position of the
comment.
Some symbols used in flowcharts
7- connector
is used to join areas of a flowchart.
Some symbols used in flowcharts
8- Striped symbol
is used to represent a predefined process (or series of
operations) when the flowcharts that define the process
are included with the current set of flowcharts.
Some symbols used in flowcharts
Start
Read InputNumber
Result = InputNumber * 2
Print Result
Stop
Figure shows symbols and the flow direction lines
Repeating Instructions
start
input myNumber
set myAnswer = myNumber * 2
output myAnswer
input myNumber
set myAnswer = myNumber * 2
output myAnswer
input myNumber
set myAnswer = myNumber * 2
output myAnswer
….
…..
…..
Stop
Repeating Instructions
infinite loop
Using a Sentinel Value to End a Program
Complex Flowchart
Figure shows the Flowchart for what will happen to the values of X and Y
as the program runs
X Y
1 1
2 2
1 3
2 4
1
2
Figure shows the Flowchart for what will happen to the values of
X and Y as the program runs
X Y Y
1 1
2 2 2
2 3 3
1 3
2 3
2 4 4
Exercises
Exercises: 1 Examine the following flowchart, fill in the table of values, and
answer the questions.
a. What numbers will be printed?
b. What values will X and Y have when the program ends?
Exercises
Exercises: 2- Examine the following flowchart, and fill in the table values.
What will the values of X, Y, and Z be when this program ends?
Computers, Memory, and
Input/Output
Computers can be viewed as consisting of three
types of components: input devices; output
devices; and the central processing unit, or
CPU.
Declaring and Using Variables
• All programming languages support two broad
data types;
– numeric describes data that consists of numbers
– string describes data that is nonnumeric.
• numeric constant is specific numeric value,
– such as 43, 12345800 within a program.
• string constant (literal string constant) is A specific text
value, or string of characters,
– such as “Amanda” , “ 1000$”
• Variables are named memory locations whose contents can vary
or differ over time.
Variables
Declaration
• In most programming languages, before
you can use any variable, you must
include a declaration for it.
– A declaration is a statement that provides a
data type and an identifier for a variable.
Example:
instruction NUMBER = 2
directs the computer to store the value 2 at the location
that it is calling NUMBER.
NUMBER 2 46.38 10.00
=2 NUMBER COST PROFIT
INSTRUCTION COMPUTER STORAGE
Assigning Values to Variables
Note:
Once data is stored in memory, it remains until the
instruction to read is executed again.
Place the EOF decision AFTER the corresponding READ.
Tests for EOF BEFORE executing the READ instruction.
PRINT
- To print a line on a report, we will use the instruction
PRINT.
- The PRINT instruction directs the computer to print information
stored at one or more of its memory locations.
PRINT
In addition to data stored in memory, PRINT instructions
may be used to print literals.
START
READ NAME, GROSS PAY
DO CALCULATE FED W/H LOOP WHILE NOT EOF
IF GROSS PAY > 400
FED W/H = GROSS PAY X 25 %
ELSE
FED W/H = GROSS PAY X 17 %
END IF
PRINT NAME, FED W/H
READ NAME, GROSS PAY
END DO
STOP
Performing Arithmetic Operations
Precedence and associativity of five common operators
Example:
answer = a + b + c * d / e – f
answer = 7 + 18/6 - 2 * 5
answer = a + b + ((c * d) / e) – f
Sequence
A sequence is represented by listing a
series of instructions at the same margin
STOTAL = 0
GTOTAL = 0
READ NAME, SALES
Routine Components in Programs
I. Loop
Loop is a circular logical structure.
It causes a series of steps to be continuously
repeated until a decision directs program flow to
some other part of the program.
Loops are diagrammed in either of two ways:
1. By an upward flow direction line.
2. By a connector that returns program flow to a
previous point on the chart.
I. Loop
LOOPS
Loops are represented by statements that indicate
the name of the loop, the type of loop (while or
until), and the loop exit decision.
Start
Read Name, Gross Pay
DO Calc LOOP WHILE NOT EOF
Read Name, Gross Pay
Fed Tax= Gross Pay X 17%
SS Tax= Gross Pay X 6%
Print Name, Fed Tax, SS Tax
END DO
Nested loops
LOOPS
IF COMMISSION > 0
ADD COMMISSION TO TOTAL
ADD BONUS TO TOTAL
END IF
lf-Then-Else
IF SALES > 0
SKIP
ELSE
PRINT "ERROR IN DATA"
ADD 1 TO ERROR RECORDS
END IF
Nested If-Then-Elses
Routine Components in Programs
II. COUNTER
count of the number of times a
process within a loop has been
completed.
A counter consists of two processes that
manipulate a variable:
- initializes the variable
- The second adds/subtract a constant to
(or increments/decrement) the variable
each time the loop is completed.
Example of flowchart contains a counter
The flowchart illustrates the error of initializing the
variable within the loop itself.
The flowchart prints a report of records in a file called
"SALES FILE."
Routine Components in Programs
III. An accumulator
It is a structure that maintains a current total
without affecting ongoing calculations or
processing.
- Accumulators are similar to counters in that
both consist of a process that sets a variable
equal to an initial value and another process
that, increments that variable.
- Accumulators differ from counters in that they
are incremented by variable amounts rather than
by constants.
The flowchart represents a simple program that contains an
accumulator called TOTAL. In this flowchart, the profit for each
item is accumulated in TOTAL. When EOF is reached, this total is
printed.
Flowchart (uses both a counter and an accumulator)
represents a program that will read a series of sales and
calculate and print their average.
Routine Components in Programs
IV. indicators (Switches or Flags)
Indicators, often called switches or flags, are
used to record some event or circumstance
within a program.