Designing Program Logic
Designing Program Logic
Programming Logic
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.
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.
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
• 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.
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.
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.
• 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.
Object Oriented Procedural Logic Database Query Functional o Imperative: tells the computer how to do things
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
• 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."
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:
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.
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.
• 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.
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.
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 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;
} }
} }
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; }
}
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
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));