Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Programming Logics

Programming Logics
• Simple Branching
• Looping
• Recursion
• Coupling
• Cohesion
Simple Branching
• Branching is deciding what actions to take.
• Branching is so called because the program
chooses to follow one branch or another.
if statement
• This is the most simple form of the branching
statements.
• It takes an expression in parenthesis and an
statement or block of statements.
• if the expression is true then the statement or
block of statements gets executed otherwise these
statements are skipped.
Simple Branching
if statement
if (expression)
statement;
or
if (expression)
{
Block of statements;
}
Simple Branching
if statement
or
if (expression)
{
Block of statements;
}
else
{
Block of statements;
}
Simple Branching
Other branching statements
• ? : Operator
• switch statement:
• break keyword
Looping
• Loops provide a way to repeat commands and control how many
times they are repeated.

• In computer programming, a loop is a sequence of instruction s


that is continually repeated until a certain condition is reached.

• A loop is a fundamental programming idea that is commonly


used in writing programs.

• An infinite loop is one that lacks a functioning exit routine.

• The result is that the loop repeats continually until the operating
system senses it and terminates the program with an error or
until some other event occurs
Looping
Looping
Looping Statements:
• While loop
• For loop
• Do – while loop
Recursion
• The process in which a function calls itself directly or
indirectly is called recursion and the corresponding
function is called a recursive function.

• Using a recursive algorithm, certain problems can be


solved quite easily.

• A recursive function solves a particular problem by


calling a copy of itself and solving smaller sub-problems
of the original problems. 

• It is essential to know that we should provide a certain


case in order to terminate this recursion process.
Recursion
• The process in which a function calls itself directly or
indirectly is called recursion and the corresponding
function is called a recursive function.

• Using a recursive algorithm, certain problems can be


solved quite easily.

• A recursive function solves a particular problem by


calling a copy of itself and solving smaller sub-problems
of the original problems. 

• It is essential to know that we should provide a certain


case in order to terminate this recursion process.
Cohesion
• The measure of how strongly the elements are related
functionally inside a module is called cohesion.

• The elements inside a module can be instructions,


groups of instructions, definition of data, call from
another module etc. 

• When dividing a system into modules, it must ensure


that the activities within the module are tightly bound to
one another.

• This strong relations reduces modules minimize


coupling.
Cohesion
There are seven types of cohesion:
1. Functional cohesion
2. Sequential cohesion
3. Communicational cohesion
4. Procedural cohesion
5. Temporal cohesion
6. Logical cohesion
7. Coincidental cohesion
Functional Cohesion
• Functional cohesion occurs when elements of a module are grouped
together because they are united for a single, well-defined purpose.

• All of the elements in the module work together to fulfil that


purpose.

• Functional cohesion in a module is ideal and is the highest type of


cohesion.

• Functional cohesion promotes the reusability of a module and


makes it easier to maintain.

• Some of the examples of functional cohesion are read transaction


record, seat assignment to an airline passenger etc.
Sequential Cohesion
• In this scheme of cohesion, modules are divided
into a series of activities such that the output of
one module becomes the input to the next
module and the chain continues.
Communicational cohesion:
• This form of cohesion relates to a situation where
all modules share some common data.
• This form of cohesion has clearly defined
boundaries, inputs, and outputs.
• Example- update record in the database and send
it to the printer.
Sequential Cohesion
• In this scheme of cohesion, modules are divided
into a series of activities such that the output of
one module becomes the input to the next
module and the chain continues.
Communicational cohesion:
• This form of cohesion relates to a situation where
all modules share some common data.
• This form of cohesion has clearly defined
boundaries, inputs, and outputs.
• Example- update record in the database and send
it to the printer.

You might also like