What are Conditional Statements?
Conditional statements allow us to make decisions in a program based on certain conditions.
If the condition is true, a certain block of code runs; otherwise, it may skip or execute something else.
When we write a program, sometimes we want to execute a block of code only if a certain condition is true. For example, giving "A" grade only if marks are above 90.Types of Conditional Statements in Python:
| S.No | Type | Description |
|---|---|---|
| 1. | if |
Executes a block if the condition is True |
| 2. | if-else |
Chooses between two blocks (True or False) |
| 3. | if-elif-else |
Chooses between multiple conditions |
Real-Life Example:
If it rains, take an umbrella.
Else,
wear sunglasses.
This is how conditional logic works in programming too.
General Syntax:
if condition:
# code block
elif another_condition:
# code block
else:
# code block
Upcoming Sections:
ifStatement: Click hereif-elseStatement: Click hereif-elif-elseStatement: Click here