Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit a887caa

Browse files
committed
Add Nested if condition in python conditionals file
1 parent 1951b3c commit a887caa

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

content/python/concepts/conditionals/conditionals.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,40 @@ else:
9191
```
9292

9393
What do you think will happen if `pH` is changed to 7?
94+
95+
96+
```
97+
## Nested If Statements
98+
99+
Nested `if` statements are used when you want to check a condition inside another condition. This is useful when decisions depend on multiple factors. The outer if runs first, and only `if` it evaluates to `True` does the inner `if` get evaluated.
100+
101+
Nested `if` statements help make your code more organized and allow more complex decision-making without repeating conditions.
102+
103+
```py
104+
age = 20
105+
has_membership = True
106+
107+
if age >= 18:
108+
print("You are an adult.")
109+
110+
if has_membership:
111+
print("You can enter the club.")
112+
else:
113+
print("You need a membership to enter.")
114+
else:
115+
print("You are too young to enter.")
116+
```
117+
118+
Explanation:
119+
120+
1. The outer if checks if the person is 18 or older.
121+
122+
2. Only if the outer condition is True, the inner if checks whether they have a membership.
123+
124+
3. This prevents repeating the age check for every membership condition.
125+
126+
Output:
127+
```
128+
You are an adult.
129+
You can enter the club.
130+
```

0 commit comments

Comments
 (0)