CPS120 Introduction to Computer Programming

Download Report

Transcript CPS120 Introduction to Computer Programming

CPS120 Introduction to
Computer Programming
The Programming Process
The Program Development Cycle
Analyze the problem
Design the solution algorithm
Design the user interface
Write the code
Test and debug the program
Complete the documentation
Programs
• A program is a set of stepby-step instructions that
directs the computer to do
the tasks you want it to do
and produce the results
you want.
Programming Languages
• A programming
language is a set of
rules that provides a
way of telling a
computer what
operations to perform.
What Can a Program Do?
• A program can only instruct a computer to:
–
–
–
–
–
–
–
Read Input
Sequence
Calculate
Store data
Compare and branch
Iterate or Loop
Write Output
Sequence Control Structures
• Sequence control structures direct the order
of program instructions.
• The fact that one instruction follows
another—in sequence—establishes the
control and order of operations.
Calculate
• A program can
instruct a computer
to perform
mathematical
operations.
Add 1 to
Counter
Store
• A program will often
instruct a computer to
store intermediate
results.
Place 1
in
Counter
Compare and Branch
• A program can instruct a computer to compare
two items and do something based on a match
or mismatch which, in turn, redirect the
sequence of programming instructions.
– There are two forms:
• IF-THEN
• IF-THEN-ELSE
IF-THEN
Entry
Test
condition p
Exit
false
true
True
statement a
IF-THEN-ELSE
Entry
Test
condition p
false
“false”
statement a
true
Exit
“true”
statement a
Iterate
• A program loop is a
form of iteration. A
computer can be
instructed to repeat
instructions under
certain conditions.
No
Iteration Control Structures
• Iteration control structures are looping
mechanisms.
• Loops repeat an activity until stopped. The
location of the stopping mechanism
determines how the loop will work:
– Leading decisions
– Trailing decisions
Leading Decisions
• If the stop is at the beginning of the
iteration, then the control is called a leading
decision.
• The command DO WHILE performs the
iteration and places the stop at the
beginning.
DO WHILE Loop
Entry
Exit
No
Test
condition p
Yes
Loop
statement a
Trailing Decisions
• If the stop is at the end of the iteration, the
control mechanism is called a trailing
decision.
• The command DO UNTIL performs the
iteration and puts the stop at the end of the
loop.
DO UNTIL Loop
Entry
Loop
statement a
Test
condition p
Exit
No
Yes
The Program Development Cycle
Analyze the problem
Design the solution algorithm
Design the user interface
Write the code
Test and debug the program
Complete the documentation
Testing the Algorithm
• The process itself must be tested
• Testing at the algorithm development phase
involves looking at each level of the topdown design
Testing the Algorithm
• Desk checking: sit at a desk with a pencil and
paper and work through the design
• Walk-through: Manual simulation of the design
by the team members
– Take sample data values and simulate the design using
the sample data
• Inspection: The design is handed out in advance,
and one person (not the designer) reads the design
line by line while the others point out errors