Skip to content

Commit d2e2fc5

Browse files
committed
fixing_nested_if
1 parent 43b6c11 commit d2e2fc5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

multiple_comditions.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
while True:
2+
try:
3+
user = int(input("enter any number b/w 1-3\n"))
4+
if user == 1:
5+
print("in first if")
6+
elif user == 2:
7+
print("in second if")
8+
elif user ==3:
9+
print("in third if")
10+
else:
11+
print("Enter numbers b/w the range of 1-3")
12+
except:
13+
print("enter only digits")
14+
15+
16+
"""
17+
## Why we are using elif instead of nested if ?
18+
When you have multiple conditions to check, using nested if means that if the first condition is true, the program still checks the second
19+
if condition, even though it's already decided that the first condition worked. This makes the program do more work than necessary.
20+
On the other hand, when you use elif, if one condition is satisfied, the program exits the rest of the conditions and doesn't continue checking.
21+
It’s more efficient and clean, as it immediately moves to the correct option without unnecessary steps.
22+
"""

0 commit comments

Comments
 (0)