12A-Creating Computer Programs
12A-Creating Computer Programs
Introduction
• Software provides the instructions that run
the hardware in a computer
• Without software the computer can not
function
• Some software commands may be built into
select pieces of hardware (like CPUs and ROM
chips), but even in these cases, the
programming is simply a “hard-coded” version
of software
• An example of such hard-coding can be found
in some device such as a digital watch
• Although you may not even think of it, some
kind of software is built into the watch’s
circuitry that makes it work
• The term “software” can be used generically,
as in the statement “Software tells the
computer what to do, and how to do it”
• The term can be used more specifically to
describe an operating system or application
• For example, you might say, “Windows 10 is a
piece of software,” or “PhotoShop is a piece of
software”
• On any computer, the software usually
consists of the operating system, various
utilities (sometimes considered part of the
operating system), and applications
What is a Computer Program
• A computer program is a set of instructions or
statements (also called code) to be carried out
by the computer’s CPU
• Computer programs, or software, come in
many forms:
– Operating systems
– Utilities
– Applications
• A program is typically composed of a main
module and sub-modules
• These modules are stored as a collection of
files
• Large programs can contain thousands of
individual files, each serving a specific purpose
• Some of the files contain instructions for the
computer, while the other files contain data
• For Windows-based PCs, some common
extensions for program files include the
following:
1) Executable Files
• An executable (.exe, .com) file is the part of a
program that actually sends commands to the
processor
• In fact, when you run a program, you are running the
executable file
• The processor executes the commands in the file –
thus the name executable file
• Executable files usually have the filename extension
.exe
2) Dynamic Link Library Files
• A dynamic link library (.dll) file is a partial .exe file
• A .dll file will not run on its own; instead, its
commands are accessed by another running program
• Because .dll files can contain parts of an executable
program, they provide the programmers with an
effective way of breaking large programs into small,
replaceable components
• This feature makes the entire program easier to
upgrade
• In addition, all .dll files can be shared by several programs at
one time
3) Initialization Files
• An initialization (.ini) file contains configuration
information, such as the size and starting point of a
window, the color of the background, the user’s name, and
so on
• Initialization files help programs start running or contain
information that programs can use as they run
• Although initialization files are still in use, many newer
programs store user preferences and other program
variables in the Windows Registry – a special database that
holds information about the computer’s user, installed
programs, and certain hardware devices
4) Help Files
• A help (.hlp, .chm) file contains information in an indexed
and cross-linked format
• By including a help file, programmers can provide the user
with online help information
5) Batch Files
• A batch (.bat) file automates common or repetitive tasks
• A batch file is a simple program that consists of an
unformatted text file containing one or more operating
system commands
• If you type a batch file’s name at a command prompt, your
operating system will execute the commands in the file
Hardware/Software Interaction
• Software is the reason people purchase
computers
• A program’s instructions are executed at the
hardware level, mainly on the CPU
• A highly trained individual called a computer
programmer creates this list of instructions
• The list of instructions is often called code
• The process of writing the list of instructions is
usually called coding
• Code
– The term “code” refers to the statements that are
written in any programming language, as in machine
code or higher-level code
– Computers understand only the binary number
system
– However, binary is too cryptic for humans to deal with
– This is why, computer programming languages are
created – to simplify the process of writing
instructions that computers can use
– Programming is both tedious and exciting
– It is tedious because all programming languages,
like spoken languages, have a series of demanding
rules
– Speakers of any human language can make several
grammatical mistakes, and still communicate with
their listeners
– But a piece of code must be perfect before it will
run
– No grammatical mistakes or syntax errors are
allowed in programming
– The programmer must fix these errors before the
program can be tested
– On the other hand, programming is exciting in
many levels
– First and foremost, writing code provides the
programmer with the chance to create something
new
– The developer gets to exercise his/her creativity
– Second, excitement comes from the challenge of
solving a problem
– The problem can be as simple as calculating a
value or as complex as determining the path of a
satellite in orbit
– Being able to solve problems, however minor, with
a piece of code is a lure that many programmers
find hard to resist
• Machine Code
– The computer’s memory and processing devices
use the binary number system, which consists of
1s and 0s
– Any software commands that directly affect
hardware must be written in the binary number
system
– Because these 1s and 0s form the language of
computer hardware, this code is often called
machine code or machine language
– Machine language consists entirely of 1s and 0s
– While this format would not make sense to you, it
would make perfect sense to the computer and
could be explained as the lowest-level computer
language
• Programming Languages
– Although a computer’s code must consist of
nothing but 1s and 0s, computer programmers do
not work that way
– Programmers use programming languages
instead of binary
– Programming languages enable the programmer
to describe a program using a variation of basic
English
– The instructions/statements written by a
programmer are saved in a file and called source
code
• Compilers and Interpreters
– After creating a piece of source code, the
programmer must convert the source code into
machine code (break it down into a series of 1s
and 0s) before it can run on a computer
– The job of converting the source code into
machine code is handled by one of two types of
programs:
• Compiler
• Interpreter
– A compiler converts all the source code into
machine code, creating an executable file
– The output of the compiler is called object code
– In some programming languages, the object code
must then be linked to produce a true executable
file
– In other languages, the object code itself is
directly executable
– The programmer can copy the object code onto
any similar system and run the program
– In other words, once compiled, the program is a
stand-alone executable file that no longer needs
the compiler to run
– Of course, each programming language requires
its own compiler to translate source code written
in that language into object code
– For example, the programming language C++
requires a C++ compiler, while the language Pascal
requires a Pascal compiler
– An interpreter also converts source code to machine
code
– Instead of creating an executable object code file, the
interpreter translates and then executes each line of
the program, one line at a time
– Interpreters translate code on the fly, so they have a
certain flexibility that compilers lack
– The interpreted code runs slower than compiled code
because the code must be interpreted each time it is
run, and a copy of the interpreter must accompany
the code everywhere it goes
– Therefore, every system that needs the program
to run must have a copy of the interpreter as well
as the source code
– Some popular interpreted programming languages
include LISP, BASIC and Visual Basic
Planning a Computer Program
• Programs can be extremely difficult to write
• Without a plan, the programmer will typically
either write a bad program or fail entirely at the
task
• If planned, the programmer will have an idea of
what needs to be done and know where to start
• Two planning tools that programmers often use
are Input-Processing-Output (IPO) chart and
Pseudocode
• The IPO chart helps the programmer
determine what is needed to the program
• It consists of three columns
• In the first column, the programmer lists what
data is needed to solve the problem
• In the last column, the programmer lists the
desired output
• The middle column is the hard part
• Here the programmer lists the steps needed
to get the desired output
• Typically the steps are written in pseudocode
• Pseudocode is natural language statements
that look like programming code
• The idea is to write in English what needs to
happen in code
• Quite often the programmer is unsure how to
write each step of the program
• At least in describing the code, the
programmer has a head start and can start
thinking about how to implement the code
• Table 1 shows an IPO chart detailing a simple
payroll application
• The program will calculate gross pay for an
hourly employee
Table 1 The IPO chart for a program that calculates gross pay for an hourly
employee
• To solve the problem, the programmer must
determine how many hours the employee
worked and his/her hourly pay
• This is handled in steps 1 and 2 of the processing
section of the IPO chart
• A wise programmer will then check to ensure that
this data makes sense
• Step 3 performs this check
• Valid working hours could be between 0 and 40
hours
• A person claiming to work more than 100
hours in a week is an example of invalid data
• The program must respond to such error
• Step 4 calculates the gross pay
• Finally, step 5 prints the answer on the screen
How Programs Solve Problems
• You already know that a program is a set of steps
for controlling a computer, but you may not know
what the steps look like
• Their appearance or structure depends a little on
the programming language, but the overall
concept is the same regardless of the language
• Each step in the code is an instruction that
performs a single task in a sequence of steps that
perform a more complex task
Program Flow Control
• When you launch a program, the computer
begins reading and carrying out statements at
the executable file’s main entry point
• Typically, this entry point is the first
line/statement in the file
• After execution of the first statement,
program control passes/flows to another
statement, and so on, until the last statement
of the program has been executed
• Then the program ends
• The order in which program statements are
executed is called program control flow
Algorithms
• The steps presented in an IPO chart usually
lead to some desired results
• Collectively, these steps are called an
algorithm
• An algorithm is a series of step-by-step
instructions that, when followed, produce a
known or expected results
• Flowcharts and Pseudocodes are a few of the
tools that are commonly used for
documenting a program’s algorithm
• A pictorial representation of the solution to a
problem in the form of steps is called a
flowchart
• It depicts the sequence of instructions in
which the program is executed
• It is used for developing and understanding the
program’s logic which is written by a programmer
• In contrary, the pseudocode shows the logic of a
program in English phrases which are small and
are independent of any specific programming
language
• Flowcharts are very effective for small problems,
whereas pseudocodes can be used for bulky
programs
Flowcharts
• A diagrammatic representation of diagrams
having a set of sequential operations which are
performed for obtaining the required solution of
a problem is called a flowchart
• A flowchart can be compared to the blueprint of
a building
• Before the construction of a building begins, a
designer/architect draws its blueprint
• Similarly, a programmer draws a flowchart before
writing the actual program
• Therefore, a flowchart can be used to:
– Analyze and define various processes
– Construct a step-by-step image of the process for
discussion, analysis or communication
– Easily write high-level language programs
– Find areas for improvement in standardization and
defining a process
• The various advantages of a flowchart are as
follows:
– Analysis of the program can be done in a more
effective way when a flowchart is used
– These are also considered to be good program
documentations which can be used for various
other purposes when needed
– Debugging programs becomes easy
– Using a flowchart helps maintain the operating
program in a simple manner and also enables the
programmer to be more efficient in putting his efforts
on a certain part
• The various disadvantages of a flowchart are as
follows:
– In case, if the logic of a program is complex, making a
flowchart is inappropriate as it creates complications
– When certain alterations are needed, the flowchart
has to be redrawn again
Pseudocode
• The pseudocode is an informal or an artificial
language which is used by programmers for
developing algorithms
• Pseudocode is a “text-based” design tool
which is detailed and used for specifying the
logic of the program
• There is no specific programming language
that is followed and therefore, it is basically
the program outline
• Pseudocodes can be used for simplifying the
development of a program by separating it into
logic design and codes as its main parts
• This enables the programmer to rely on the
algorithms without having the knowledge of the
syntactic details of any particular programming
language
• In fact, the pseudocode can be written even
without the slightest hint of the programming
language to be used for its final development
• When the logic development is complete, the
coding of the program is considered as a
translation of pseudocode into the
programming language the programmer
requires
• A pseudocode cannot be compiled or
executed as it does not have any syntax rules
• It is just a simple and one of the most
important steps which produces the final code
Heuristics
• Sometimes, no algorithm exists to solve a
problem, or the algorithm is so complex or
time-consuming that it cannot be coded or
run
• In such cases, the programmers rely on
heuristics to help solve problems or perform
tasks
• Heuristics are like algorithms; they are a set of
steps for finding the solution to a problem
• But unlike an algorithm, a heuristic does not
come with a guarantee of finding the best
possible solution
• Heuristics offer a good chance of finding a
solution, although not necessarily the best one
• Heuristics are less likely to appear in ordinary
applications because these applications rarely
require a task to be performed for which there is
no definite solution
• Heuristics are extremely common in more
advanced programs that track vast quantities of
data in complex ways
• For example, software that helps forecasters
predict the weather cannot possibly process
every piece of available data in every possible
form
• Even, if the program could process all the data, it
still could not hope to accurately predict
something as complex as the weather
• By using heuristics, however, the programs can
cull the weather-related data to find useful
information and then create a reasonably
accurate guess at the weather based on that data
• Some programs use both heuristics and
algorithms
• Antivirus software utilizes algorithms to find well-
known viruses like MS Blaster and Code Red
• Newer viruses are caught using heuristics that
look for virus-like patterns
Structured and Object-Oriented
Programming
• When writing a program, a programmer first
creates an IPO chart of algorithm or heuristic to
be followed
• Once the programmer is ready, he/she uses a
programming language to create the code that
will produce the desired results
• To create a program’s source code, programmers
generally follow one of the two programming
methods – Structured programming or Object-
oriented programming
• Structured programming evolved in the 1960s
• The name refers to the practice of building
programs out of small modules which are easy
to read and understand
• Each module had a single entrance and exit,
and performed a single task
• Structured programming practices can be
used with any programming language
• One goal of structured programming is the
creation of easy-to-read code
• Prior coding techniques resulted in “spaghetti
code” that was nearly impossible to read,
understand, and maintain
• Software developers have found that structured
programming results in improved efficiency, but
they continue to struggle with the process of
building software quickly and correctly
• Reuse of code is recognized as the best
solution to these software development
problems
• Reusing code allows programs to be built
quickly and correctly
• In the 1980s, computing took another leap
forward with the development of Object-
Oriented Programming (OOP)
• The building blocks of OOP, called objects, are
reusable, modular components
• OOP builds on and enhances structured
programming