You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Conditional statements are a fundamental part of programming that allow you to make decisions and execute different blocks of code based on certain conditions. In Python, you can use `if`, `elif` (short for "else if"), and `else` to create conditional statements.
4
+
5
+
## `if` Statement
6
+
7
+
The `if` statement is used to execute a block of code if a specified condition is `True`. If the condition is `False`, the code block is skipped.
8
+
9
+
```python
10
+
if condition:
11
+
# Code to execute if the condition is True
12
+
```
13
+
14
+
- Example:
15
+
16
+
```python
17
+
x =10
18
+
if x >5:
19
+
print("x is greater than 5")
20
+
```
21
+
22
+
## `elif` Statement
23
+
24
+
The `elif` statement allows you to check additional conditions if the previous `if` or `elif` conditions are `False`. You can have multiple `elif` statements after the initial `if` statement.
25
+
26
+
```python
27
+
if condition1:
28
+
# Code to execute if condition1 is True
29
+
elif condition2:
30
+
# Code to execute if condition2 is True
31
+
elif condition3:
32
+
# Code to execute if condition3 is True
33
+
# ...
34
+
else:
35
+
# Code to execute if none of the conditions are True
36
+
```
37
+
38
+
- Example:
39
+
40
+
```python
41
+
x =10
42
+
if x >15:
43
+
print("x is greater than 15")
44
+
elif x >5:
45
+
print("x is greater than 5 but not greater than 15")
46
+
else:
47
+
print("x is not greater than 5")
48
+
```
49
+
50
+
## `else` Statement
51
+
52
+
The `else` statement is used to specify a block of code to execute when none of the previous conditions (in the `if` and `elif` statements) are `True`.
0 commit comments