SlideShare a Scribd company logo
1
Designing
ICT Department
Rift Valley University
Programming Logic
2
By: Gadisa A.
Designing Program Logic
INTRODUCTION OF COMPUTER PROGRAMMING
Computer Program – Is a list of instructions that the
computer machine follows to properly accept input,
correctly process them and present the results in the
most understandable way.
Computer Programming – is considered to be an art and
at the same time a science. It is an art because there is
no standard way to interpret a problem and solve it
using a standard form of programming procedures and
styles. It is fast becoming a science because standard
programming practice is starting to be adopted.
Programs fall into two major classes:
• Application Programs – application program is one
that carries out some function directly for a user, such
as word processing or game-playing.
• Operating Systems – operating system is a program
that manages the computer and the various resources
and devices connected to it, such as RAM (random
access memory), hard drives, monitors, keyboards,
printers, and modems, so that they may be used by
other programs.
Three types of applications programs translate
from source code to object code:
• Compilers – Computer software that translates
(compiles) source code written in a high-level
language (e.g., C++) into a set of machine-
language instructions that can be understood by
a digital computer's CPU.
• Interpreters – Is a computer program that directly
executes instructions written in a programming or
scripting language, without requiring them
previously to have been compiled into a
machine language program.
• Assemblers – Is a program that converts
assembly language into machine code. It takes
the basic commands and operations from
assembly code and converts them into binary
code that can be recognized by a specific type
of processor. Assemblers are similar to compilers
in that they produce executable code
3
By: Gadisa A.
Designing Program Logic
INTRODUCTION OF COMPUTER PROGRAMMING
Computer is composed of:
1. CPU (Central Processing Unit) – this is where all mathematical and
logical forms of processing are done on data.
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.
3. Input and Output Device – data have to come into the computer by
first entering them through input devices. Ex. Keyboard. After the
computer executes a program to process data and produce
information, this information remains in the memory.
Part of the program has to show these results to the outside world.
Output devices such as monitor screen and printers are used to display
the results to the user.
4.Software – computers today come bundled with some software
already installed in them. Operating System – performs for the
programmer many tasks that used to be very difficult to program
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.
The following are standard Operating System functions.
• Validating user identification every time the user uses
the computer
• Provides standard functions like editor, compilers and
linkers
• Manages files of the user by providing functions like
Copy, Delete, Append
• Retrieves data out of data files
• Manages memory for the user by determining where in
the memory a program is to be loaded every time it is
called to run
• Makes communication between the CPU, Memory and
input/output devices transparent to the user or the user
are unaware of these things while they are at work.
4
By: Gadisa A.
Designing Program Logic
COMPUTER LANGUAGE
Programming Language
• A programming language is a set of rules that provides
a way of telling a computer what operations to
perform.
• A programming language is a set of rules for
communicating an algorithm
• It provides a linguistic framework for describing
computations
• The grammatical rules are called syntax.
• Each programming language has a different set of
syntax rules.
Why are there so many programming languages?
• Programming languages have evolved over time as
better ways 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.
Classification of Programming Language
Programming Languages - Generations
• High level languages
o 4th Generation Data query, analysis and reporting
o 3rd Generation Imperative
• Low level languages
o 2nd Generation Assembly
o 1st Generation Machine code
5
By: Gadisa A.
Designing Program Logic
COMPUTER PROGRAMMING
High level languages
• Programming languages that are closer to human
language than to machine language. They are
characterized to use common English words as their
instructions. Aside from that, high level languages have
the following characteristics:
• A compiled program is usually not as efficient as when
the program was originally written in machine language
or assembly. Each of these programming languages
was designed to solve particular kinds of problems.
• COBOL – Common Business Oriented Language was
designed to solve business problems like accounting.
• FORTRAN – Formula Translation is a high level language
used for scientific and engineering applications.
• Pascal and C – are general-purpose language. They are high-
level language but they can be embedded with assembly
language code effectively making these languages middle-level
languages. C language is responsible in the implementation of
major software including operating systems, compilers and
software development tools
The Simplest computer programs perform three basic operations:
Get the input from the keyboard.
Process the input data.
Display the results on the screen.
INPUT DATA PROCESS DATA
DISPLAY ALL
RESULT
6
By: Gadisa A.
Designing Program Logic
COMPUTER LANGUAGE
Low-Level Language:
• Machine code and Assembly code
High-Level Language:
• A programming language that allows programs to be written
using English keywords and is platform independent
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.
• Disadvantages
o More difficult to understand, maintain and debug than
high level languages.
o More difficult to write without making errors.
o Machine dependent making it difficult to port to a different
instruction set processor Memory addresses and operations
have to be remembered Low Level Languages.
7
By: Gadisa A.
Designing Program Logic
COMPUTER LANGUAGE
What are the types of programming languages?
• First Generation Languages
• Second Generation Languages
• Third Generation Languages
• Fourth Generation Languages
• Fifth Generation Languages
First Generation Language
• Machine language
o Operation code – such as addition or subtraction.
o Operands – that identify the data to be processed.
o Machine language is machine dependent as it is the only
language the computer can understand.
o Very efficient code but very difficult to write.
Second Generation Languages
• Assembly languages
o Symbolic operation codes replaced binary operation codes.
o Assembly language programs needed to be “assembled” for
execution by the computer. Each assembly language
instruction is translated into one machine language
instruction.
o Very efficient code and easier to write.
Third Generation Languages
• Closer to English but included simple mathematical notation.
o Programs written in source code which must be translated
into machine language programs called object code.
o The translation of source code to object code is
accomplished by a machine language system program
called a compiler.
• Alternative to compilation is interpretation which is accomplished by
a system program called an interpreter.
• Common third generation languages
o FORTRAN
o COBOL
o C and C++
o Visual Basic
Fourth Generation Languages
• A high level language (4GL) that requires fewer instructions to
accomplish a task than a third generation language.
• Used with databases
o Query languages
o Report generators
o Forms designers
o Application generators
8
By: Gadisa A.
Designing Program Logic
COMPUTER LANGUAGE
Fifth Generation Languages
• Declarative languages
• Functional(?): Lisp, Scheme, SML
o Also called applicative
o Everything is a function
• Logic: Prolog
o Based on mathematical logic
o Rule- or Constraint-based
Beyond Fifth Generation Language
• 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 than stating their question.
• Limited capabilities at present.
The principal paradigm
• Imperative Programming (C)
• Object-Oriented Programming (C++)
• Logic/Declarative Programming (Prolog)
• Functional/Applicative Programming (Lisp)
Traditional Programming Language
• FORTRAN
o Formula Translation.
o Developed at IBM in the mid-1950s.
o Designed for scientific and mathematical applications by
scientists and engineers.
• COBOL
o Common Business Oriented Language.
o Developed in 1959. ◦ Designed to be common to many
different computers.
o Typically used for business applications.
• BASIC
o Beginner’s All-purpose Symbolic Instruction Code.
o Developed at Dartmouth College in mid 1960s.
o Developed as a simple language for students to write
programs with which they could interact through terminals.
• C
o Developed by Bell Laboratories in the early 1970s.
o Provides control and efficiency of assembly language while
having third generation language features.
o Often used for system programs. ◦ UNIX is written in C.
9
By: Gadisa A.
Designing Program Logic
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 structured are based on what the program will have
to do rather than the components of the computer it will be used
with.
• This means that programs of high level languages are portable.
These programs can be written on one computer and then
executed on another.
• There are 2 classified groups of high level languages
o Imperative: tells the computer how to do things
o Declarative: focuses on what we want to get from the
computer
High Level
Programming
Languages
Imperative
Object
Oriented
Procedural
Declarative
Logic
Database
Query
Functional
10
By: Gadisa A.
Designing Program Logic
ALGORITHM
What is an algorithm?
• A set of steps to accomplish a task
• An algorithm is a sequence of unambiguous instruction
• An algorithm is a well-defined procedure that allows a
computer to solved a problem.
• An algorithm is a self-contained step-by-step set of
operation to be performed to solve a specific problem.
How to write an algorithm?
• Keep in mind that algorithm is a step-by-step process.
• Depending upon programming language, include syntax
where necessary.
• Include variables and their usage.
• If they are any loops, try to give sub number lists.
• Try to give go back to step number if loop or condition fails.
• 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.
Characteristics of an Algorithm
• Unambiguous − Algorithm should be clear and
unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only
one meaning.
• Input − An algorithm should have 0 or more well-defined
inputs.
• Output − An algorithm should have 1 or more well-
defined outputs, and should match the desired output.
• Finiteness − Algorithms must terminate after a finite
number of steps.
• Feasibility − Should be feasible with the available
resources.
• Independent − An algorithm should have step-by-step
directions, which should be independent of any
programming code.
Two forms of Algorithm:
• Pseudocode
• Flowchart
11
By: Gadisa A.
Designing Program Logic
ALGORITHM: PSEUDOCODE
Pseudocode
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 pseudocode are as
follows.
Pseudocode Guidelines
• The keywords used for pseudocode in this document are:
1. for start and finish
BEGIN MAINPROGRAM, END MAINPROGRAM
2. for initialization
INITIALISATION, END INITIALISATION
3. for subprogram
BEGIN SUBPROGRAM. END SUBPROGRAM
4. for selection
IF. THEN, ELSE, ENDIF
5. for multi-way selection
CASEWHERE. OTHERWISE. ENDCASE
6. for pre-test repetition
WHILE. ENDWHILE
7. for post-test repetition
REPEAT. UNTIL
• Keywords are written in CAPITALS.
• Structural elements come in pairs,
o for every BEGIN there is an END
o for every IF there is an ENDIF. etc.
• Indenting is used to show structure in the algorithm.
• The names of subprograms are underlined.
o This means that when refining the solution to a problem. a
word in an algorithm can be underlined and a subprogram
developed.
This feature enables the use of the 'top-down' development
concept. where details for a particular process need only be
considered within the relevant sub-routine.
12
By: Gadisa A.
Designing Program Logic
ALGORITHM: FLOW CHARTS
Flow Charts
• A drawing that uses symbols interconnected with lines to represent the
flow of information
• A process flow diagram that illustrates the successive steps in a process,
procedure, system or model.
• 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
A Flowchart is used for:
1. Defining and analyzing processes
2. Building a step-by-step picture of the process for analysis, discussion, or
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.
Basic flow charting shapes and symbols
Flowcharts use special shapes to represent different types of actions
or steps in a process and Lines and arrows show the sequence of
the steps, and the relationships among them.
Start/End – The terminator symbol marks the starting or ending point
the system. It usually contains the word "Start" or "End."
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.
Decision – A decision or branching point. Lines representing
different decisions emerge from different points of the diamond.
13
By: Gadisa A.
Designing Program Logic
ALGORITHM: FLOW CHARTS
Connector – Indicates that the flow continues where a matching
symbol 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
Example of Flowchart
14
By: Gadisa A.
Designing Program Logic
ALGORITHM: FLOW CHARTS
Common Types of Flowcharts
There are four flowchart types that are particularly versatile. These
four common diagrams are great for describing business,
manufacturing, or administrative processes, seeing how an
organization functions, or how different departments work together.
1. The Process Flowchart
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 the four commonly used flowchart types
because it can be applied to virtually anything.
• Map out roles and responsibilities within an organization to
gain clarity.
2. The Workflow Chart
Understand How Data and Documents Flow Within Your
Organization
A workflow chart shows the way a business or process functions.
This type of workflow diagram can be used to:
• Train new employees
• Discover potential problem areas
• Clarify business operations by showing a high-level overview
3. The Swim lane Flowchart
Describe How Separate Departments, Processes or Employees
Interact
The swim lane flowchart comes in handy when you need to show
multiple flows of information side by side.
A swim lane flowchart or diagram is great for documenting a whole
process that interacts with different segments of an organization or
requires collaboration among different teams.
4. The Data Flowchart
See Where Data Flows In and Out of an Information System with a
Data Flow Diagram
A data flowchart or data flow diagram shows the way data is
processed. It comes in handy when you want to design or analyze a
system. Although most 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.
15
By: Gadisa A.
Designing Program Logic
ALGORITHM: FLOW CHARTS
Steps for creating a flowchart are:
1. Familiarize the participants with the flowchart symbols
2. Brainstorm major process tasks. Ask questions such as
"What really happens next in the process? ", "Does a
decision need to be made before the next step? ", or
What approvals are required before moving on to the
next task?"
3. Draw the process flowchart using the symbols on a flip
chart or overhead transparency.
Every process will have a start and an end (shown by
elongated circles). All processes will have tasks and most
will have decision points (shown by a diamond).
CONSTRUCTION/INTERPRETATION tip for a flow chart.
• Define the boundaries of the process clearly.
• Use the simplest symbols possible.
• Make sure every feedback loop has an escape.
• Analyze flow chart of actual process.
• Analyze flow chart of best process.
• Compare both charts, looking for areas where they are
different. Most of the time, the stages where differences
occur is considered to be the problem area or process.
• Take appropriate in-house steps to correct the
differences between the two separate flows.
16
By: Gadisa A.
Designing Program Logic
ALGORITHM: FLOW CHARTS
Tips for Effective Flowcharts
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.
Flowchart and Pseudocode
17
By: Gadisa A.
Designing Program Logic
DATA TYPES
Data Types
• A data type defines a set of values and a set of operation on
those values. A C++ program may need to required different
type of data. Each type required different type of memory.
• It indicates the type of data that can be store in a variable
• There are three basic data type.
o Character
o Integer
o Float
Character data type
• This data type is used to store character values.
• Characters may include letters, digits and punctuation
character. It take one byte in memory i.e. ‘a’ and ‘@’ etc.
• Represented by the “char” data type.
We can declare it as below.
Char a= ‘A’
Char f= ‘B’
Char z= ‘D’ etc
Integer data type:
• A numeric value with no fraction is called integer data. It include
both positive and negative values.
• Represented by “int”
• Int range –32,768 to 32,767
• Some example of integer value are 56 and -12
• Int size is ‘2’ bytes
• Long int size ‘4’ bytes
We can declare it as below.
Int a=20;
Int b=40;
Int c=30;
18
By: Gadisa A.
Designing Program Logic
DATA TYPES
Type of Integer data type
Int data type
• To store integer values and takes two byte in memory. i.e.
int num;
Long int data type
• To store large integer value. It has greater range and takes ‘4’
bytes in memory. 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.
Type of float
• Float data type
o This data type is used to store real values and
o Takes ‘4’ bytes in memory. i.e. Float num;
• Double data type
o It has great range as compared to float and takes ‘8’ byte
in memory. i.e. Double num;
19
By: Gadisa A.
Designing Program Logic
DATA TYPES
Variable
• A quantity whose values may change during the execution of the
program is called variable.
• It may be a numeric or non-numeric quantity
• It represented by an identifier called variable name
• A variable represents a memory location in the computer memory.
Data is store into the memory location. The name of memory location
or variable name remains fixed during the execution of the program
but the data store in that location may change from time to time
Variable Declaration
• A variable is case sensitive, that is, uppercase variables are treated
differently
• Variable “SUM” is different from variable “sum”
• Variable “NetPay” is different from variable “NETpay”
Rules for variable Declaration
• Variable may include letter, number and underscore (_)
• The first latter of the variable must be character or underscore.
• Blank spaces special symbols, commas are not used
• A variable can be ‘31’ character long for many compilers
• Key word are not used as variable name.
Variable Declaration
• The process of specifying the name and its data type is called
variable declaration
• All variables that are used in a program are declared using
variable declaration statement.
• When a variable is declared a certain number of bytes in the
memory are allocated to the variable name.
Variable Initialization
• The process of assigning a value to a variable at the time of
declaration is known as variable initialization.
• The equal (=) is used to initialize a variable.
• Variable name is given on the left side and value is given on the
right side of equal sign.
20
By: Gadisa A.
Designing Programming Logic
if Statement
‘if’ statements test whether a certain condition has been reached,
such as whether a number has reaches a certain value, and if true,
executes the statements in the brackets.
The code in the brackets is skipped if the statement is false.
The syntax for if statement is:
void loop(){
if(some variable <test condition> value){
Do Something;
}
}
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.
STRUCTURE: CONTROL STRUCTURE
If…else
The ‘if…else’ statements allow either/or statements to be made.
These statements can be used when more than one courses of
action are possible, as determined by the variable or value being
tested. The syntax for ‘if…else’ implementation is:
void loop(){
if(inputPin == HIGH){
doThing1;
}else{
doThing2;
}
}
21
By: Gadisa A.
Designing Programming Logic
STRUCTURE: CONTROL STRUCTURE
If…else if ladder
If more than two courses of action are possible, then the
‘if…else if’ ladder is to be used, which allows the choice
and execution of one statement from an infinite number of
possibilities. The syntax is as follows:
void loop(){
if(input Pin <test condition> value1){
doThing1;
}else if(input Pin <test condition> value2)
{
doThing2;
}else if(input Pin <test condition> value3)
{
doThing3;
}else{
doThing4;
}
}
for Statement
The ‘for’ statement is used to repeat a statement of a block
of statements a specified number of times. The arguments for
the ‘for’ loop consists of:
1. Initialization. This initializes a variable with a specified initial
value.
2. Test condition. This condition is tested for every loop and
the ‘for’ statement stops looping when this condition does
not hold true.
3. Increment/decrement expression. This expression operates
on the initialized variable.
The syntax for the ‘for’ loop is as follows:
void loop(){
for(initialization; condition;
increment/decrement expression){
Do Something;
}
}
22
By: Gadisa A.
Designing Programming Logic
STRUCTURE: CONTROL STRUCTURE
while Loop
The ‘while’ loop will loop continuously and infinitely, until
the expression inside the brackets become false. If the
tested variable of value is not changed, the loop will go on
for infinity.
void loop(){
while(variable <test condition> value){
Do Something;
}
}
do…while Loop
The ‘do…while’ class works in a similar manner to the
‘while’ class, the only difference being, that, in the ‘do…
while’ class the condition is tested at the bottom of the loop,
making sure the loops is carried out at least once.
void loop(){
do{
Do Something;
}while(variable <test condition> value)
}
23
By: Gadisa A.
Designing Programming Logic
FUNCTIONS: MATH
min(x,y)
Returns the smaller of the two values passed into the
function as arguments.
void loop(){
int x,y;
int smaller Num = min(x,y); //Sets
smaller Num equal //to x or y
depending upon //whichever is smaller
}
max(x,y)
Returns the larger of the two values passed into the
function as arguments.
void loop(){
int x,y;
int larger Num = max(x,y); //Sets
larger Num equal to
//x or y depending upon
//whichever is
larger
}
24
By: Gadisa A.
Designing Programming Logic
FUNCTIONS: RANDOM NUMBERS
randomSeed(seed)
Sets a value as a starting point before the random() function. The
seed can be any variable, input, constraints or other functions. Must
be used before the random function.
void loop(){
randomSeed(analogRead(3)); //Uses the analog value at
//pin 3 to set a
starting //point for the
random //function
}
random(min,max)
This function helps in generating pseudo-random values for the
given range in the arguments. The function randomSeed(seed)
must be used before using this function.
void loop(){
int value_1;
int value_2;
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
}
25
By: Gadisa A.
Designing Programming Logic
REFERENCE
https://www.arduino.cc/
https://en.wikipedia.org/wiki/Arduino
https://www.slideshare.net/

More Related Content

Similar to Introductions to Design Logic.pptx IT level (20)

PDF
Introduction to computer programming
VanessaBuensalida
 
PPTX
programming.pptx
DarianElmyra
 
PPTX
Introduction to Computer, Programming languages , Networks and Internet.pptx
SheharBano86
 
PDF
Introduction To Programming In C Language C Programming Best C Programming La...
simanuaderli
 
PPTX
Programming languages,compiler,interpreter,softwares
Nisarg Amin
 
PDF
Computer programming programming_langugages
eShikshak
 
PPTX
Programming languages.pptx
Christ Association
 
PPTX
For EETggggggggggggggggggggggggggggggggggg
narrowide762
 
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy1.pdf
amanpathak160605
 
PPSX
Ic lecture8
AttaullahRahimoon
 
PPT
introduction to computer programming CPPL1.ppt
biniyamtiktok
 
PPTX
Programming Paradigm & Languages
Gaditek
 
PPTX
Programming Paradigm & Languages
Gaditek
 
PPTX
Programming Fundamentals and Programming Languages Concepts
imtiazalijoono
 
PPTX
Introduction_to_Programming.pptx
PmarkNorcio
 
PPTX
Programming languages
sushma chinta
 
PPTX
Python-L1.pptx
DukeCalvin
 
PPTX
COM 113 INTRO TO COMPUTER PROGRAMMING-PRT2.pptx
IorlahaSamuel1
 
PPTX
COM 113 INTRO TO COMPUTER PROGRAMMING-PRT2.pptx
IorlahaSamuel1
 
Introduction to computer programming
VanessaBuensalida
 
programming.pptx
DarianElmyra
 
Introduction to Computer, Programming languages , Networks and Internet.pptx
SheharBano86
 
Introduction To Programming In C Language C Programming Best C Programming La...
simanuaderli
 
Programming languages,compiler,interpreter,softwares
Nisarg Amin
 
Computer programming programming_langugages
eShikshak
 
Programming languages.pptx
Christ Association
 
For EETggggggggggggggggggggggggggggggggggg
narrowide762
 
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy1.pdf
amanpathak160605
 
Ic lecture8
AttaullahRahimoon
 
introduction to computer programming CPPL1.ppt
biniyamtiktok
 
Programming Paradigm & Languages
Gaditek
 
Programming Paradigm & Languages
Gaditek
 
Programming Fundamentals and Programming Languages Concepts
imtiazalijoono
 
Introduction_to_Programming.pptx
PmarkNorcio
 
Programming languages
sushma chinta
 
Python-L1.pptx
DukeCalvin
 
COM 113 INTRO TO COMPUTER PROGRAMMING-PRT2.pptx
IorlahaSamuel1
 
COM 113 INTRO TO COMPUTER PROGRAMMING-PRT2.pptx
IorlahaSamuel1
 

More from gadisaadamu101 (9)

PPTX
chapter Three artificial intelligence 1.pptx
gadisaadamu101
 
PPTX
Chapter Four Deep Learning artificial intelligence .pptx
gadisaadamu101
 
PPTX
Software Development Life Cycle and Models.pptx
gadisaadamu101
 
PPTX
Introduction to Project Planning chapter 1.pptx
gadisaadamu101
 
PPTX
introduction to software SDLC and Models.pptx
gadisaadamu101
 
PPT
Chapter Five: Introduction to Syncho.pptduction to Syncho.ppt
gadisaadamu101
 
PPT
Network and system administration Chapter 5.pptxChapter 6.ppt
gadisaadamu101
 
PPTX
Network and system administration Chapter 5.pptx
gadisaadamu101
 
PPT
network and system admistration by Chapter 4.ppt
gadisaadamu101
 
chapter Three artificial intelligence 1.pptx
gadisaadamu101
 
Chapter Four Deep Learning artificial intelligence .pptx
gadisaadamu101
 
Software Development Life Cycle and Models.pptx
gadisaadamu101
 
Introduction to Project Planning chapter 1.pptx
gadisaadamu101
 
introduction to software SDLC and Models.pptx
gadisaadamu101
 
Chapter Five: Introduction to Syncho.pptduction to Syncho.ppt
gadisaadamu101
 
Network and system administration Chapter 5.pptxChapter 6.ppt
gadisaadamu101
 
Network and system administration Chapter 5.pptx
gadisaadamu101
 
network and system admistration by Chapter 4.ppt
gadisaadamu101
 
Ad

Recently uploaded (20)

PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
Design Thinking basics for Engineers.pdf
CMR University
 
Day2 B2 Best.pptx
helenjenefa1
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Thermal runway and thermal stability.pptx
godow93766
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Ad

Introductions to Design Logic.pptx IT level

  • 1. 1 Designing ICT Department Rift Valley University Programming Logic
  • 2. 2 By: Gadisa A. Designing Program Logic INTRODUCTION OF COMPUTER PROGRAMMING Computer Program – Is a list of instructions that the computer machine follows to properly accept input, correctly process them and present the results in the most understandable way. Computer Programming – is considered to be an art and at the same time a science. It is an art because there is no standard way to interpret a problem and solve it using a standard form of programming procedures and styles. It is fast becoming a science because standard programming practice is starting to be adopted. Programs fall into two major classes: • Application Programs – application program is one that carries out some function directly for a user, such as word processing or game-playing. • Operating Systems – operating system is a program that manages the computer and the various resources and devices connected to it, such as RAM (random access memory), hard drives, monitors, keyboards, printers, and modems, so that they may be used by other programs. Three types of applications programs translate from source code to object code: • Compilers – Computer software that translates (compiles) source code written in a high-level language (e.g., C++) into a set of machine- language instructions that can be understood by a digital computer's CPU. • Interpreters – Is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program. • Assemblers – Is a program that converts assembly language into machine code. It takes the basic commands and operations from assembly code and converts them into binary code that can be recognized by a specific type of processor. Assemblers are similar to compilers in that they produce executable code
  • 3. 3 By: Gadisa A. Designing Program Logic INTRODUCTION OF COMPUTER PROGRAMMING Computer is composed of: 1. CPU (Central Processing Unit) – this is where all mathematical and logical forms of processing are done on data. 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. 3. Input and Output Device – data have to come into the computer by first entering them through input devices. Ex. Keyboard. After the computer executes a program to process data and produce information, this information remains in the memory. Part of the program has to show these results to the outside world. Output devices such as monitor screen and printers are used to display the results to the user. 4.Software – computers today come bundled with some software already installed in them. Operating System – performs for the programmer many tasks that used to be very difficult to program 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. The following are standard Operating System functions. • Validating user identification every time the user uses the computer • Provides standard functions like editor, compilers and linkers • Manages files of the user by providing functions like Copy, Delete, Append • Retrieves data out of data files • Manages memory for the user by determining where in the memory a program is to be loaded every time it is called to run • Makes communication between the CPU, Memory and input/output devices transparent to the user or the user are unaware of these things while they are at work.
  • 4. 4 By: Gadisa A. Designing Program Logic COMPUTER LANGUAGE Programming Language • A programming language is a set of rules that provides a way of telling a computer what operations to perform. • A programming language is a set of rules for communicating an algorithm • It provides a linguistic framework for describing computations • The grammatical rules are called syntax. • Each programming language has a different set of syntax rules. Why are there so many programming languages? • Programming languages have evolved over time as better ways 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. Classification of Programming Language Programming Languages - Generations • High level languages o 4th Generation Data query, analysis and reporting o 3rd Generation Imperative • Low level languages o 2nd Generation Assembly o 1st Generation Machine code
  • 5. 5 By: Gadisa A. Designing Program Logic COMPUTER PROGRAMMING High level languages • Programming languages that are closer to human language than to machine language. They are characterized to use common English words as their instructions. Aside from that, high level languages have the following characteristics: • A compiled program is usually not as efficient as when the program was originally written in machine language or assembly. Each of these programming languages was designed to solve particular kinds of problems. • COBOL – Common Business Oriented Language was designed to solve business problems like accounting. • FORTRAN – Formula Translation is a high level language used for scientific and engineering applications. • Pascal and C – are general-purpose language. They are high- level language but they can be embedded with assembly language code effectively making these languages middle-level languages. C language is responsible in the implementation of major software including operating systems, compilers and software development tools The Simplest computer programs perform three basic operations: Get the input from the keyboard. Process the input data. Display the results on the screen. INPUT DATA PROCESS DATA DISPLAY ALL RESULT
  • 6. 6 By: Gadisa A. Designing Program Logic COMPUTER LANGUAGE Low-Level Language: • Machine code and Assembly code High-Level Language: • A programming language that allows programs to be written using English keywords and is platform independent 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. • Disadvantages o More difficult to understand, maintain and debug than high level languages. o More difficult to write without making errors. o Machine dependent making it difficult to port to a different instruction set processor Memory addresses and operations have to be remembered Low Level Languages.
  • 7. 7 By: Gadisa A. Designing Program Logic COMPUTER LANGUAGE What are the types of programming languages? • First Generation Languages • Second Generation Languages • Third Generation Languages • Fourth Generation Languages • Fifth Generation Languages First Generation Language • Machine language o Operation code – such as addition or subtraction. o Operands – that identify the data to be processed. o Machine language is machine dependent as it is the only language the computer can understand. o Very efficient code but very difficult to write. Second Generation Languages • Assembly languages o Symbolic operation codes replaced binary operation codes. o Assembly language programs needed to be “assembled” for execution by the computer. Each assembly language instruction is translated into one machine language instruction. o Very efficient code and easier to write. Third Generation Languages • Closer to English but included simple mathematical notation. o Programs written in source code which must be translated into machine language programs called object code. o The translation of source code to object code is accomplished by a machine language system program called a compiler. • Alternative to compilation is interpretation which is accomplished by a system program called an interpreter. • Common third generation languages o FORTRAN o COBOL o C and C++ o Visual Basic Fourth Generation Languages • A high level language (4GL) that requires fewer instructions to accomplish a task than a third generation language. • Used with databases o Query languages o Report generators o Forms designers o Application generators
  • 8. 8 By: Gadisa A. Designing Program Logic COMPUTER LANGUAGE Fifth Generation Languages • Declarative languages • Functional(?): Lisp, Scheme, SML o Also called applicative o Everything is a function • Logic: Prolog o Based on mathematical logic o Rule- or Constraint-based Beyond Fifth Generation Language • 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 than stating their question. • Limited capabilities at present. The principal paradigm • Imperative Programming (C) • Object-Oriented Programming (C++) • Logic/Declarative Programming (Prolog) • Functional/Applicative Programming (Lisp) Traditional Programming Language • FORTRAN o Formula Translation. o Developed at IBM in the mid-1950s. o Designed for scientific and mathematical applications by scientists and engineers. • COBOL o Common Business Oriented Language. o Developed in 1959. ◦ Designed to be common to many different computers. o Typically used for business applications. • BASIC o Beginner’s All-purpose Symbolic Instruction Code. o Developed at Dartmouth College in mid 1960s. o Developed as a simple language for students to write programs with which they could interact through terminals. • C o Developed by Bell Laboratories in the early 1970s. o Provides control and efficiency of assembly language while having third generation language features. o Often used for system programs. ◦ UNIX is written in C.
  • 9. 9 By: Gadisa A. Designing Program Logic 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 structured are based on what the program will have to do rather than the components of the computer it will be used with. • This means that programs of high level languages are portable. These programs can be written on one computer and then executed on another. • There are 2 classified groups of high level languages o Imperative: tells the computer how to do things o Declarative: focuses on what we want to get from the computer High Level Programming Languages Imperative Object Oriented Procedural Declarative Logic Database Query Functional
  • 10. 10 By: Gadisa A. Designing Program Logic ALGORITHM What is an algorithm? • A set of steps to accomplish a task • An algorithm is a sequence of unambiguous instruction • An algorithm is a well-defined procedure that allows a computer to solved a problem. • An algorithm is a self-contained step-by-step set of operation to be performed to solve a specific problem. How to write an algorithm? • Keep in mind that algorithm is a step-by-step process. • Depending upon programming language, include syntax where necessary. • Include variables and their usage. • If they are any loops, try to give sub number lists. • Try to give go back to step number if loop or condition fails. • 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. Characteristics of an Algorithm • Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning. • Input − An algorithm should have 0 or more well-defined inputs. • Output − An algorithm should have 1 or more well- defined outputs, and should match the desired output. • Finiteness − Algorithms must terminate after a finite number of steps. • Feasibility − Should be feasible with the available resources. • Independent − An algorithm should have step-by-step directions, which should be independent of any programming code. Two forms of Algorithm: • Pseudocode • Flowchart
  • 11. 11 By: Gadisa A. Designing Program Logic ALGORITHM: PSEUDOCODE Pseudocode 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 pseudocode are as follows. Pseudocode Guidelines • The keywords used for pseudocode in this document are: 1. for start and finish BEGIN MAINPROGRAM, END MAINPROGRAM 2. for initialization INITIALISATION, END INITIALISATION 3. for subprogram BEGIN SUBPROGRAM. END SUBPROGRAM 4. for selection IF. THEN, ELSE, ENDIF 5. for multi-way selection CASEWHERE. OTHERWISE. ENDCASE 6. for pre-test repetition WHILE. ENDWHILE 7. for post-test repetition REPEAT. UNTIL • Keywords are written in CAPITALS. • Structural elements come in pairs, o for every BEGIN there is an END o for every IF there is an ENDIF. etc. • Indenting is used to show structure in the algorithm. • The names of subprograms are underlined. o This means that when refining the solution to a problem. a word in an algorithm can be underlined and a subprogram developed. This feature enables the use of the 'top-down' development concept. where details for a particular process need only be considered within the relevant sub-routine.
  • 12. 12 By: Gadisa A. Designing Program Logic ALGORITHM: FLOW CHARTS Flow Charts • A drawing that uses symbols interconnected with lines to represent the flow of information • A process flow diagram that illustrates the successive steps in a process, procedure, system or model. • 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 A Flowchart is used for: 1. Defining and analyzing processes 2. Building a step-by-step picture of the process for analysis, discussion, or 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. Basic flow charting shapes and symbols Flowcharts use special shapes to represent different types of actions or steps in a process and Lines and arrows show the sequence of the steps, and the relationships among them. Start/End – The terminator symbol marks the starting or ending point the system. It usually contains the word "Start" or "End." 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. Decision – A decision or branching point. Lines representing different decisions emerge from different points of the diamond.
  • 13. 13 By: Gadisa A. Designing Program Logic ALGORITHM: FLOW CHARTS Connector – Indicates that the flow continues where a matching symbol 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 Example of Flowchart
  • 14. 14 By: Gadisa A. Designing Program Logic ALGORITHM: FLOW CHARTS Common Types of Flowcharts There are four flowchart types that are particularly versatile. These four common diagrams are great for describing business, manufacturing, or administrative processes, seeing how an organization functions, or how different departments work together. 1. The Process Flowchart 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 the four commonly used flowchart types because it can be applied to virtually anything. • Map out roles and responsibilities within an organization to gain clarity. 2. The Workflow Chart Understand How Data and Documents Flow Within Your Organization A workflow chart shows the way a business or process functions. This type of workflow diagram can be used to: • Train new employees • Discover potential problem areas • Clarify business operations by showing a high-level overview 3. The Swim lane Flowchart Describe How Separate Departments, Processes or Employees Interact The swim lane flowchart comes in handy when you need to show multiple flows of information side by side. A swim lane flowchart or diagram is great for documenting a whole process that interacts with different segments of an organization or requires collaboration among different teams. 4. The Data Flowchart See Where Data Flows In and Out of an Information System with a Data Flow Diagram A data flowchart or data flow diagram shows the way data is processed. It comes in handy when you want to design or analyze a system. Although most 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.
  • 15. 15 By: Gadisa A. Designing Program Logic ALGORITHM: FLOW CHARTS Steps for creating a flowchart are: 1. Familiarize the participants with the flowchart symbols 2. Brainstorm major process tasks. Ask questions such as "What really happens next in the process? ", "Does a decision need to be made before the next step? ", or What approvals are required before moving on to the next task?" 3. Draw the process flowchart using the symbols on a flip chart or overhead transparency. Every process will have a start and an end (shown by elongated circles). All processes will have tasks and most will have decision points (shown by a diamond). CONSTRUCTION/INTERPRETATION tip for a flow chart. • Define the boundaries of the process clearly. • Use the simplest symbols possible. • Make sure every feedback loop has an escape. • Analyze flow chart of actual process. • Analyze flow chart of best process. • Compare both charts, looking for areas where they are different. Most of the time, the stages where differences occur is considered to be the problem area or process. • Take appropriate in-house steps to correct the differences between the two separate flows.
  • 16. 16 By: Gadisa A. Designing Program Logic ALGORITHM: FLOW CHARTS Tips for Effective Flowcharts 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. Flowchart and Pseudocode
  • 17. 17 By: Gadisa A. Designing Program Logic DATA TYPES Data Types • A data type defines a set of values and a set of operation on those values. A C++ program may need to required different type of data. Each type required different type of memory. • It indicates the type of data that can be store in a variable • There are three basic data type. o Character o Integer o Float Character data type • This data type is used to store character values. • Characters may include letters, digits and punctuation character. It take one byte in memory i.e. ‘a’ and ‘@’ etc. • Represented by the “char” data type. We can declare it as below. Char a= ‘A’ Char f= ‘B’ Char z= ‘D’ etc Integer data type: • A numeric value with no fraction is called integer data. It include both positive and negative values. • Represented by “int” • Int range –32,768 to 32,767 • Some example of integer value are 56 and -12 • Int size is ‘2’ bytes • Long int size ‘4’ bytes We can declare it as below. Int a=20; Int b=40; Int c=30;
  • 18. 18 By: Gadisa A. Designing Program Logic DATA TYPES Type of Integer data type Int data type • To store integer values and takes two byte in memory. i.e. int num; Long int data type • To store large integer value. It has greater range and takes ‘4’ bytes in memory. 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. Type of float • Float data type o This data type is used to store real values and o Takes ‘4’ bytes in memory. i.e. Float num; • Double data type o It has great range as compared to float and takes ‘8’ byte in memory. i.e. Double num;
  • 19. 19 By: Gadisa A. Designing Program Logic DATA TYPES Variable • A quantity whose values may change during the execution of the program is called variable. • It may be a numeric or non-numeric quantity • It represented by an identifier called variable name • A variable represents a memory location in the computer memory. Data is store into the memory location. The name of memory location or variable name remains fixed during the execution of the program but the data store in that location may change from time to time Variable Declaration • A variable is case sensitive, that is, uppercase variables are treated differently • Variable “SUM” is different from variable “sum” • Variable “NetPay” is different from variable “NETpay” Rules for variable Declaration • Variable may include letter, number and underscore (_) • The first latter of the variable must be character or underscore. • Blank spaces special symbols, commas are not used • A variable can be ‘31’ character long for many compilers • Key word are not used as variable name. Variable Declaration • The process of specifying the name and its data type is called variable declaration • All variables that are used in a program are declared using variable declaration statement. • When a variable is declared a certain number of bytes in the memory are allocated to the variable name. Variable Initialization • The process of assigning a value to a variable at the time of declaration is known as variable initialization. • The equal (=) is used to initialize a variable. • Variable name is given on the left side and value is given on the right side of equal sign.
  • 20. 20 By: Gadisa A. Designing Programming Logic if Statement ‘if’ statements test whether a certain condition has been reached, such as whether a number has reaches a certain value, and if true, executes the statements in the brackets. The code in the brackets is skipped if the statement is false. The syntax for if statement is: void loop(){ if(some variable <test condition> value){ Do Something; } } 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. STRUCTURE: CONTROL STRUCTURE If…else The ‘if…else’ statements allow either/or statements to be made. These statements can be used when more than one courses of action are possible, as determined by the variable or value being tested. The syntax for ‘if…else’ implementation is: void loop(){ if(inputPin == HIGH){ doThing1; }else{ doThing2; } }
  • 21. 21 By: Gadisa A. Designing Programming Logic STRUCTURE: CONTROL STRUCTURE If…else if ladder If more than two courses of action are possible, then the ‘if…else if’ ladder is to be used, which allows the choice and execution of one statement from an infinite number of possibilities. The syntax is as follows: void loop(){ if(input Pin <test condition> value1){ doThing1; }else if(input Pin <test condition> value2) { doThing2; }else if(input Pin <test condition> value3) { doThing3; }else{ doThing4; } } for Statement The ‘for’ statement is used to repeat a statement of a block of statements a specified number of times. The arguments for the ‘for’ loop consists of: 1. Initialization. This initializes a variable with a specified initial value. 2. Test condition. This condition is tested for every loop and the ‘for’ statement stops looping when this condition does not hold true. 3. Increment/decrement expression. This expression operates on the initialized variable. The syntax for the ‘for’ loop is as follows: void loop(){ for(initialization; condition; increment/decrement expression){ Do Something; } }
  • 22. 22 By: Gadisa A. Designing Programming Logic STRUCTURE: CONTROL STRUCTURE while Loop The ‘while’ loop will loop continuously and infinitely, until the expression inside the brackets become false. If the tested variable of value is not changed, the loop will go on for infinity. void loop(){ while(variable <test condition> value){ Do Something; } } do…while Loop The ‘do…while’ class works in a similar manner to the ‘while’ class, the only difference being, that, in the ‘do… while’ class the condition is tested at the bottom of the loop, making sure the loops is carried out at least once. void loop(){ do{ Do Something; }while(variable <test condition> value) }
  • 23. 23 By: Gadisa A. Designing Programming Logic FUNCTIONS: MATH min(x,y) Returns the smaller of the two values passed into the function as arguments. void loop(){ int x,y; int smaller Num = min(x,y); //Sets smaller Num equal //to x or y depending upon //whichever is smaller } max(x,y) Returns the larger of the two values passed into the function as arguments. void loop(){ int x,y; int larger Num = max(x,y); //Sets larger Num equal to //x or y depending upon //whichever is larger }
  • 24. 24 By: Gadisa A. Designing Programming Logic FUNCTIONS: RANDOM NUMBERS randomSeed(seed) Sets a value as a starting point before the random() function. The seed can be any variable, input, constraints or other functions. Must be used before the random function. void loop(){ randomSeed(analogRead(3)); //Uses the analog value at //pin 3 to set a starting //point for the random //function } random(min,max) This function helps in generating pseudo-random values for the given range in the arguments. The function randomSeed(seed) must be used before using this function. void loop(){ int value_1; int value_2; 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 }
  • 25. 25 By: Gadisa A. Designing Programming Logic REFERENCE https://www.arduino.cc/ https://en.wikipedia.org/wiki/Arduino https://www.slideshare.net/