Decision Logic Structure Overview Learning Objectives Learning Outcomes

Decision Logic Structure

Overview • • • Learning Objectives Learning Outcomes Overview of sequential logic structure Decision Logic Structures Example Summary

Learning Objectives • To explain data dictionary • To explain decision logic structures • To apply decision logic structures to a real problem

Learning Outcomes • The Students are able to: • To use decision logic structures • To apply decision logic structures to real problem

Introduction Four Logic Structures a. Sequential Structures b. Decision Structures c. Loop Structures d. Case Structures

Sequential Structures - It executes instructions one after another in a sequence. -

Decision Structures - It branches to execute one of two possible sets of the instructions.

Decision Structures

Example of Decision Structures - Assume your are calculating pay at an hourly rate and overtime pay ( over 40 hours) at 1. 5 times the hourly rate. Find final gross pay of an employee.

Decision Structures

Decision Structures - There are three types of decision logic for solution consisting of more than one decision. - 1. Straight trough logic - 2. Positive Logic - 3. Negative Logic

1. Straight Through Logic • Straight trough logic- all of the decisions are processed sequentially, one after the other. • There is no Else part of the instructions • The False branch always goes to the next decision, • The True branch goes to the next decision

Example 1 • find the amount to charge people of varying ages for a concert ticket. • When the person is under 16, the charge is $7; • When the person is 65 or over, the charge is $5; all others are charged $10. The conditions are • the following: • Age < 16 $7 • Age >= 16 and Age < 65 $10 • Age >= 65 $5 •

Example 1 of Straight Through Logic

Python Code of Straight Through Logic age=input(“Enter N: ”) if age< 16: print “Charge 7” if age>=16 and age<65: print “Charge 10” if age>=65: print “Charge 5”

Example 2 • To o change the value of X to 0 when X becomes greater than 100, and to change the value of Y to 0 when Y becomes greater than 250. Each decision is independent of the other. The conditions are: X > 100 X becomes 0 X > 250 Y becomes 0 •

Example 2 Python Code X=input(“Enter X: ”) if X > 100 : X=0 if X > 250: Y=0

Example 2 of Straight Through Logic

Example 3 • Find the score of a student of 5 subject marks • Score >= 68 then display distinction class • Score >= 60 and Score<68 then display first class • Score >= 50 and Score < 60 then display second class • Score >= 40 and Score < 50 then display Pass Class • Score < 40 then display Fail

Example 3 Python Code perc=input(“Enter percentage: ”) if perc >= 68: print “distinction class” if perc >= 60 and perc<68: print “first class” if perc >= 50 and perc< 60: print “second class” if perc >= 40 and perc < 50: print “Pass Class” If perc < 40: print “Fail”

Example 3 Algorithm 1)Start 2) Read perc 3)if perc >= 68 then print “distinction class” end if 4)if perc >= 60 and perc<68: print “first class” end if 5)if perc >= 50 and perc< 60: print “second class” end if 6)if perc >= 40 and perc < 50: print “Pass Class” end if 7)if perc < 40: print “Fail” end if 8)Stop

Example 3 Straight Through Logic if perc>68 F if perc>=60 and perc<68 F if perc>=50 an d perc<60 T Print “Distinction” T Print “First Class” T Print “Second Class” F if perc>=40 an d perc<50 F T if perc<40 T F Print “Pass Class” Print “Fail”

Example 4 • Check number is even or odd. • Python: n=input(“Enter N: ”) if n%2==0: print “Even” if n%2==1: print “Odd”

Example 4 Algorithm 1) Start 2) Read n 3) if n%2==0 then print “Even” end if 4) if n%2==1 then print “Odd” end if 5) Stop

Example 4 Straight Through Logic if n%2==0 T Print “Even” T Print “Odd” F if n%2==1 F

2. Positive Logic • Positive Logic: allows the flow of the processing to continue through the module instead of processing succeeding decisions, once the resultant of a decision is True. • Whenever the resultant is False, another decision in the sequence is processed until the resultant is True, • or there are no more decisions to process. At that time, the False branch processes the remaining instructions. •

Example 1 • find the amount to charge people of varying ages for a concert ticket. • When the person is under 16, the charge is $7; • When the person is 65 or over, the charge is $5; all others are charged $10. The conditions are • the following: • Age < 16 $7 • Age >= 16 and Age < 65 $10 • Age >= 65 $5 •

Example 1 Python Code age=input(“Enter N: ”) if age< 16: print “Charge 7” elif age<65: print “Charge 10” else: print “Charge 5” •

Example 1 of Positive Logic

Example 2 of Positive Logic • When the person has sold more than $6, 000, the commission is 10%. The conditions are • the following: • Sales Commission • 2000. 02 • 2001– 4000. 04 • 4001– 6000. 07 • >6000. 10

Example 2 Python Code s=input(“Enter sales: ”) if s<=2000: c=0. 02 elif s<=4000: c=0. 04 elif s<=6000: c=0. 07 else: c=0. 10

Example 2 Algorithm 1)Start 2) Read s 3) if s<=2000 then c=0. 02 else if s<=4000 then c=0. 04 else if s<=6000 then c=0. 07 else c=0. 10 end if 4)Stop

Example 2 of Positive Logic

Example 3 • Find the score of a student of 5 subject marks • Score >= 68 then display distinction class • Score >= 60 and Score<68 then display first class • Score >= 50 and Score < 60 then display second class • Score >= 40 and Score < 50 then display Pass Class • Score < 40 then display Fail

Example 3 Python Code perc=input(“Enter percentage: ”) if perc >= 68: print “distinction class” elif perc >= 60: print “first class” elif perc >= 50 : print “second class” elif perc >= 40 : print “Pass Class” else: print “Fail”

Example 3 Algorithm 1)Start 2) Read perc 3)if perc >= 68 then print “distinction class” else if perc >= 60 then print “first class” else if perc >= 50 then print “second class” else if perc >= 40 then print “Pass Class” else print “Fail” end if 8)Stop

Example 3 Positive Logic if perc>68 T Print “Distinction” F if perc>=60 T Print “First Class” T Print “Second Class” F if perc>=50 F if perc>=40 F Print “Fail” T Print “Pass Class”

Example 4 • Check number is even or odd. • Python: n=input(“Enter N: ”) if n%2==0: print “Even” else: print “Odd”

Example 4 Algorithm 1) Start 2) Read n 3) if n%2==0 then print “Even” else print “Odd” end if 5) Stop

Example 4 Positive Logic if n%2==0 F Print “Odd” T Print “Even”

2. Negative Logic • Negative Logic: It is similar to positive logic except that the flow of the processing continues through the module when the resultant of a decision is False. • Whenever the resultant is True, another decision is processed until the resultant is False, • or there are no more decisions to process. At that time, the True branch processes the remaining instructions. • Negative logic is the hardest to use and understand.

Example 1 • find the amount to charge people of varying ages for a concert ticket. • When the person is under 16, the charge is $7; • When the person is 65 or over, the charge is $5; all others are charged $10. The conditions are • the following: • Age < 16 $7 • Age >= 16 and Age < 65 $10 • Age >= 65 $5 •

Example 1 of Negative Logic

Python Code of Straight Through Logic age=input(“Enter N: ”) if age>= 16: if age >= 65: print “Charge 5” else: print “Charge 10” else: print “Charge 7”

Example 2 of Negative Logic • When the person has sold more than $6, 000, the commission is 10%. The conditions are • the following: • Sales Commission • 2000. 02 • 2001– 4000. 04 • 4001– 6000. 07 • >6000. 10

Example 2 Python Code s=input(“Enter sales: ”) if s>2000: if s>4000: if s>6000: c=0. 10 else: c=0. 07 else: c=0. 04 else: c=0. 02

1) Start Example 2 Algorithm 2) Read s 3) if s>2000 then if s>4000 then if s>6000 then c=0. 10 else then c=0. 07 end if else: c=0. 04 end if else: c=0. 02 end if 4) Stop

Example 2 of Negative Logic

Example 3 • Find the score of a student of 5 subject marks • Score >= 68 then display distinction class • Score >= 60 and Score<68 then display first class • Score >= 50 and Score < 60 then display second class • Score >= 40 and Score < 50 then display Pass Class • Score < 40 then display Fail

Example 3 Python Code perc=input(“Enter percentage: ”) if perc < 68: if perc < 60: if perc < 50: if perc < 40: print “Fail” else: print “Pass Class” else: print “second class else: print “first class” else: print “distinction class”

Example 3 Algorithm 1)Start 2) Read perc 3) if perc < 68 then if perc < 60 then if perc < 50 then if perc < 40 then print “Fail” else print “Pass Class” end if else print “second class end if else print “first class” end if else print “distinction class” end if 4)Stop

Example 3 Negative Logic if perc<68 T F Print “Distinction” F Print “First Class” if perc<60 T if F Print “Second Class” perc<50 T if F Print “Pass Class” perc<40 T Print “Fail”

Example 4 • Check number is even or odd. • Python: n=input(“Enter N: ”) if n%2==1: print “Odd” else: print “Even”

Example 4 Algorithm 1) Start 2) Read n 3) if n%2==1 then print “Odd” else print “Even” end if 5) Stop

Example 4 Negative Logic if n%2==1 F Print “EVEN” T Print “ODD”

Nested if/then/else instructions • Decisions using positive or negative logic use nested if/then/else instructions. • Each level of in each level of a decision is embedded in level before it.

Nested if/then/else instructions

Nested if/then/else instructions vs straight through logic

Logic Conversion • It is necessary to change the logic from positive to negative or vice versa in order to improve the efficiency or readability of solution

Conversion from Positive Logic to Negative Logic

Conversion from Positive Logic to Negative Logic

Four Ways to Design a Set of Conditions

Four Ways to Design a Set of Conditions

Four Ways to Design a Set of Conditions

Four Ways to Design a Set of Conditions

Four Ways to Design a Set of Conditions

Summary • • Straight Through Logic Structures Positive Logic Structures Negative Logic Structures Exaples

Important Questions • • Explain the Four Logic Structures. Explain the Sequential Logic Structures Explain the straight through logic structures Explain positive logic with example Explain negative logic with example Explain types of Decision Logic Structures Solve the problem i. e. find score of student for 5 subjects.
- Slides: 68