# Conditional Statements

Conditional statements are one of the first logical flows you will learn. They're fundamental in almost every programming language, and allow you to execute different **blocks** of code based on different **conditions** being satisfied. Hence they're called "conditional statements".

The syntax of a conditional statement in Python is as follows:

```python
if something:
    do something cool
```

Changes in indentation (like on **line 2**) signify the start and end of the **block** of code to be executed.

Changes in indentation (like on **line 2**) signify the start and end of the **block** of code to be executed.

```python
hour = int(input("Enter the hour: "))

if hour <= 12:
    print("Good morning!")
if (hour >= 12):
    print("Good afternoon!")
```

This program takes an integer from the user, and outputs "Good morning!" or "Good afternoon!" based on the integer value. It makes use of the **`int()`** function to force the conversion of the user's input into the **integer** data type, so that it can be processed as such.

But, what if both conditions are met in the above program, such as with an input of **12**? Well, both **blocks** will be executed! How do we ensure only the first satisfied block is executed? Using the **`elif`** statement:

```python
if hour <= 12:
    print("Good morning!")
elif (hour > 12 and hour <= 18):
    print("Good afternoon!")
elif (hour > 18):
    print("Good evening!")
```

Here, not only have we ensured only one **block** will ever be executed, but we've made use of several **`elif`** statements to also include an output for "Good evening!".

{% hint style="info" %}
There is no limit as to how many consecutive **`if`** or **`elif`** statements you can use.
{% endhint %}

This is great, but what if we want to inform the user they're entering wrong data if they input a value outside of the range **0-23**. We could adjust the conditions, but why not use the added **`else`** statement?

```python
hour = int(input("Enter the hour: "))

if (hour >= 0 and hour <= 12):
    print("Good morning!")
elif (hour > 12 and hour <= 18):
    print("Good afternoon!")
elif (hour > 18 and hour <= 23):
    print("Good evening!")
else:
    print("Please enter an integer in range 0-23!")
```

Now the program will output "Please enter an integer in range 0-23!" if the user attempts to enter any integer outside of this range. Awesome!

{% hint style="info" %}
You should only use one **`else`** statement at the end of a sequence of **`if`** and **`elif`** statements.
{% endhint %}

{% hint style="info" %}
If the user inputs a value that can't be converted into an integer, an error will be thrown by the program!
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hackathonsforschools.gitbook.io/student-summer-sprint-1/technical-workshops/introduction-to-python/conditional-statements.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
