🐍 Decision Table
Definition • Structure • Rules • Examples • Python Implementation
1. What is a Decision Table?
It is especially useful when a problem contains multiple conditions and multiple possible actions.
Conditions → Rules → Actions
2. Main Components of a Decision Table
Conditions
Conditions are the questions or situations that must be evaluated.
Example: Age ≥ 18?Condition Entries
These indicate whether a condition is True, False, Yes, or No.
Actions
Actions are the operations performed when conditions are satisfied.
Rules
Each column generally represents one possible combination of conditions and its corresponding action.
3. General Structure of a Decision Table
| Conditions | |||
|---|---|---|---|
| Condition 1 | Y | Y | N |
| Condition 2 | Y | N | Y |
| Actions | |||
| Action 1 | ✓ | ✗ | ✗ |
| Action 2 | ✗ | ✓ | ✓ |
Here Y = Yes/True and N = No/False.
4. Example – Student Pass or Fail
Problem
A student passes if the marks are greater than or equal to 40. Otherwise, the student fails.
Decision Table
| Condition / Action | Rule 1 | Rule 2 |
|---|---|---|
| Marks ≥ 40? | Yes | No |
| Display "PASS" | ✓ | ✗ |
| Display "FAIL" | ✗ | ✓ |
Python Program
marks = int(input("Enter marks: "))
if marks >= 40:
print("PASS")
else:
print("FAIL")
Output
5. Example – Login System
Consider a login system with two conditions:
- Username is correct.
- Password is correct.
Decision Table
| Condition / Action | Rule 1 | Rule 2 | Rule 3 | Rule 4 |
|---|---|---|---|---|
| Username Correct? | Yes | Yes | No | No |
| Password Correct? | Yes | No | Yes | No |
| Login Successful | ✓ | ✗ | ✗ | ✗ |
| Login Failed | ✗ | ✓ | ✓ | ✓ |
Python Implementation
username = input("Username: ")
password = input("Password: ")
if username == "admin" and password == "1234":
print("Login Successful")
else:
print("Login Failed")
Output
6. Example – Discount Calculation
Suppose a shop provides a discount based on purchase amount.
| Condition / Action | Rule 1 | Rule 2 | Rule 3 |
|---|---|---|---|
| Amount ≥ 5000? | Yes | No | No |
| Amount ≥ 2000? | — | Yes | No |
| 20% Discount | ✓ | ✗ | ✗ |
| 10% Discount | ✗ | ✓ | ✗ |
| No Discount | ✗ | ✗ | ✓ |
Python Program
amount = float(input("Enter purchase amount: "))
if amount >= 5000:
discount = amount * 0.20
elif amount >= 2000:
discount = amount * 0.10
else:
discount = 0
final_amount = amount - discount
print("Discount =", discount)
print("Final Amount =", final_amount)
7. Advantages of Decision Tables
✅ Advantages
- Easy to understand.
- Clearly represents multiple conditions.
- Helps identify missing combinations.
- Reduces logical errors.
- Useful for complex decision-making problems.
- Helps programmers design conditional statements.
- Useful for testing and test-case generation.
- Provides clear documentation.
- Easy to compare different rules.
❌ Disadvantages
- Large numbers of conditions can create large tables.
- Tables can become difficult to read.
- Creating all possible combinations may be time-consuming.
- Not ideal for simple sequential problems.
- Complex actions may require additional explanation.
- Frequent changes may require table modification.
8. Decision Table vs Flowchart
| Decision Table | Flowchart |
|---|---|
| Uses rows and columns. | Uses graphical symbols. |
| Best for multiple conditions and combinations. | Best for showing program flow. |
| Clearly shows different rules. | Clearly shows sequence of operations. |
| Compact for complex decisions. | Can become large for complex decisions. |
| Useful for generating test cases. | Useful for understanding program execution. |
9. Steps to Create a Decision Table
- Identify all important conditions.
- Identify all possible actions.
- List the possible values of each condition.
- Generate possible combinations of conditions.
- Create a separate rule for each combination.
- Determine the appropriate action for every rule.
- Remove impossible or unnecessary rules.
- Verify the completed decision table.
10. Important Points for Examination
- A decision table represents decision logic in tabular form.
- Conditions describe situations that must be evaluated.
- Actions describe what should happen.
- Each rule represents a combination of conditions.
- Decision tables are useful when multiple conditions exist.
- They can help identify missing or contradictory rules.
- They are useful for designing and testing programs.
- Decision tables can be converted into Python
if-elif-elselogic.
11. Quick Summary
Basic Structure:
Conditions → Condition Values → Rules → Actions
Common Python Statements Used:
if → First conditionelif → Additional conditionelse → Default action
Decision tables are particularly useful for complex decision-making and test-case design.
No comments:
Post a Comment