python LogoConditional Statements (if, else, elif)

Conditional statements are fundamental constructs in programming that allow your program to make decisions and execute different blocks of code based on whether certain conditions are true or false. They are essential for creating dynamic and responsive applications.

- `if` Statement:
The `if` statement is the most basic form of a conditional statement. It evaluates a given condition. If the condition is `True`, the code block indented directly below the `if` statement is executed. If the condition is `False`, that code block is skipped.

Syntax:
```python
if condition:
Code to be executed if condition is True
```

- `else` Statement:
The `else` statement is used in conjunction with an `if` statement. It provides an alternative block of code to be executed when the `if` condition (and any preceding `elif` conditions) evaluates to `False`. The `else` block acts as a fallback for all other cases not covered by the `if` or `elif` conditions.

Syntax:
```python
if condition:
Code to be executed if condition is True
else:
Code to be executed if condition is False
```

- `elif` Statement:
The `elif` statement, which stands for "else if," allows you to check multiple conditions sequentially. It is used when you have more than two possible outcomes or paths in your program. If the initial `if` condition is `False`, Python proceeds to check the first `elif` condition. If that's also `False`, it moves to the next `elif`, and so on. As soon as an `elif` condition evaluates to `True`, its corresponding code block is executed, and the rest of the `elif`/`else` chain is skipped. An optional `else` statement can be placed at the very end to handle any cases not explicitly caught by the `if` or `elif` conditions.

Syntax:
```python
if condition1:
Code to be executed if condition1 is True
elif condition2:
Code to be executed if condition1 is False and condition2 is True
elif condition3:
Code to be executed if condition1 and condition2 are False, and condition3 is True
else:
Code to be executed if all preceding conditions are False
```

Key Points:
- Indentation: Python uses indentation (whitespace) to define code blocks. All statements within an `if`, `elif`, or `else` block must be indented consistently.
- Colon (`:`): Every `if`, `elif`, and `else` statement must end with a colon.
- Conditions: Conditions are expressions that typically use comparison operators (`==`, `!=`, `>`, `<`, `>=`, `<=`) and logical operators (`and`, `or`, `not`) to evaluate to a Boolean value (`True` or `False`).

Example Code

 --- Example of Conditional Statements (if, else, elif) ---

 Scenario 1: Assigning a grade based on a score
score = 85  Let's assume a student's score

print(f"Student score: {score}")

if score >= 90:
    print("Grade: A")
elif score >= 80:  This condition is checked only if score < 90
    print("Grade: B")
elif score >= 70:  This condition is checked only if score < 80
    print("Grade: C")
elif score >= 60:  This condition is checked only if score < 70
    print("Grade: D")
else:  This block is executed if all above conditions are False (score < 60)
    print("Grade: F")

print("\n" + "-" - 30 + "\n")

 Scenario 2: Checking if a number is positive, negative, or zero
number = -7

print(f"Number to check: {number}")

if number > 0:
    print("The number is positive.")
elif number < 0:
    print("The number is negative.")
else:
    print("The number is zero.")

print("\n" + "-" - 30 + "\n")

 Scenario 3: A simple if-else example for age verification
age = 17

print(f"Age: {age}")

if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

print("\n" + "-" - 30 + "\n")

 Scenario 4: Using logical operators within conditions
temperature = 25
is_raining = True

print(f"Temperature: {temperature}°C, Raining: {is_raining}")

if temperature > 20 and not is_raining:
    print("It's a great day for outdoor activities!")
elif temperature > 10 and is_raining:
    print("It's cool and rainy, bring an umbrella.")
else:
    print("It might be cold or an unusual weather day.")