You are on page 1of 27

Problem Solving with decisions

Decision Structures allow computers to choose one action out of the two
Flowchart :
A condition can be one of four things:
• A logical expression : an expression that uses
logical operators (AND, OR, and NOT)
• An expression using relational operators: greater
than, less than, greater than or equal to, less than or
equal to, equal to, and not equal to
• A variable of the logical data type (True or False)
• A combination of logical, relational, and
mathematical operators.
Logical operators are used to link more than one
condition together
Nested If/Then/Else Instructions

Write a program to calculate salary of an Employee. If he is a Regular worker, he gats regular


Salary else he is paid on Hourly basis. In that too, if( Hours > 40) pay additional over time as: for
every Hours he is paid 1.5 times normal rate

Input = Number of hours he/she has worked (Hours), and R (per hour)
Analysis:
• Normal payment = Rate* Hours
• If overtime then payment = Normal payment + 1.5 (Extra hours *Rate)
= Rate * Hours + 1.5 (Extra hours *Rate)
= Rate * Hours + 1.5 ( (hours-40) * Rate)
For Normal payment Hours=40
= Rate *40 + Rate * 1.5 (Hours – 40)
Nested If/Then/Else Instructions

Write a program to calculate salary of a worker.


Input = Number of hours he/she has worked (Hours), and R (per hour)
If( Hours > 40) pay additional over time as: for every Hours he is paid 1.5 times normal rate
Analysis:
• Normal payment = Rate* Hours
• If overtime then payment = Normal payment + 1.5 (Extra hours *Rate)
= Rate * Hours + 1.5 (Extra hours *Rate)
= Rate * Hours + 1.5 ( (hours-40) * Rate)
For Normal payment Hours=40
= Rate *40 + Rate * 1.5 (Hours – 40)
= Rate (40 + 1.5 (Hours-40)
Nested If/Then/Else Instructions
Flowchart Pseudocode
Algorithm
Multiple If/Then/Else Instructions

3 Types of Decision logic are:


• Straight through logic
• Positive logic
• Negative logic.
Multiple If/Then/Else Instructions

3 Types of Decision logic are:


• Straight through logic
• Positive logic
• Negative logic.

Straight-through logic :
o All of the decisions are processed sequentially, one after the other.
o There is no Else part of the instructions.
o The False branch always goes to the next decision, and the True branch goes to the next
decision after the instructions for the True branch have been processed.
Straight Through Logic
• Decisions making using straight-through logic, all conditions are tested.
• To test a condition means to process a condition to get a True or False resultant.
• Straight-through logic is the least efficient of all types of decision logic because all the
decisions must be processed.
Example: Ticket rate based on age group
Nested If/Then/Else Instructions
Flowchart Pseudocode
Algorithm
Positive Logic

3 Types of Decision logic are:


• Straight through logic
• Positive logic
• Negative logic.

Positive logic :
o Positive logic is the easiest type for most people to use and understand.
o It is the way we think.
o Positive logic always uses nested If/Then/Else instructions.
o When you use positive logic, you are telling the computer to follow a set of instructions
and continue processing if the condition is True; if the condition is not True, then the
computer processes another decision
Positive Logic
Pseudocode
Algorithm Flowchart
Positive Logic

Develop Algorithm and Flowchart


Positive Logic

IF sales <= 2000


Then
c=0.02
Else
Other calculations of c
Positive Logic

IF sales <= 2000


Then
c=0.02
Else // we are in else because sales is > 2000
If sales < = 4000
Then
c= 0.04
Else
Other calculations of c
Positive Logic

IF sales <= 2000


Then
c=0.02
Else // we are in else because sales is > 2000
If sales < = 4000
Then
c= 0.04
Else // we are here because sales > 2000 and also > 4000
If sales < = 6000
Then
c= 0.08
Else
Other calculation on c
Positive Logic

IF sales <= 2000


Then
c=0.02
Else // we are in else because sales is > 2000
If sales < = 4000
Then
c= 0.04
Else // we are here because sales > 2000 and also > 4000
If sales < = 6000
Then
c= 0.08
Else // we are here because sales > 2000 then > 4000 and now > 6000
c = 0.1
Positive Logic

Flowchart
Positive Logic

Flowchart

Develop various test cases and show correctness.


Negative Logic
• When we use negative logic we are telling the computer to process another decision when the
resultant of the condition is True
• If the resultant is False, then the computer processes a consequent set of instructions and
continues processing the module.
• It is often advantageous to use negative logic to decrease the number of tests or to improve the
readability of a program.
Negative Logic
IF sales <= 2000 IF sales > 6000
Then Then
c=0.02 c=0.1
Else Else // sales is less than 6000
If sales < = 4000 If sales > 6000
Then Then
c= 0.04 c= 0.07
Else Else
If sales < = 6000 If sales > 4000
Then Then
c= 0.08 c= 0.04
Else Else
c = 0.1 c = 0.02
Positive & Negative Logic

Flowchart of Positive Logic Flowchart of Negative logic

True cond False False cond True


ition ition
? ?

Process Process Process


Process
another consequent another
consequent
instruction instruction instruction
instruction

Next instruction Next instruction


Positive & Negative Logic
• Negative logic might be required to (1) Increase readability of the code (2) Reduce number of
operation (comparisons)
IF sales > 6000
• Majority of the activities happen
Then
when condition is FASLE c=0.1
Else // sales is less than 6000
If sales > 6000
Then
c= 0.07
Else
If sales > 4000
Then
c= 0.04
Else
c = 0.02
Negative Logic
Logic Conversion

To convert from positive logic to negative logic or vice versa, do the following:
1. Change all < to > =
2. Change all < to >
3. Change all > to < =
4. Change all >= to <
5. Change all == to <> (Not equal to)
6. Change all < > to == (Not equal to)
7. Interchange all of the Then set of instructions with the corresponding Else set of instructions.
Logic Conversion

Example of Ticket Problem :


Positive logic is
Apply 7 Rules to change it to Negative logic
If age < 16
1. Change all < to > =
Then
If age > = 16
Charge = 7
Then
Else
Charge = 7
If age <65
Else
Then
If age > = 65
Charge =10
Then
Else
Charge =10
Charge = 5
Else
Charge = 5
Logic Conversion

Example of Ticket Problem : Apply 7 Rules to change it Apply 7 Rules to change it


to Negative logic to Negative logic
Positive logic is
7. Swap If – Else activities 7. Swap If – Else activities
If age < 16
If age > = 16 If age > = 16
Then
Then Then
Charge = 7
Charge = 7 Charge = 7
Else
Else Else
If age <65
If age > = 65 If age > = 65
Then
Then Then
Charge =10
Charge =10 Charge =10
Else
Else Else
Charge = 5
Charge = 5 Charge = 5
Logic Conversion

Example of Ticket Problem : Apply 7 Rules to change it Apply 7 Rules to change it


to Negative logic to Negative logic
Positive logic is
7. Swap If – Else activities 7. Swap If – Else activities
If age < 16
If age > = 16 If age > = 16
Then
Then Then
Charge = 7
Charge = 7 If age > = 65
Else
Else Then
If age <65
If age > = 65 Charge =10
Then
Then Else
Charge =10
Charge =10 Charge =5
Else Else
Else
Charge = 5 Charge = 7
Charge = 5
Logic Conversion

Example of Ticket Problem : Apply 7 Rules to change it Apply 7 Rules to change it


to Negative logic to Negative logic
Positive logic is
7. Swap If – Else activities 7. Swap If – Else activities
If age < 16
If age > = 16 If age > = 16
Then
Then Then
Charge = 7
Charge = 7 If age > = 65
Else
Else Then
If age <65
If age > = 65 Charge =5
Then
Then Else
Charge =10
Charge =10 Charge = 10
Else Else
Else
Charge = 5 Charge = 7
Charge = 5

You might also like