PPROGRAMMING

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

Introduction to Programming

THE COPPERBELT UNIVERSITY


SCHOOL OF TECHNOLOGY
COMPUTER SCIENCE DEPARTMENT

PROGRAMMING

Programming can be broadly defined as

a) Instructing a computer to do something for you with the help of a programming language.

b) The process of translating the system specifications prepared during the design stage into
program code.

A program can be defined as a set of instructions that is written in a programming language. It is a


complete statement of a solution to a problem capable of being executed on a computer.

There are two types of programming languages; namely:


 Low- Level Languages (Machine and Assembly Languages)
 High-Level Languages

LOW-LEVEL LANGUAGES

In computing, a programming language designed for a particular computer and reflecting its internal
machine code. Low-level languages are therefore often described as machine-oriented languages.
They cannot easily be converted to run on a computer with a different central processing unit, and they
are relatively difficult to learn because a detailed knowledge of the internal working of the computer is
required.

All computers operate by following machine language programs, a long sequence of instructions
called machine code that is addressed to the hardware of the computer and is written in binary
notation (see numeration), which uses only the digits 1 and 0. First-generation languages, called
machine languages, required the writing of long strings of binary numbers to represent such
operations as “add,” “subtract,” “and compare.” Later improvements allowed octal, decimal, or
hexadecimal representation of the binary strings.

Because writing programs in machine language is impractical (it is tedious and error prone), symbolic,
or assembly, languages-second-generation languages were introduced in the early 1950s. They use
simple mnemonics such as A for “add” or M for “multiply,” which are translated into machine
language by a computer program called an assembler. The assembler then turns that program into a
machine language program. An extension of such a language is the macro instruction, a mnemonic
(such as “READ”) for which the assembler substitutes a series of simpler mnemonics. The resulting
machine language programs, however, are specific to one type of computer and will usually not run on
a computer with a different type of central processing unit (CPU).

In assembly language, for example, ‘JMP’ means ‘jump’ and ‘LDA’ means ‘load accumulator’.
Assembly code is used by programmers who need to write very fast or efficient programs.

By Serah Beza Mbewe 1


Introduction to Programming

HIGH-LEVEL LANGUAGES

These are a type of advanced computer programming languages that isn't limited by the type of
computer or for one specific job and is more easily understood. Today, there are dozens of high-level
languages; some commonly used high-level languages are BASIC, C, FORTAN and Pascal. They are
used to write programs that are more or less independent of a particular type of computer. Such
languages are considered high-level because they are closer to human languages and further from
machine languages. In contrast, assembly languages are considered low-level because they are very
close to machine languages.

The main advantage of high-level languages over low-level languages is that they are easier to read,
write, and maintain. Ultimately, programs written in a high-level language must be translated into
machine language by a compiler or interpreter.

GOOD QUALITIES OF A PROGRAM

External qualities: What the user cares about.


• Usefulness — does it do what the user wants?
• Performance — is it fast enough and can it fit in my computer? ( Speed, size).
• Correctness — does it meet its specification? (Assumes precise specification)
• Reliability — does it do what the user wants most of the time?
• Robustness — does it respond well to error/failures of other systems?
• Usability — is it easy to use?
• Interoperability — does is conform to relevant standards/formats etc.?
• User documentation.

Internal qualities : What future developers will care about (Attributes of source code and
documentation)
• Correctness — internal documentation consistent with itself and the code.
• Changeability — can the most likely changes be made easily? Can other changes be made
reasonably?
• Understandability — is code & documentation understandable?
• Reusability — Can components be reused in other projects? (Process qualities)
 Portability—can it be installed on any machine?

What managers care about


• Cost — production and maintenance.
• Timeliness — When will it be ready?
• Traceability — Can the progress of the production be monitored?

STRUCTURED PROGRAMMING
By Serah Beza Mbewe 2
Introduction to Programming

Structured programming is a subset of procedural programming that enforces a logical structure on the
program being written to make it more efficient and easier to understand and modify. Certain
languages such as Ada, Pascal, and dBASE are designed with features that encourage or enforce a
logical program structure.

Structured programming frequently employs a top-down design model, in which developers map out
the overall program structure into separate subsections. A defined function or set of similar functions
is coded in a separate module or submodule, which means that code can be loaded into memory more
efficiently and that modules can be reused in other programs. After a module has been tested
individually, it is then integrated with other modules into the overall program structure.

Program flow follows a simple hierarchical model that employs looping constructs such as "for,"
"repeat," and "while." Use of the "Go To" statement is discouraged.

Therefore structured programming can be defined as a technique for organizing and coding computer
programs in which a hierarchy of modules is used, each having a single entry and a single exit point,
and in which control is passed downward through the structure without unconditional branches to
higher levels of the structure. Three types of control flow are used: sequential, test, and iteration.

MODULAR PROGRAMMING

Modular programming is subdividing your program into separate subprograms such as functions and
subroutines. For example, if your program needs initial and boundary conditions, use subroutines to
set them. Then if someone else wants to compute a different solution using your program, only these
subroutines need to be changed. This is a lot easier than having to read through a program line by line,
trying to figure out what each line is supposed to do and whether it needs to be changed. And in ten
years from now, you yourself will probably no longer remember how the program worked.

Subprograms make your actual program shorter, hence easier to read and understand. Further, the
arguments show exactly what information a subprogram is using. That makes it easier to figure out
whether it needs to be changed when you are modifying your program. Forgetting to change all
occurrences of a variable is a very common source of errors.

Subprograms make it simpler to figure out how the program operates. If the boundary conditions are
implemented using a subroutine, your program can be searched for this subroutine to find all places
where the boundary conditions are used. This might include some unexpected places, such as in the
output, or in performing a numerical check on the overall accuracy of the program.

Subprograms reduce the likelihood of bugs. Because subprograms can use local variables, there is less
change that the code in the subroutine interferes with that of the program itself, or with that in other
subprograms. The smaller size of the individual modules also makes it easier to understand the global
effects of changing a variable.

Perform your output using a subprogram. You tend to become more careless when programming a `no
critical' program part such as output. Also, output often requires additional variables not of interest to

By Serah Beza Mbewe 3


Introduction to Programming

the rest of the program. The code for output tends to be lengthy; hence your program will become
shorter and easier to read if you move output to a subroutine.

Use the correct type of subprogram. For example, if your program uses an exact solution, cast it into
the form of a function subprogram, not a subroutine. Clarity and ease of use improve.

Bottom up methods

It is clear that many problems that occur in programming, and system design, are too large to be
solved all at once. The answer is to try to solve such problems in stages. But where should one begin?
If the problem is different from anything met before, it may be necessary to set about solving a part of
the problem that is understood, and then, as other parts of the problem become clearer, solving them
until the whole problem is solved. Such an approach in which the whole situation is built up by joining
together the completed parts is called a “bottom up” approach. In this approach one start from what
output is needed from the system, then they go on to determine what input and process will produce
the output needed.

Top down methods

There is one severe danger inherent in the bottom up approach; the various parts may not fit together
properly, and, in some cases, considerable effort may be necessary to overcome this problem. “Top
down” methods attempt to avoid such problems by starting with the general concept of what is
required, breaking the whole into component parts, and then tackling the component parts in the same
manner. The interrelationships between components are decided upon before the components are
created so that the final integration of the parts into the whole should be more straightforward. A
number of methods in use today may be described as top down.

PROGRAMMING TOOLS

FLOWCHARTS

One of the most powerful facilities available in programming languages is the ability to perform
operations selectively under certain conditions. A flowchart is logic diagram that describes each step
that the program must perform to arrive at the solution and how the steps relate to each other. It is a
popular logic tool used for showing an algorithm in graphics form

Programmer prepares flowchart before coding. They are not used very often today because of newer
superior methods.

Most common Flow chart symbols are

Direction Start / End Input / Output Decision Process Connector


(Flow line and
arrowhead)

By Serah Beza Mbewe 4


Introduction to Programming

The most important point: it is vital to be quite clear precisely under what conditions operations are to
be carried out by a program. These conditions must be expressed as program instructions in the
simplest and clearest manner possible.

In its most general sense, a conditional operation implies a choice between two alternatives, with a
condition for making the choice.
For example
If the weather is fine then go fishing
else watch television.

If the condition (the weather is fine) is true, then the first action (go fishing) is carried out. If the
condition is false, then the second action (watch television) is carried out.
The following figure illustrates this process in terms of flow diagram symbols.

PSEUDO CODE

Pseudo means imitation. So pseudo code is an imitation of actual computer instructions. It is a notation
for program design using keywords from a programming language mixed with natural language (e.g.
English) which outlines the task to be performed. It is also called Program Design Language (PDL). It
has no formal syntactical rules.
The principal aim is to allow the programmer to express his understanding of the problem and its
proposed solution; he must write down a sequence of successively more detailed descriptions of the
problem and its solution which can be read and understood by a human being (perhaps another
programmer) and so checked for correctness.
The flowchart only emphasizes logical steps (detailed plan) for a program; pseudocode emphasizes
both logical steps and a top-down structure for a program.

By Serah Beza Mbewe 5


Introduction to Programming

FLOWCHARTS AND PSEUDOCODES FOR MAIN PROGRAM CONTROL STRUCTURES

1 SELECTION

a) If statement

An If block allows a program to decide on a course of action based on whether


a certain condition is true or false. A block of the form
If condition Then
Action l
Else
action 2
End If
causes the program to take action 1 if condition is true and action 2 if condition is false. Each action
consists of one or more Visual Basic statements. After an action is taken, execution continues with the
line after the If block.

Flowchart:

Pseudocode:

If condition is true Then


Processing step(s) 1
Else
Processing step(s) 2
End If

b) Select Case

Flowchart:

By Serah Beza Mbewe 6


Introduction to Programming

Pseudocode:

Select Case selector


Case valuelist1
Action 1
Case valuelist2
Action 2
:
Case valuelistn
Action n
Case Else
Action of last resort
End Select

2 LOOPS

A loop, one of the most important structures in Visual Basic, is used to repeat a sequence of statements
a number of times. At each repetition, or pass, the statements act upon variables whose values are
changing.

a) Do Loops

The Do loop repeats a sequence of statements either as long as or until a certain condition is true. A
Do statement precedes the sequence of statements, and a Loop statement follows the sequence of
statements. The condition, along with either the word While or Until, follows the word Do or the word
Loop. When Visual Basic executes a Do loop of the form

Do While condition
statement (s)
Loop

By Serah Beza Mbewe 7


Introduction to Programming

it first checks the truth value of condition. If condition is false, then the statements inside the loop are
not executed, and the program continues with the line after the Loop statement, If condition is true,
then the statements inside the loop are executed. When the statement Loop is encountered, the entire
process is repeated, beginning with the testing of condition in the Do While statement. In other words,
the statements inside the loop are repeatedly executed only as long as (that is, while) the condition is
true.

Flowchart:
Pseudocode:

Do While condition is true


Processing step(s)
Loop

b) For … Next

When program execution reaches a For….Next loop, such as the one shown previously, the For
statement assigns to the control variable i the initial value 1 and checks to see whether i is greater than
the terminating value n. If so, then execution jumps to the line following the Next statement. If i <= n,
the statements inside the loop are executed. Then, the Next statement increases the value of i by 1 and
checks this new value to see if it exceeds n. If not, the entire process is repeated until the value of i
exceeds n. When this happens, the program moves to the line following the loop.

For i = 1 to n
Processing step(s)
By Serah Beza Mbewe 8
Introduction to Programming

Next i

By Serah Beza Mbewe 9

You might also like