0% found this document useful (0 votes)
83 views24 pages

Designing Program Logic

Uploaded by

Gadisa Adamu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views24 pages

Designing Program Logic

Uploaded by

Gadisa Adamu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Designing

Programming Logic

Rift Valley University ICT Department 1


INTRODUCTION OF COMPUTER PROGRAMMING

Computer Program – Is a list of instructions that the computer Three types of applications programs translate from
machine follows to properly accept input, correctly process them source code to object code:
and present the results in the most understandable way.
• Compilers – Computer software that translates (compiles)
Computer Programming – is considered to be an art and at the source code written in a high-level language (e.g., C++)
same time a science. It is an art because there is no standard way into a set of machine-language instructions that can be
to interpret a problem and solve it using a standard form of understood by a digital computer's CPU.
programming procedures and styles. It is fast becoming a science
because standard programming practice is starting to be adopted. • Interpreters – Is a computer program that directly
executes instructions written in a programming or scripting
Programs fall into two major classes: language, without requiring them previously to have been
compiled into a machine language program.
• Application Programs – application program is one that
carries out some function directly for a user, such as word • Assemblers – Is a program that converts assembly
processing or game-playing. language into machine code. It takes the basic commands
and operations from assembly code and converts them into
• Operating Systems – operating system is a program that
binary code that can be recognized by a specific type of
manages the computer and the various resources and devices processor. Assemblers are similar to compilers in that they
connected to it, such as RAM (random access memory), hard produce executable code
drives, monitors, keyboards, printers, and modems, so that they
may be used by other programs.

Designing Program Logic By: Gadisa A. 2


INTRODUCTION OF COMPUTER PROGRAMMING

Computer is composed of: The following are standard Operating System functions.
1. CPU (Central Processing Unit) – this is where all mathematical and logical forms • Validating user identification every time the user uses the computer
of processing are done on data.
• Provides standard functions like editor, compilers and linkers
2. Memory – before a computer can do any productive work such as programming, it
has to remember things such as data it needs to produce information. • Manages files of the user by providing functions like Copy, Delete,
3. Input and Output Device – data have to come into the computer by first entering Append
them through input devices. Ex. Keyboard. After the computer executes a program
to process data and produce information, this information remains in the memory. • Retrieves data out of data files
Part of the program has to show these results to the outside world. Output devices such • Manages memory for the user by determining where in the memory
as monitor screen and printers are used to display the results to the user. a program is to be loaded every time it is called to run
4.Software – computers today come bundled with some software already installed in • Makes communication between the CPU, Memory and
them. Operating System – performs for the programmer many tasks that used to be very input/output devices transparent to the user or the user are unaware
difficult to program
of these things while they are at work.
Two forms of Memory
• Internal Memory (RAM-Random Access Memory) – stores programs and data
that are executed and processed by the computer.
• Secondary Memory – supplements RAM so those computers are able to
permanently save programs and data that are not yet needed for execution and
processing. Ex. Floppy disk, hard disk & etc.

Designing Program Logic By: Gadisa A. 3


COMPUTER LANGUAGE

Programming Language
Classification of Programming Language
• A programming language is a set of rules that provides a way of
telling a computer what operations to perform. Programming Languages - Generations

• A programming language is a set of rules for communicating an • High level languages


algorithm
o 4th Generation Data query, analysis and reporting
• It provides a linguistic framework for describing computations
o 3rd Generation Imperative
• The grammatical rules are called syntax.
• Each programming language has a different set of syntax rules. • Low level languages

Why are there so many programming languages? o 2nd Generation Assembly

• Programming languages have evolved over time as better ways o 1st Generation Machine code
have been developed to design them.
o First programming languages were developed in the 1950s.
o Since then thousands of languages have been developed.
• Different programming languages are designed for different types
of programs.

Designing Program Logic By: Gadisa A. 4


COMPUTER PROGRAMMING

High level languages • Pascal and C – are general-purpose language. They are high-level language
but they can be embedded with assembly language code effectively making
• Programming languages that are closer to human language than to
these languages middle-level languages. C language is responsible in the
machine language. They are characterized to use common English
implementation of major software including operating systems, compilers and
words as their instructions. Aside from that, high level languages
software development tools
have the following characteristics: The Simplest computer programs perform three basic operations:

• A compiled program is usually not as efficient as when the program  Get the input from the keyboard.

was originally written in machine language or assembly. Each of  Process the input data.
these programming languages was designed to solve particular  Display the results on the screen.
kinds of problems.

• COBOL – Common Business Oriented Language was designed to


DISPLAY ALL
solve business problems like accounting. INPUT DATA PROCESS DATA
RESULT

• FORTRAN – Formula Translation is a high level language used


for scientific and engineering applications.

Designing Program Logic By: Gadisa A. 5


COMPUTER LANGUAGE

Low-Level Language: • Disadvantages


o More difficult to understand, maintain and debug than high level
• Machine code and Assembly code
languages.
High-Level Language: o More difficult to write without making errors.
• A programming language that allows programs to be written using English o Machine dependent making it difficult to port to a different instruction
keywords and is platform independent set processor Memory addresses and operations have to be remembered
Low Level Languages.

Low-Level Language:
• Advantages
o Achieves a smaller memory footprint (compact).
o Achieves better code optimization and therefore code will run faster and
more efficiently.
o Allows direct manipulation if the registers on the processors, giving
high levels of control.
o Processors in these systems may be slow and have limited memory so
the efficiency of assembly language or machine code is needed. Also
useful real time control systems where speed is very important.
o Since you are writing at machine level you control the level of the code
created.
o Can be easily modified.

Designing Program Logic By: Gadisa A. 6


COMPUTER LANGUAGE

What are the types of programming languages? Third Generation Languages


• First Generation Languages • Closer to English but included simple mathematical notation.
o Programs written in source code which must be translated into machine
• Second Generation Languages language programs called object code.
• Third Generation Languages o The translation of source code to object code is accomplished by a
machine language system program called a compiler.
• Fourth Generation Languages
• Alternative to compilation is interpretation which is accomplished by a system
• Fifth Generation Languages program called an interpreter.
First Generation Language • Common third generation languages
• Machine language o FORTRAN
o Operation code – such as addition or subtraction. o COBOL
o Operands – that identify the data to be processed. o C and C++
o Machine language is machine dependent as it is the only language the o Visual Basic
computer can understand. Fourth Generation Languages
o Very efficient code but very difficult to write.
• A high level language (4GL) that requires fewer instructions to accomplish a
Second Generation Languages task than a third generation language.
• Assembly languages • Used with databases
o Symbolic operation codes replaced binary operation codes. o Query languages
o Assembly language programs needed to be “assembled” for execution o Report generators
by the computer. Each assembly language instruction is translated into
one machine language instruction. o Forms designers
o Very efficient code and easier to write. o Application generators

Designing Program Logic By: Gadisa A. 7


COMPUTER LANGUAGE

Fifth Generation Languages Traditional Programming Language


• Declarative languages • FORTRAN
o Formula Translation.
• Functional(?): Lisp, Scheme, SML
o Also called applicative o Developed at IBM in the mid-1950s.
o Everything is a function o Designed for scientific and mathematical applications by scientists and
engineers.
• Logic: Prolog
• COBOL
o Based on mathematical logic
o Common Business Oriented Language.
o Rule- or Constraint-based
o Developed in 1959. ◦ Designed to be common to many different
Beyond Fifth Generation Language computers.
o Typically used for business applications.
• Though no clear definition at present, natural language programs generally can
be interpreted and executed by the computer with no other action by the user • BASIC
than stating their question.
o Beginner’s All-purpose Symbolic Instruction Code.
• Limited capabilities at present. o Developed at Dartmouth College in mid 1960s.
The principal paradigm o Developed as a simple language for students to write programs with
which they could interact through terminals.
• Imperative Programming (C)
• C
• Object-Oriented Programming (C++) o Developed by Bell Laboratories in the early 1970s.
• Logic/Declarative Programming (Prolog) o Provides control and efficiency of assembly language while having
third generation language features.
• Functional/Applicative Programming (Lisp)
o Often used for system programs. ◦ UNIX is written in C.

Designing Program Logic By: Gadisa A. 8


HIERARCHY OF HIGH LEVEL LANGUAGES

High Level Languages

• High level languages are problem-orientated whereas low level languages are
machine-orientated.

• Problem-orientated means that the commands and the way the program is
High Level structured are based on what the program will have to do rather than the
Programming
Languages
components of the computer it will be used with.

• This means that programs of high level languages are portable. These programs
Imperative Declarative
can be written on one computer and then executed on another.

• There are 2 classified groups of high level languages

Object Oriented Procedural Logic Database Query Functional o Imperative: tells the computer how to do things

o Declarative: focuses on what we want to get from the computer

Designing Program Logic By: Gadisa A. 9


ALGORITHM

What is an algorithm? Characteristics of an Algorithm


• A set of steps to accomplish a task • Unambiguous − Algorithm should be clear and unambiguous.
Each of its steps (or phases), and their inputs/outputs should be
• An algorithm is a sequence of unambiguous instruction clear and must lead to only one meaning.
• An algorithm is a well-defined procedure that allows a computer to • Input − An algorithm should have 0 or more well-defined inputs.
solved a problem.
• Output − An algorithm should have 1 or more well-defined
• An algorithm is a self-contained step-by-step set of operation to be outputs, and should match the desired output.
performed to solve a specific problem.
• Finiteness − Algorithms must terminate after a finite number of
How to write an algorithm? steps.
• Keep in mind that algorithm is a step-by-step process. • Feasibility − Should be feasible with the available resources.
• Depending upon programming language, include syntax where • Independent − An algorithm should have step-by-step directions,
necessary. which should be independent of any programming code.
• Include variables and their usage. Two forms of Algorithm:
• If they are any loops, try to give sub number lists. • Pseudocode
• Try to give go back to step number if loop or condition fails. • Flowchart
• Use jump statement to jump from one statement to another.
• Try to avoid unwanted raw data in algorithm.
• Use break and stop to terminate the process.

Designing Program Logic By: Gadisa A. 10


ALGORITHM: PSEUDOCODE

Pseudocode
• Keywords are written in CAPITALS.
Pseudocode essentially is English with some defined rules of structure and some
keywords that make it appear a bit like program code. Some guidelines for writing • Structural elements come in pairs,
pseudocode are as follows.
o for every BEGIN there is an END
Pseudocode Guidelines
• The keywords used for pseudocode in this document are: o for every IF there is an ENDIF. etc.
1. for start and finish
• Indenting is used to show structure in the algorithm.
BEGIN MAINPROGRAM, END MAINPROGRAM
2. for initialization
• The names of subprograms are underlined.
INITIALISATION, END INITIALISATION
3. for subprogram o This means that when refining the solution to a problem. a word in an
BEGIN SUBPROGRAM. END SUBPROGRAM algorithm can be underlined and a subprogram developed.
4. for selection
IF. THEN, ELSE, ENDIF This feature enables the use of the 'top-down' development concept. where details
5. for multi-way selection for a particular process need only be considered within the relevant sub-routine.
CASEWHERE. OTHERWISE. ENDCASE
6. for pre-test repetition
WHILE. ENDWHILE
7. for post-test repetition
REPEAT. UNTIL

Designing Program Logic By: Gadisa A. 11


ALGORITHM: FLOW CHARTS
Basic flow charting shapes and symbols
Flow Charts
Flowcharts use special shapes to represent different types of actions or steps in a
• A drawing that uses symbols interconnected with lines to represent the flow of process and Lines and arrows show the sequence of the steps, and the
relationships among them.
information

• A process flow diagram that illustrates the successive steps in a process, Start/End – The terminator symbol marks the starting or ending point the system.
procedure, system or model. It usually contains the word "Start" or "End."

• A pictorial representation showing/describing all of the steps of a process being


studied or even used to plan stages of a project.

Purpose of Flow Charts Action or Process – A box can represent a single step ("add two cups of flour), or
and entire sub-process ("make bread") within a larger process.
A Flowchart is used for:

1. Defining and analyzing processes

Decision – A decision or branching point. Lines representing different decisions


2. Building a step-by-step picture of the process for analysis, discussion, or
emerge from different points of the diamond.
communication purposes

3. Defining, standardizing, or finding areas for improvement in a process

By visualizing the process, a flowchart can quickly help identify bottlenecks or


inefficiencies where the process can be streamlined or improved.

Designing Program Logic By: Gadisa A. 12


ALGORITHM: FLOW CHARTS

Connector – Indicates that the flow continues where a matching symbol Example of Flowchart
containing the same letter) has been placed.

Flow Line – Lines indicate the sequence of steps and the direction of flow.

Input/ Output – Represents material or information entering or leaving the


system, such as customer order (input) or a product

Designing Program Logic By: Gadisa A. 13


ALGORITHM: FLOW CHARTS

Common Types of Flowcharts


3. The Swim lane Flowchart
There are four flowchart types that are particularly versatile. These four common
diagrams are great for describing business, manufacturing, or administrative Describe How Separate Departments, Processes or Employees Interact
processes, seeing how an organization functions, or how different departments
work together.
The swim lane flowchart comes in handy when you need to show multiple
1. The Process Flowchart
flows of information side by side.
Illustrate How a Process Works or Plan a Project with a Process Flowchart
A process flowchart or process flow diagram is probably the most versatile of A swim lane flowchart or diagram is great for documenting a whole process
the four commonly used flowchart types because it can be applied to virtually
anything. that interacts with different segments of an organization or requires
• Map out roles and responsibilities within an organization to gain clarity. collaboration among different teams.

2. The Workflow Chart


4. The Data Flowchart
Understand How Data and Documents Flow Within Your Organization
See Where Data Flows In and Out of an Information System with a Data Flow
A workflow chart shows the way a business or process functions. This type of
workflow diagram can be used to: Diagram
• Train new employees
A data flowchart or data flow diagram shows the way data is processed. It
• Discover potential problem areas
comes in handy when you want to design or analyze a system. Although most
• Clarify business operations by showing a high-level overview
often used for software development and design, it can be used to analyze any
type of information flow, like how information moves through a business.

Designing Program Logic By: Gadisa A. 14


ALGORITHM: FLOW CHARTS

Steps for creating a flowchart are: CONSTRUCTION/INTERPRETATION tip for a flow chart.

1. Familiarize the participants with the flowchart symbols • Define the boundaries of the process clearly.

2. Brainstorm major process tasks. Ask questions such as "What • Use the simplest symbols possible.
really happens next in the process? ", "Does a decision need to be
• Make sure every feedback loop has an escape.
made before the next step? ", or What approvals are required
before moving on to the next task?" • Analyze flow chart of actual process.

3. Draw the process flowchart using the symbols on a flip chart or • Analyze flow chart of best process.
overhead transparency.
• Compare both charts, looking for areas where they are different.
Every process will have a start and an end (shown by elongated Most of the time, the stages where differences occur is considered
circles). All processes will have tasks and most will have decision
to be the problem area or process.
points (shown by a diamond).
• Take appropriate in-house steps to correct the differences between
the two separate flows.

Designing Program Logic By: Gadisa A. 15


ALGORITHM: FLOW CHARTS

Tips for Effective Flowcharts Flowchart and Pseudocode


1. Label each flowchart with a title identifying the process that it illustrates. (For
example: Entry Process").
2. Clearly indicate the starting and ending points of the process, using the
standard terminator symbols.
3. Keep the direction of flow consistent. Avoid confusion by keeping your flow
lines moving from top to bottom and left to right.
4. Break the steps down to a consistent level of detail. Don't include trivial sub-
steps of one task while treating another equivalent task as a whole.
5. Avoid crossing flow lines. In a well-designed chart, flow lines will not cross
each other. By rearranging a chart, you can usually get rid of crossed lines
6. Make sure there are at least two no outcomes from every decision diamond.
5. Label your flowchart components. Use active verbs to label activity steps and
questions to label decisions. Clearly label the form a decision diamond in
terms that answer the question.

Designing Program Logic By: Gadisa A. 16


DATA TYPES

Data Types We can declare it as below.


• A data type defines a set of values and a set of operation on those values. A C+ Char a= ‘A’
+ program may need to required different type of data. Each type required Char f= ‘B’
different type of memory. Char z= ‘D’ etc
• It indicates the type of data that can be store in a variable
• There are three basic data type. Integer data type:
o Character • A numeric value with no fraction is called integer data. It include both positive
o Integer and negative values.
o Float • Represented by “int”
Character data type • Int range –32,768 to 32,767
• This data type is used to store character values. • Some example of integer value are 56 and -12
• Characters may include letters, digits and punctuation character. It take one • Int size is ‘2’ bytes
byte in memory i.e. ‘a’ and ‘@’ etc.
• Long int size ‘4’ bytes
• Represented by the “char” data type.
We can declare it as below.
Int a=20;
Int b=40;
Int c=30;

Designing Program Logic By: Gadisa A. 17


DATA TYPES

Type of Integer data type Type of float


Int data type • Float data type
o This data type is used to store real values and
• To store integer values and takes two byte in memory. i.e.
o Takes ‘4’ bytes in memory. i.e. Float num;
int num;
• Double data type
Long int data type
o It has great range as compared to float and takes ‘8’ byte in memory. i.e.
• To store large integer value. It has greater range and takes ‘4’ bytes in memory. Double num;
i.e. long int num;
Float data type:
• A numeric value with fraction is called real data type / float data type. It
include both
• Positive and negative values. i.e. 12.4 and -34.3
• This data type are represented by “float”.
• Float number are declared as
Float a= “40.00”
Float b= “23.04”
Float c= “10.3” etc.

Designing Program Logic By: Gadisa A. 18


DATA TYPES

Rules for variable Declaration


Variable
• Variable may include letter, number and underscore (_)
• A quantity whose values may change during the execution of the program is • The first latter of the variable must be character or underscore.
called variable.
• Blank spaces special symbols, commas are not used

• It may be a numeric or non-numeric quantity • A variable can be ‘31’ character long for many compilers
• Key word are not used as variable name.
• It represented by an identifier called variable name
Variable Declaration
• A variable represents a memory location in the computer memory. Data is store • The process of specifying the name and its data type is called variable
into the memory location. The name of memory location or variable name declaration

remains fixed during the execution of the program but the data store in that • All variables that are used in a program are declared using variable declaration
statement.
location may change from time to time
• When a variable is declared a certain number of bytes in the memory are
allocated to the variable name.
Variable Declaration
Variable Initialization
• A variable is case sensitive, that is, uppercase variables are treated differently
• The process of assigning a value to a variable at the time of declaration is
known as variable initialization.
• Variable “SUM” is different from variable “sum”
• The equal (=) is used to initialize a variable.
• Variable “NetPay” is different from variable “NETpay” • Variable name is given on the left side and value is given on the right side of
equal sign.

Designing Program Logic By: Gadisa A. 19


STRUCTURE: CONTROL STRUCTURE

if Statement
If…else

‘if’ statements test whether a certain condition has been reached, such as whether
The ‘if…else’ statements allow either/or statements to be made. These

a number has reaches a certain value, and if true, executes the statements in the statements can be used when more than one courses of action are possible, as

brackets. determined by the variable or value being tested. The syntax for ‘if…else’
implementation is:
The code in the brackets is skipped if the statement is false.

The syntax for if statement is:


void loop(){

void loop(){
if(inputPin == HIGH){
if(some variable <test condition> value){
doThing1;
Do Something;
}else{
}

} doThing2;

}
The above condition test compares some variable with a value. If this condition is
satisfied, then the code inside the parenthesis (do something) is executed.

}
Designing Programming Logic By: Gadisa A. 20
STRUCTURE: CONTROL STRUCTURE

If…else if ladder for Statement

If more than two courses of action are possible, then the ‘if…else if’ The ‘for’ statement is used to repeat a statement of a block of
ladder is to be used, which allows the choice and execution of one statements a specified number of times. The arguments for the ‘for’
statement from an infinite number of possibilities. The syntax is as loop consists of:
follows:
1. Initialization. This initializes a variable with a specified initial
value.
void loop(){ 2. Test condition. This condition is tested for every loop and the ‘for’
statement stops looping when this condition does not hold true.
if(input Pin <test condition> value1){ 3. Increment/decrement expression. This expression operates on the
doThing1; initialized variable.
}else if(input Pin <test condition> The syntax for the ‘for’ loop is as follows:
value2){
doThing2;
}else if(input Pin <test condition> void loop(){
value3){
doThing3; for(initialization; condition;
}else{ increment/decrement expression){
doThing4; Do Something;
} }

} }

Designing Programming Logic By: Gadisa A. 21


STRUCTURE: CONTROL STRUCTURE

do…while Loop
while Loop
The ‘do…while’ class works in a similar manner to the
The ‘while’ loop will loop continuously and infinitely, until the ‘while’ class, the only difference being, that, in the ‘do…
expression inside the brackets become false. If the tested variable of
while’ class the condition is tested at the bottom of the loop,
making sure the loops is carried out at least once.
value is not changed, the loop will go on for infinity.

void loop(){

void loop(){
do{
Do Something;
}while(variable <test condition> value)
while(variable <test condition> value){

Do Something; }
}

Designing Programming Logic By: Gadisa A. 22


FUNCTIONS: MATH

max(x,y)
min(x,y)
Returns the larger of the two values passed into the function
Returns the smaller of the two values passed into the function as arguments.

as arguments.
void loop(){

int x,y;
void loop(){
int larger Num = max(x,y); //Sets
larger Num equal to
//x or y depending upon
//whichever is
int x,y; larger
}
int smaller Num = min(x,y); //Sets
smaller Num equal
//to x or y depending upon
//whichever is smaller

Designing Programming Logic By: Gadisa A. 23


FUNCTIONS: RANDOM NUMBERS

randomSeed(seed) random(min,max)
Sets a value as a starting point before the random() function. The seed can be any This function helps in generating pseudo-random values for the given range in the
variable, input, constraints or other functions. Must be used before the random arguments. The function randomSeed(seed) must be used before using this
function. function.

void loop(){

void loop(){
int value_1;
randomSeed(analogRead(3)); //Uses the analog value at
//pin 3 to set a int value_2;
starting //point for the
random //function
} rendomSeed(analogRead(3));

value_l = random(100); //Sets value_l equal to any


//random number between 0
and //100
value_2 = random(50,100): //Sets value_2 equal to any
//random number
between SO //and 100
}

Designing Programming Logic By: Gadisa A. 24

You might also like