# If Statements

{% embed url="<https://www.youtube.com/embed/qYkwh7xp-K4>" %}

&#x20;[Control Flow](http://python.swaroopch.com/control_flow.html)

Programs aren't very interesting when they only do one thing. We like programs to be multi-purposed and to accept inputs. In order to accomplish this we need some sort of control that allows us to switch on data.

## The if statement

If statements allow us to do something conditionally.

```python
if (condition):
    print('do something if our condition is true')
```

The if statement uses the reserved word `if` and then has parentheses which contain a condition. Only boolean values can go inside the parentheses. If that value is `True`, then the command(s) that are inside the if statement will be executed.

The if statement has a colon after the parentheses which indicates that a ***block*** is going to follow. The contents of the block are indented in Python. It is important that this indentation is consistent. If you indent your blocks by a single tab, then you must always do this. If you use four spaces then you must similarly always use 4 spaces. When you want to end the block then you simply go back to the indentation level you were at before the if.

```python
if (True):
    print("I'm inside the if")
print("I'm outside the if")
```

&#x20;*Try creating your own if statement!*

## Else

Sometimes we want to do something when a condition is `True` and something else when it is false. An else block allows you to switch on conditions like this:

```python
happy = True
if (happy):
    print("clap your hands")
else:
    print("stomp your feet")
```

You can also switch on multiple conditions using an `elif` (else if) statement. An `elif` is just like an `if` but comes after an initial `if`.

```python
mood = 'Grumpy'
if (mood == 'Morose');
    print("We should be friends")
elif (mood == 'Grumpy'):
    print("Don't be coming around me")
else:
    print("I guess I don't recognize your mood")
```

## Complex Examples

You can imagine more complex conditions where multiple variables come into play

```python
age = 11
mood = 'Grumpy'
family_member = True
if (mood == 'Morose' and age > 10 or family_member);
    print("We should be friends")
```

The above condition uses `and` and `or` to combine multiple conditions. When you say the whole statement out loud you can tell what should happen. In this case `mood` must be equal to `'Morose'` *and* `age` must be greater than 10. Since that combined condition is `False` we would not enter the if block. We have a third condition though. The `family_member` condition is also part of this and since there is an `or` combining the previous two conditions and this one, only one *or* the other is necessary to enter the block. Since `family_member` is `True` we will enter.

&#x20;*Try collecting some data from the user (input) and doing an if statement based on their response!*

## Tip

You might look at the `=` and `==` and wonder how they are different. The single `=` is used as the *assignment* operator. This operator assigns variables on the left of the sign to the value of whatever expression is on the right. The `==` operator is used to express boolean conditions. The two operands in this case must be exactly equal to be evaluated as True. `42 == "42"` may look like they are equal but they have different types. One is an `int` and the other is a `str`. Things can never be equal if they are of different types.


---

# 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://programming.reedcwilson.com/lesson-1/if-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.
