In programming, especially when working with C, we often deal with complex decision-making situations. These decisions involve choosing an execution path based on multiple conditions. To make this process simpler and error-free, programmers use Decision Trees and Decision Tables before writing the actual code.
These tools help visualize logic, handle multiple conditions clearly, and reduce mistakes in conditional branching.
1. What Are Decision Trees?
A Decision Tree is a graphical representation of decisions and their possible outcomes. It uses a tree-like structure where each node represents a condition or decision, and each branch represents the result of that decision.
Why Use Decision Trees?
- To handle complex nested conditions
- To visualize multi-level decision-making
- To avoid confusion in
if-elsechains - To check all possible paths easily
Decision trees are especially useful when many conditions depend on each other.
2. Structure of a Decision Tree
A decision tree contains:
- Root Node: The first main decision
- Branches: Yes/No or True/False paths
- Internal Nodes: Sub-decisions
- Leaf Nodes: Final outcome or action
3. Example of a Decision Tree in C Programming
Problem:
Write a program that decides if a person is eligible to vote and if they need a voter ID.
Logic:
- Must be 18 or older to vote.
- If age ≥ 18, check if they have a voter ID.
Decision Tree Diagram (Text Form)
[Start]
|
Is age >= 18?
/ \
Yes No
| |
Do you have voter ID? [Not Eligible]
/ \
Yes No
| |
[Eligible] [Apply for ID]
Equivalent C Code
#include <stdio.h>
int main() {
int age, id;
printf("Enter age: ");
scanf("%d", &age);
if (age >= 18) {
printf("Do you have a voter ID? (1 for Yes, 0 for No): ");
scanf("%d", &id);
if (id == 1)
printf("Eligible to vote.");
else
printf("Eligible but must apply for Voter ID.");
} else {
printf("Not eligible to vote.");
}
return 0;
}
Decision trees help plan these nested decisions clearly.
4. What Are Decision Tables?
A Decision Table is a structured way to represent complex logic using rows and columns.
It lists all possible conditions and the resulting actions in a tabular format.
Why Use Decision Tables?
- Ensures no combination of conditions is missed
- Best for business logic or rule-based programs
- Avoids logical duplication
- Easy to read and maintain
Decision tables are especially useful when multiple conditions can occur together.
5. Structure of a Decision Table
A decision table consists of:
- Condition Stub: List of conditions
- Condition Entries: True/False or values
- Action Stub: List of possible actions
- Action Entries: What action happens when conditions are met
6. Example of a Decision Table in C
Problem:
Determine electricity bill discount based on two conditions:
- Customer Type (Domestic / Commercial)
- Units Consumed (>500 or ≤500)
Decision Table
| Condition / Rule | Rule 1 | Rule 2 | Rule 3 | Rule 4 |
|---|---|---|---|---|
| Customer Type = Domestic | T | T | F | F |
| Units > 500 | T | F | T | F |
| Action | ||||
| Discount = 20% | Yes | No | No | No |
| Discount = 10% | No | Yes | Yes | No |
| No Discount | No | No | No | Yes |
Interpretation of Rules
- Rule 1: Domestic + Units > 500 → 20% Discount
- Rule 2: Domestic + Units ≤ 500 → 10% Discount
- Rule 3: Commercial + Units > 500 → 10% Discount
- Rule 4: Commercial + Units ≤ 500 → No Discount
C Code Implementation
#include <stdio.h>
int main() {
char type;
int units;
float discount = 0;
printf("Enter Customer Type (D/C): ");
scanf(" %c", &type);
printf("Enter Units Consumed: ");
scanf("%d", &units);
if (type == 'D' || type == 'd') { // Domestic
if (units > 500)
discount = 20;
else
discount = 10;
}
else if (type == 'C' || type == 'c') { // Commercial
if (units > 500)
discount = 10;
else
discount = 0;
}
printf("Applicable Discount: %.2f%%", discount);
return 0;
}
7. Decision Trees vs Decision Tables
| Feature | Decision Tree | Decision Table |
|---|---|---|
| Visual Form | Diagram (tree structure) | Table (rows & columns) |
| Best For | Step-by-step decisions | Multiple combined conditions |
| Detection Of | Flow errors | Missing condition combinations |
| Easy For | Beginners | System analysts |
8. When to Use What?
Use a Decision Tree When:
- Decisions occur one after another
- There are hierarchical “if-else” conditions
- You want a clear visual of the flow
Use a Decision Table When:
- Multiple conditions interact simultaneously
- You need to test all combinations
- You want to avoid missing edge cases
Conclusion
Decision Trees and Decision Tables are powerful tools that make programming logic clear, structured, and error-free. They help visualize conditions, organize logic, and avoid mistakes before writing code in C. Whether you’re a student or a professional, using these methods improves problem-solving and builds better programs.
🔗 View other articles about PHP:
https://savanka.com/category/learn/c-programming