> For the complete documentation index, see [llms.txt](https://calnotes.gitbook.io/cs61a-guidebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnotes.gitbook.io/cs61a-guidebook/building-blocks/control-flow/what-is-an-if-statement.md).

# What is an if statement?

Conditional execution.

As it stands right now, we've seen Python as a series of statements that we execute one after another. However, we may wish to, on occasion, **conditionally** execute a piece of code. For example, if a variable `a` is set to 1, we want to print "Hello!", print "World!" if it is set to 2, or print "Bye" otherwise. We can do so through if statements.

```python
if (a == 1):
    print("Hello!")
elif (a == 2):
    print("World!")
else:
    print("Bye")
```

In an if-else block, **Python will only ever execute one suite of statements**.&#x20;

Something fascinating happens in if-statements. Not only does Python not run the statements for which the condition is not True, it does not even execute them — errors are ignored. Consider the following:

```python
a = 1
if (a == 1):
    print("Hello!")
else:
    print(1/0)
```

This code does not error, because Python never ends up reading the else-block — the condition is True, and the if suite is executed instead.

### Evaluating if Statement Conditions

In our section on [Data Types](/cs61a-guidebook/building-blocks/expressions-in-python/data-types-in-python.md), we had talked about truth-y and false-y values. If conditions basd on such values check for their truthiness or falsiness by the same design. For example

```python
if a:
    print("Hello")
else:
    print("World")
```

Setting `a=1` would print `Hello`, while setting `a=0` would print `World`.
