Decision Making in Python (with Diagram)
In Python, decision making allows a program to choose different actions based on conditions. The program evaluates a condition and decides which block of code should execute.
Decision making is done using conditional statements like if, if-else, and if-elif-else.
if Statement
The if statement executes a block of code only when the condition is True.
Syntax
if condition:
statement
Example
x = 10
if x > 5:
print("x is greater than 5")
Diagram (Flowchart)
Start
|
Condition
|
+----+----+
| |
True False
| |
Execute Skip
Statement Statement
|
End
if – else Statement
The if-else statement is used when there are two possible outcomes.
Example
num = 7
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Diagram
Start
|
Check Condition
|
+---+---+
| |
True False
| |
Print Even Print Odd
| |
+-----+
|
End
if – elif – else Statement
Used when multiple conditions must be checked.
Example
marks = 80
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Diagram
Start
|
marks >= 90 ?
/ \
Yes No
| |
Grade A marks >= 60 ?
/ \
Yes No
| |
Grade B Grade C
|
End
Summary Table
| Decision Statement | Purpose |
|---|---|
if | Executes when condition is true |
if-else | Two-way decision |
if-elif-else | Multiple conditions |
Conclusion:
Decision making helps Python programs control the flow of execution and perform different actions based on conditions.
Comments
Post a Comment