You are on page 1of 28

PROGRAMMING

TECHNIQUES
Lecture 2a
Lecture Outline
■ Algorithms
■ Pseudocode
■ Flowcharts
■ Types of Errors
Algorithms
■ An algorithm is a sequence of steps for solving a specific
problem given its input data and the expected output
data.
– It can also be described as a step-by-step problem-
solving process in which a solution is arrived at in a
finite amount of time.
■ When we write a computer program, we are generally
implementing a method that has been devised previously
to solve some problem.
Example
■ Music Purchase Program Algorithm
1. Enter the number of songs
2. Calculate the price
– Use the formula: Price = 0.99 * songs
3. Display the results: Price
Operations used to construct algorithms belong
to one of three categories:
■ Sequential (sequence) operations:
– This carries out a single well-defined task. When that task
is finished, the algorithm moves on to the next operation.
■ Conditional (decision/selection) operations:
– These ask a question, and the next operation is selected
on the basis of the answer to that question.
■ Iterative (loop/repetition) operations
– They tell us not to go on to the next instruction but,
instead, to go back and repeat the execution of a previous
block of instructions.
The Sequential Operations
■ A sequential operations consists of a series of
consecutive statements, executed in the order in which
they appear.
■ None of the statements in this operation causes a
branch. The general form of a sequential structure is as
follows:
Statement 1
Statement 2
…Statement N
Conditional Operations
■ Selection/decision/conditional operations contain branch
points or statements that cause a branch to take place.
■ In a conditional operation there is a branch forward at
some point, which causes a portion of the program to be
skipped.
■ Depending upon a given condition at the branch point, a
certain block of statements will be executed while
another is skipped.
Conditional Structure
Iterative (Loop/Repetition) Operation
■ A iterative operation contains a branch which results in a
block of statements that can be executed many times.
■ It will be repeated as long as a given condition within the
loop structure causes the branch to be taken.
– The ability to repeat the same actions over and over is
the most basic requirement in programming
Repetition Operation
Pseudocode
■ Pseudocode uses short, English-like phrases to describe
the outline of a program.
– It’s not actual code from any specific programming
language, but sometimes it strongly resembles the
actual code.
Example 1
■ Pseudocode for cooking rice might look as follows:
1) Wash the rice
2) Put into boiling water in saucepan
3) Leave to boil for some minutes until thoroughly cooked
■ Pseudocode for circle area program might look as follows:
1) Input the circle radius
2) Compute circle area using the circle area formula
3) Output the results of circle area
Example 2
■ Pseudocode for adding two numbers might look as:
1) Enter first number
2) Enter second number
3) Calculate the sum of the two numbers, i.e. num1 + num2
4) Display the results
Try
1. Write an algorithm in pseudocode to find the profit on
each item bought in a shop.
2. Write an algorithm in pseudocode to find the area and
perimeter of a rectangle.
Flow charts
■ This is a diagram that uses special symbols to display
pictorially the flow of execution within a program or a
program module.
■ It is a formalized graphic representation of a program’s
logic sequence, a work or manufacturing process, or any
similar structure.
■ It provides an easy, clear way to see which pieces of code
follow the various programming structures that all
programs are constructed from.
Creating flow charts
■ A programmer would create a flow-chart to aid in
understanding the flow of a program.
■ Then, the programmer would write code, in a specific
programming language, following the logic that he had
created in the flowchart.
■ Interactive flowcharting applications are available for
creating flow charts
– Raptor, MS word templates, etc
Flow Chart Symbols
■ A typical flowchart will include some or all of the following
symbols;
– Start and End symbols are represented as ovals or
rounded rectangles.
– Arrows show the flow of control. An arrow coming from
one symbol and ending at another symbol represents
that control passes to the symbol to which the arrow
points.
– Processing steps are represented as rectangles.
Flow Chart Symbols
– Input / Output steps are represented as parallelograms.
– Conditional (or decision or selection) segments are
represented as diamond shapes. These typically
contain a Yes/No question or a True/False test. This
symbol has two arrows coming out of it.
– Connectors are represented as circles. These are used
to connect one program segment to another.
Flow
Chart
Symbols
Example 1 Start

■ Consider the pseudocode below: Welcome


Message
1. Display Welcome Message
2. Input Data
Input Data

3. Calculate Average
4. Output Results
Calculate Average

Output Results

End
Example 2
Circle area program
1) Input the circle radius
2) Compute circle area using the circle area formula
3) Output the results of circle area

Input circle Compute Display results:


Start End
radius Area = 𝜋 ∗ 𝑟𝑎𝑑𝑖𝑢𝑠^2 Circle Area
Example 3
Example 4
Example 5
Types of Programming Errors
■ Errors are common in when writing programs.
■ If a test run turns up problems with the program, we must
debug it, which means we must locate and eliminate the
errors.
■ The two fundamental types of errors that can arise in
coding a program are syntax errors and logic errors.
Syntax Errors
■ A syntax error is the violation of the programming language’s
rules for creating valid statements.
■ It can be caused, for example, by misspelling a keyword or by
omitting a required punctuation mark.
■ Syntax errors are detected by the language software, either
when the invalid statement is typed or when the program is
translated by the computer into machine language.
■ When the software detects a syntax error, normally it issues a
message and highlights the offending statement. Therefore,
syntax errors are often easy to find and correct.
Logic Errors
■ A logic error results from failing to use the proper
combination of statements to accomplish a certain task.
■ It may occur due to faulty analysis, faulty design, or failure
to code the program properly.
■ Logic errors often cause the program to fail to proceed
beyond a certain point (i.e., to crash or hang or freeze) or
to give incorrect results.
– Unlike syntax errors, logic errors are not detected by
the programming language software.
The following are a few kinds of logic errors:
– Use of an incorrect formula to produce a desired
result
– Use of an incorrect sequence of statements to carry
out an algorithm.
– Failure to predict some input data that may lead to an
illegal operation (such as division by zero) when the
program is run (this kind of bug is sometimes called a
runtime error).

You might also like